blob: 179de30805c89f4dbddeb5ead6de97764a908dc1 [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 Kuchibhotlaad57b262016-06-27 20:46:37 -070059// Do not log anything
60void BlackholeLogger(gpr_log_func_args* args) {}
61
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080062// Prints the values of all Gauges (unless total_only is set to 'true' in which
63// case this only prints the sum of all gauge values).
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070064bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only,
65 int deadline_secs) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070066 grpc::ClientContext context;
67 EmptyMessage message;
68
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080069 std::chrono::system_clock::time_point deadline =
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -070070 std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080071
72 context.set_deadline(deadline);
73
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080074 std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
75 stub->GetAllGauges(&context, message));
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070076
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080077 GaugeResponse gauge_response;
78 long overall_qps = 0;
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -080079 while (reader->Read(&gauge_response)) {
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080080 if (gauge_response.value_case() == GaugeResponse::kLongValue) {
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080081 if (!total_only) {
Sree Kuchibhotlaad57b262016-06-27 20:46:37 -070082 std::cout << gauge_response.name() << ": "
83 << gauge_response.long_value() << std::endl;
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080084 }
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080085 overall_qps += gauge_response.long_value();
86 } else {
Sree Kuchibhotla18a0e472016-06-29 12:04:09 -070087 std::cout << "Gauge '" << gauge_response.name() << "' is not long valued"
Sree Kuchibhotlaad57b262016-06-27 20:46:37 -070088 << std::endl;
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080089 }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070090 }
91
Sree Kuchibhotlaad57b262016-06-27 20:46:37 -070092 std::cout << overall_qps << std::endl;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070093
94 const grpc::Status status = reader->Finish();
95 if (!status.ok()) {
Sree Kuchibhotlaad57b262016-06-27 20:46:37 -070096 std::cout << "Error in getting metrics from the client" << std::endl;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070097 }
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -080098
99 return status.ok();
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700100}
101
102int main(int argc, char** argv) {
103 grpc::testing::InitTest(&argc, &argv, true);
104
Sree Kuchibhotlaad57b262016-06-27 20:46:37 -0700105 // The output of metrics client is in some cases programatically parsed (for
106 // example by the stress test framework). So, we do not want any of the log
107 // from the grpc library appearing on stdout.
108 gpr_set_log_function(BlackholeLogger);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700109
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -0800110 std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel(
111 FLAGS_metrics_server_address, grpc::InsecureChannelCredentials()));
112
Sree Kuchibhotlaeaa30722016-05-17 17:18:23 -0700113 if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only,
114 FLAGS_deadline_secs)) {
Sree Kuchibhotla44ca2c22016-02-16 09:48:36 -0800115 return 1;
116 }
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700117
118 return 0;
119}