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