metrics_client.cc
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 
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"
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
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) {
54  EmptyMessage message;
55 
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.
96 
97  std::shared_ptr<grpc::Channel> channel(
98  grpc::CreateChannel(absl::GetFlag(FLAGS_metrics_server_address),
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 }
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
absl::time_internal::cctz::time_point
std::chrono::time_point< std::chrono::system_clock, D > time_point
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:39
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
main
int main(int argc, char **argv)
Definition: metrics_client.cc:89
grpc::ClientContext::set_deadline
void set_deadline(const T &deadline)
Definition: grpcpp/impl/codegen/client_context.h:274
status
absl::Status status
Definition: rls.cc:251
PrintMetrics
bool PrintMetrics(std::unique_ptr< MetricsService::Stub > stub, bool total_only, int deadline_secs)
Definition: metrics_client.cc:51
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
gpr_log_func_args
Definition: include/grpc/impl/codegen/log.h:77
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
metrics_server.h
grpcpp.h
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
ABSL_FLAG
ABSL_FLAG(std::string, metrics_server_address, "localhost:8081", "The metrics server addresses in the fomrat <hostname>:<port>")
BlackholeLogger
void BlackholeLogger(gpr_log_func_args *)
Definition: metrics_client.cc:47
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
test_config.h
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
kDeadlineSecs
int kDeadlineSecs
Definition: metrics_client.cc:32
gpr_set_log_function
GPRAPI void gpr_set_log_function(gpr_log_func func)
Definition: log.cc:143
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8


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