xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/metrics_client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <memory>
20 #include <string>
21 
22 #include "absl/flags/flag.h"
23 
24 #include <grpc/support/log.h>
25 #include <grpcpp/grpcpp.h>
26 
27 #include "src/core/lib/gprpp/crash.h"
28 #include "src/proto/grpc/testing/metrics.grpc.pb.h"
29 #include "src/proto/grpc/testing/metrics.pb.h"
30 #include "test/cpp/util/metrics_server.h"
31 #include "test/cpp/util/test_config.h"
32 
33 int kDeadlineSecs = 10;
34 
35 ABSL_FLAG(std::string, metrics_server_address, "localhost:8081",
36           "The metrics server addresses in the fomrat <hostname>:<port>");
37 // TODO(Capstan): Consider using absl::Duration
38 ABSL_FLAG(int32_t, deadline_secs, kDeadlineSecs,
39           "The deadline (in seconds) for RCP call");
40 ABSL_FLAG(bool, total_only, false,
41           "If true, this prints only the total value of all gauges");
42 
43 using grpc::testing::EmptyMessage;
44 using grpc::testing::GaugeResponse;
45 using grpc::testing::MetricsService;
46 
47 // Do not log anything
BlackholeLogger(gpr_log_func_args *)48 void BlackholeLogger(gpr_log_func_args* /*args*/) {}
49 
50 // Prints the values of all Gauges (unless total_only is set to 'true' in which
51 // case this only prints the sum of all gauge values).
PrintMetrics(std::unique_ptr<MetricsService::Stub> stub,bool total_only,int deadline_secs)52 bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only,
53                   int deadline_secs) {
54   grpc::ClientContext context;
55   EmptyMessage message;
56 
57   std::chrono::system_clock::time_point deadline =
58       std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
59 
60   context.set_deadline(deadline);
61 
62   std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
63       stub->GetAllGauges(&context, message));
64 
65   GaugeResponse gauge_response;
66   long overall_qps = 0;
67   while (reader->Read(&gauge_response)) {
68     if (gauge_response.value_case() == GaugeResponse::kLongValue) {
69       if (!total_only) {
70         std::cout << gauge_response.name() << ": "
71                   << gauge_response.long_value() << std::endl;
72       }
73       overall_qps += gauge_response.long_value();
74     } else {
75       std::cout << "Gauge '" << gauge_response.name() << "' is not long valued"
76                 << std::endl;
77     }
78   }
79 
80   std::cout << overall_qps << std::endl;
81 
82   const grpc::Status status = reader->Finish();
83   if (!status.ok()) {
84     std::cout << "Error in getting metrics from the client" << std::endl;
85   }
86 
87   return status.ok();
88 }
89 
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91   grpc::testing::InitTest(&argc, &argv, true);
92 
93   // The output of metrics client is in some cases programmatically parsed (for
94   // example by the stress test framework). So, we do not want any of the log
95   // from the grpc library appearing on stdout.
96   gpr_set_log_function(BlackholeLogger);
97 
98   std::shared_ptr<grpc::Channel> channel(
99       grpc::CreateChannel(absl::GetFlag(FLAGS_metrics_server_address),
100                           grpc::InsecureChannelCredentials()));
101 
102   if (!PrintMetrics(MetricsService::NewStub(channel),
103                     absl::GetFlag(FLAGS_total_only),
104                     absl::GetFlag(FLAGS_deadline_secs))) {
105     return 1;
106   }
107 
108   return 0;
109 }
110