rpc_filter.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2021 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_FILTER_H__
  14. #define __RPC_FILTER_H__
  15. #include <map>
  16. #include <vector>
  17. #include <string>
  18. #include "workflow/WFTask.h"
  19. #include "workflow/WFTaskFactory.h"
  20. namespace srpc
  21. {
  22. static constexpr unsigned int OTLP_HTTP_REDIRECT_MAX = 0;
  23. static constexpr unsigned int OTLP_HTTP_RETRY_MAX = 1;
  24. static constexpr const char *OTLP_SERVICE_NAME = "service.name";
  25. static constexpr const char *OTLP_METHOD_NAME = "operation.name";
  26. static constexpr const char *SRPC_HTTP_METHOD = "http.method";
  27. static constexpr const char *SRPC_HTTP_STATUS_CODE = "http.status_code";
  28. static constexpr size_t RPC_REPORT_THREHOLD_DEFAULT = 100;
  29. static constexpr size_t RPC_REPORT_INTERVAL_DEFAULT = 1000; /* msec */
  30. using RPCModuleData = std::map<std::string, std::string>;
  31. static RPCModuleData global_empty_map;
  32. class RPCFilter
  33. {
  34. public:
  35. SubTask *create_filter_task(const RPCModuleData& data)
  36. {
  37. if (this->filter(const_cast<RPCModuleData&>(data)))
  38. return this->create(const_cast<RPCModuleData&>(data));
  39. return WFTaskFactory::create_empty_task();
  40. }
  41. private:
  42. virtual SubTask *create(RPCModuleData& data) = 0;
  43. virtual bool filter(RPCModuleData& data) = 0;
  44. public:
  45. RPCFilter(enum RPCModuleType module_type)
  46. {
  47. this->module_type = module_type;
  48. }
  49. virtual ~RPCFilter() { }
  50. enum RPCModuleType get_module_type() const { return this->module_type; }
  51. virtual bool client_begin(SubTask *task, RPCModuleData& data)
  52. {
  53. return true;
  54. }
  55. virtual bool server_begin(SubTask *task, RPCModuleData& data)
  56. {
  57. return true;
  58. }
  59. virtual bool client_end(SubTask *task, RPCModuleData& data)
  60. {
  61. return true;
  62. }
  63. virtual bool server_end(SubTask *task, RPCModuleData& data)
  64. {
  65. return true;
  66. }
  67. private:
  68. enum RPCModuleType module_type;
  69. };
  70. } // end namespace srpc
  71. #endif