3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
22 #include "absl/flags/flag.h"
24 #include <grpc/support/log.h>
25 #include <grpcpp/grpcpp.h>
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"
32 int kDeadlineSecs = 10;
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");
42 using grpc::testing::EmptyMessage;
43 using grpc::testing::GaugeResponse;
44 using grpc::testing::MetricsService;
46 // Do not log anything
47 void BlackholeLogger(gpr_log_func_args* /*args*/) {}
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,
53 grpc::ClientContext context;
56 std::chrono::system_clock::time_point deadline =
57 std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
59 context.set_deadline(deadline);
61 std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
62 stub->GetAllGauges(&context, message));
64 GaugeResponse gauge_response;
66 while (reader->Read(&gauge_response)) {
67 if (gauge_response.value_case() == GaugeResponse::kLongValue) {
69 std::cout << gauge_response.name() << ": "
70 << gauge_response.long_value() << std::endl;
72 overall_qps += gauge_response.long_value();
74 std::cout << "Gauge '" << gauge_response.name() << "' is not long valued"
79 std::cout << overall_qps << std::endl;
81 const grpc::Status status = reader->Finish();
83 std::cout << "Error in getting metrics from the client" << std::endl;
89 int main(int argc, char** argv) {
90 grpc::testing::InitTest(&argc, &argv, true);
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);
97 std::shared_ptr<grpc::Channel> channel(
98 grpc::CreateChannel(absl::GetFlag(FLAGS_metrics_server_address),
99 grpc::InsecureChannelCredentials()));
101 if (!PrintMetrics(MetricsService::NewStub(channel),
102 absl::GetFlag(FLAGS_total_only),
103 absl::GetFlag(FLAGS_deadline_secs))) {