• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <map>
22 #include <mutex>
23 
24 #include "src/proto/grpc/testing/metrics.grpc.pb.h"
25 #include "src/proto/grpc/testing/metrics.pb.h"
26 
27 /*
28  * This implements a Metrics server defined in
29  * src/proto/grpc/testing/metrics.proto. Any
30  * test service can use this to export Metrics (TODO (sreek): Only Gauges for
31  * now).
32  *
33  * Example:
34  *    MetricsServiceImpl metricsImpl;
35  *    ..
36  *    // Create QpsGauge(s). Note: QpsGauges can be created even after calling
37  *    // 'StartServer'.
38  *    QpsGauge qps_gauge1 = metricsImpl.CreateQpsGauge("foo", is_present);
39  *    // qps_gauge1 can now be used anywhere in the program by first making a
40  *    // one-time call qps_gauge1.Reset() and then calling qps_gauge1.Incr()
41  *    // every time to increment a query counter
42  *
43  *    ...
44  *    // Create the metrics server
45  *    std::unique_ptr<grpc::Server> server = metricsImpl.StartServer(port);
46  *    server->Wait(); // Note: This is blocking.
47  */
48 namespace grpc {
49 namespace testing {
50 
51 class QpsGauge {
52  public:
53   QpsGauge();
54 
55   // Initialize the internal timer and reset the query count to 0
56   void Reset();
57 
58   // Increment the query count by 1
59   void Incr();
60 
61   // Return the current qps (i.e query count divided by the time since this
62   // QpsGauge object created (or Reset() was called))
63   long Get();
64 
65  private:
66   gpr_timespec start_time_;
67   long num_queries_;
68   std::mutex num_queries_mu_;
69 };
70 
71 class MetricsServiceImpl final : public MetricsService::Service {
72  public:
73   grpc::Status GetAllGauges(ServerContext* context, const EmptyMessage* request,
74                             ServerWriter<GaugeResponse>* writer) override;
75 
76   grpc::Status GetGauge(ServerContext* context, const GaugeRequest* request,
77                         GaugeResponse* response) override;
78 
79   // Create a QpsGauge with name 'name'. is_present is set to true if the Gauge
80   // is already present in the map.
81   // NOTE: CreateQpsGauge can be called anytime (i.e before or after calling
82   // StartServer).
83   std::shared_ptr<QpsGauge> CreateQpsGauge(const grpc::string& name,
84                                            bool* already_present);
85 
86   std::unique_ptr<grpc::Server> StartServer(int port);
87 
88  private:
89   std::map<string, std::shared_ptr<QpsGauge>> qps_gauges_;
90   std::mutex mu_;
91 };
92 
93 }  // namespace testing
94 }  // namespace grpc
95 
96 #endif  // GRPC_TEST_CPP_METRICS_SERVER_H
97