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