blob: 7a0cb994df50e9b9c17c68a1f12947c754b5dde8 [file] [log] [blame]
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *is % allowed in string
32 */
33
34#include <memory>
35#include <string>
36
37#include <gflags/gflags.h>
38#include <grpc++/grpc++.h>
39
Craig Tiller1b4e3302015-12-17 16:35:00 -080040#include "src/proto/grpc/testing/metrics.grpc.pb.h"
41#include "src/proto/grpc/testing/metrics.pb.h"
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080042#include "test/cpp/util/metrics_server.h"
43#include "test/cpp/util/test_config.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070044
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070045int kDeadlineSecs = 10;
46
Sree Kuchibhotla4dd02fc2016-05-17 18:01:43 -070047DEFINE_string(metrics_server_address, "localhost:8081",
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070048 "The metrics server addresses in the fomrat <hostname>:<port>");
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070049DEFINE_int32(deadline_secs, kDeadlineSecs,
50 "The deadline (in seconds) for RCP call");
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080051DEFINE_bool(total_only, false,
52 "If true, this prints only the total value of all gauges");
53
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070054using grpc::testing::EmptyMessage;
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080055using grpc::testing::GaugeResponse;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070056using grpc::testing::MetricsService;
57using grpc::testing::MetricsServiceImpl;
58
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080059// Prints the values of all Gauges (unless total_only is set to 'true' in which
60// case this only prints the sum of all gauge values).
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070061bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only,
62 int deadline_secs) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070063 grpc::ClientContext context;
64 EmptyMessage message;
65
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080066 std::chrono::system_clock::time_point deadline =
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070067 std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080068
69 context.set_deadline(deadline);
70
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080071 std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
72 stub->GetAllGauges(&context, message));
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070073
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080074 GaugeResponse gauge_response;
75 long overall_qps = 0;
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080076 while (reader->Read(&gauge_response)) {
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080077 if (gauge_response.value_case() == GaugeResponse::kLongValue) {
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080078 if (!total_only) {
Yuchen Zeng0ba11442016-06-10 15:15:44 -070079 gpr_log(GPR_INFO, "%s: %lld", gauge_response.name().c_str(),
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080080 gauge_response.long_value());
81 }
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080082 overall_qps += gauge_response.long_value();
83 } else {
yang-g3ff97272015-11-20 17:01:57 -080084 gpr_log(GPR_INFO, "Gauge %s is not a long value",
85 gauge_response.name().c_str());
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080086 }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070087 }
88
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080089 gpr_log(GPR_INFO, "%ld", overall_qps);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070090
91 const grpc::Status status = reader->Finish();
92 if (!status.ok()) {
93 gpr_log(GPR_ERROR, "Error in getting metrics from the client");
94 }
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080095
96 return status.ok();
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070097}
98
99int main(int argc, char** argv) {
100 grpc::testing::InitTest(&argc, &argv, true);
101
102 // Make sure server_addresses flag is not empty
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800103 if (FLAGS_metrics_server_address.empty()) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700104 gpr_log(
105 GPR_ERROR,
106 "Cannot connect to the Metrics server. Please pass the address of the"
107 "metrics server to connect to via the 'metrics_server_address' flag");
108 return 1;
109 }
110
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -0800111 std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel(
112 FLAGS_metrics_server_address, grpc::InsecureChannelCredentials()));
113
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -0700114 if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only,
115 FLAGS_deadline_secs)) {
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -0800116 return 1;
117 }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700118
119 return 0;
120}