Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / test / cpp / interop / metrics_client.cc
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/proto/grpc/testing/metrics.grpc.pb.h"
28 #include "src/proto/grpc/testing/metrics.pb.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 fomrat <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 // Do not log anything
47 void BlackholeLogger(gpr_log_func_args* /*args*/) {}
48
49 // Prints the values of all Gauges (unless total_only is set to 'true' in which
50 // case this only prints the sum of all gauge values).
51 bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only,
52                   int deadline_secs) {
53   grpc::ClientContext context;
54   EmptyMessage message;
55
56   std::chrono::system_clock::time_point deadline =
57       std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
58
59   context.set_deadline(deadline);
60
61   std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
62       stub->GetAllGauges(&context, message));
63
64   GaugeResponse gauge_response;
65   long overall_qps = 0;
66   while (reader->Read(&gauge_response)) {
67     if (gauge_response.value_case() == GaugeResponse::kLongValue) {
68       if (!total_only) {
69         std::cout << gauge_response.name() << ": "
70                   << gauge_response.long_value() << std::endl;
71       }
72       overall_qps += gauge_response.long_value();
73     } else {
74       std::cout << "Gauge '" << gauge_response.name() << "' is not long valued"
75                 << std::endl;
76     }
77   }
78
79   std::cout << overall_qps << std::endl;
80
81   const grpc::Status status = reader->Finish();
82   if (!status.ok()) {
83     std::cout << "Error in getting metrics from the client" << std::endl;
84   }
85
86   return status.ok();
87 }
88
89 int main(int argc, char** argv) {
90   grpc::testing::InitTest(&argc, &argv, true);
91
92   // The output of metrics client is in some cases programmatically parsed (for
93   // example by the stress test framework). So, we do not want any of the log
94   // from the grpc library appearing on stdout.
95   gpr_set_log_function(BlackholeLogger);
96
97   std::shared_ptr<grpc::Channel> channel(
98       grpc::CreateChannel(absl::GetFlag(FLAGS_metrics_server_address),
99                           grpc::InsecureChannelCredentials()));
100
101   if (!PrintMetrics(MetricsService::NewStub(channel),
102                     absl::GetFlag(FLAGS_total_only),
103                     absl::GetFlag(FLAGS_deadline_secs))) {
104     return 1;
105   }
106
107   return 0;
108 }