metrics_server.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "test/cpp/util/metrics_server.h"
  19. #include <grpc/support/log.h>
  20. #include <grpcpp/server.h>
  21. #include <grpcpp/server_builder.h>
  22. #include "src/proto/grpc/testing/metrics.grpc.pb.h"
  23. #include "src/proto/grpc/testing/metrics.pb.h"
  24. namespace grpc {
  25. namespace testing {
  26. QpsGauge::QpsGauge()
  27. : start_time_(gpr_now(GPR_CLOCK_REALTIME)), num_queries_(0) {}
  28. void QpsGauge::Reset() {
  29. std::lock_guard<std::mutex> lock(num_queries_mu_);
  30. num_queries_ = 0;
  31. start_time_ = gpr_now(GPR_CLOCK_REALTIME);
  32. }
  33. void QpsGauge::Incr() {
  34. std::lock_guard<std::mutex> lock(num_queries_mu_);
  35. num_queries_++;
  36. }
  37. long QpsGauge::Get() {
  38. std::lock_guard<std::mutex> lock(num_queries_mu_);
  39. gpr_timespec time_diff =
  40. gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start_time_);
  41. long duration_secs = time_diff.tv_sec > 0 ? time_diff.tv_sec : 1;
  42. return num_queries_ / duration_secs;
  43. }
  44. grpc::Status MetricsServiceImpl::GetAllGauges(
  45. ServerContext* /*context*/, const EmptyMessage* /*request*/,
  46. ServerWriter<GaugeResponse>* writer) {
  47. gpr_log(GPR_DEBUG, "GetAllGauges called");
  48. std::lock_guard<std::mutex> lock(mu_);
  49. for (auto it = qps_gauges_.begin(); it != qps_gauges_.end(); it++) {
  50. GaugeResponse resp;
  51. resp.set_name(it->first); // Gauge name
  52. resp.set_long_value(it->second->Get()); // Gauge value
  53. writer->Write(resp);
  54. }
  55. return Status::OK;
  56. }
  57. grpc::Status MetricsServiceImpl::GetGauge(ServerContext* /*context*/,
  58. const GaugeRequest* request,
  59. GaugeResponse* response) {
  60. std::lock_guard<std::mutex> lock(mu_);
  61. const auto it = qps_gauges_.find(request->name());
  62. if (it != qps_gauges_.end()) {
  63. response->set_name(it->first);
  64. response->set_long_value(it->second->Get());
  65. }
  66. return Status::OK;
  67. }
  68. std::shared_ptr<QpsGauge> MetricsServiceImpl::CreateQpsGauge(
  69. const std::string& name, bool* already_present) {
  70. std::lock_guard<std::mutex> lock(mu_);
  71. std::shared_ptr<QpsGauge> qps_gauge(new QpsGauge());
  72. const auto p = qps_gauges_.insert(std::make_pair(name, qps_gauge));
  73. // p.first is an iterator pointing to <name, shared_ptr<QpsGauge>> pair.
  74. // p.second is a boolean which is set to 'true' if the QpsGauge is
  75. // successfully inserted in the guages_ map and 'false' if it is already
  76. // present in the map
  77. *already_present = !p.second;
  78. return p.first->second;
  79. }
  80. // Starts the metrics server and returns the grpc::Server instance. Call Wait()
  81. // on the returned server instance.
  82. std::unique_ptr<grpc::Server> MetricsServiceImpl::StartServer(int port) {
  83. gpr_log(GPR_INFO, "Building metrics server..");
  84. const std::string address = "0.0.0.0:" + std::to_string(port);
  85. ServerBuilder builder;
  86. builder.AddListeningPort(address, grpc::InsecureServerCredentials());
  87. builder.RegisterService(this);
  88. std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  89. gpr_log(GPR_INFO, "Metrics server %s started. Ready to receive requests..",
  90. address.c_str());
  91. return server;
  92. }
  93. } // namespace testing
  94. } // namespace grpc