metrics_server.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *is % allowed in string
  17. */
  18. #ifndef GRPC_TEST_CPP_METRICS_SERVER_H
  19. #define GRPC_TEST_CPP_METRICS_SERVER_H
  20. #include <map>
  21. #include <mutex>
  22. #include <grpcpp/server.h>
  23. #include "src/proto/grpc/testing/metrics.grpc.pb.h"
  24. #include "src/proto/grpc/testing/metrics.pb.h"
  25. /*
  26. * This implements a Metrics server defined in
  27. * src/proto/grpc/testing/metrics.proto. Any
  28. * test service can use this to export Metrics (TODO (sreek): Only Gauges for
  29. * now).
  30. *
  31. * Example:
  32. * MetricsServiceImpl metricsImpl;
  33. * ..
  34. * // Create QpsGauge(s). Note: QpsGauges can be created even after calling
  35. * // 'StartServer'.
  36. * QpsGauge qps_gauge1 = metricsImpl.CreateQpsGauge("foo", is_present);
  37. * // qps_gauge1 can now be used anywhere in the program by first making a
  38. * // one-time call qps_gauge1.Reset() and then calling qps_gauge1.Incr()
  39. * // every time to increment a query counter
  40. *
  41. * ...
  42. * // Create the metrics server
  43. * std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port);
  44. * server->Wait(); // Note: This is blocking.
  45. */
  46. namespace grpc {
  47. namespace testing {
  48. class QpsGauge {
  49. public:
  50. QpsGauge();
  51. // Initialize the internal timer and reset the query count to 0
  52. void Reset();
  53. // Increment the query count by 1
  54. void Incr();
  55. // Return the current qps (i.e query count divided by the time since this
  56. // QpsGauge object created (or Reset() was called))
  57. long Get();
  58. private:
  59. gpr_timespec start_time_;
  60. long num_queries_;
  61. std::mutex num_queries_mu_;
  62. };
  63. class MetricsServiceImpl final : public MetricsService::Service {
  64. public:
  65. grpc::Status GetAllGauges(ServerContext* context, const EmptyMessage* request,
  66. ServerWriter<GaugeResponse>* writer) override;
  67. grpc::Status GetGauge(ServerContext* context, const GaugeRequest* request,
  68. GaugeResponse* response) override;
  69. // Create a QpsGauge with name 'name'. is_present is set to true if the Gauge
  70. // is already present in the map.
  71. // NOTE: CreateQpsGauge can be called anytime (i.e before or after calling
  72. // StartServer).
  73. std::shared_ptr<QpsGauge> CreateQpsGauge(const std::string& name,
  74. bool* already_present);
  75. std::unique_ptr<grpc::Server> StartServer(int port);
  76. private:
  77. std::map<string, std::shared_ptr<QpsGauge>> qps_gauges_;
  78. std::mutex mu_;
  79. };
  80. } // namespace testing
  81. } // namespace grpc
  82. #endif // GRPC_TEST_CPP_METRICS_SERVER_H