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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:40