Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 4 | * 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 Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 40 | #include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 41 | #include "src/proto/grpc/testing/metrics.pb.h" |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 42 | #include "test/cpp/util/metrics_server.h" |
| 43 | #include "test/cpp/util/test_config.h" |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 44 | |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 45 | int kDeadlineSecs = 10; |
| 46 | |
Sree Kuchibhotla | 4dd02fc | 2016-05-17 18:01:43 -0700 | [diff] [blame] | 47 | DEFINE_string(metrics_server_address, "localhost:8081", |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 48 | "The metrics server addresses in the fomrat <hostname>:<port>"); |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 49 | DEFINE_int32(deadline_secs, kDeadlineSecs, |
| 50 | "The deadline (in seconds) for RCP call"); |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 51 | DEFINE_bool(total_only, false, |
| 52 | "If true, this prints only the total value of all gauges"); |
| 53 | |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 54 | using grpc::testing::EmptyMessage; |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 55 | using grpc::testing::GaugeResponse; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 56 | using grpc::testing::MetricsService; |
| 57 | using grpc::testing::MetricsServiceImpl; |
| 58 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 59 | // 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 Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 61 | bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only, |
| 62 | int deadline_secs) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 63 | grpc::ClientContext context; |
| 64 | EmptyMessage message; |
| 65 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 66 | std::chrono::system_clock::time_point deadline = |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 67 | std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs); |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 68 | |
| 69 | context.set_deadline(deadline); |
| 70 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 71 | std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader( |
| 72 | stub->GetAllGauges(&context, message)); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 73 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 74 | GaugeResponse gauge_response; |
| 75 | long overall_qps = 0; |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 76 | while (reader->Read(&gauge_response)) { |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 77 | if (gauge_response.value_case() == GaugeResponse::kLongValue) { |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 78 | if (!total_only) { |
Yuchen Zeng | 0ba1144 | 2016-06-10 15:15:44 -0700 | [diff] [blame] | 79 | gpr_log(GPR_INFO, "%s: %lld", gauge_response.name().c_str(), |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 80 | gauge_response.long_value()); |
| 81 | } |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 82 | overall_qps += gauge_response.long_value(); |
| 83 | } else { |
yang-g | 3ff9727 | 2015-11-20 17:01:57 -0800 | [diff] [blame] | 84 | gpr_log(GPR_INFO, "Gauge %s is not a long value", |
| 85 | gauge_response.name().c_str()); |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 86 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 89 | gpr_log(GPR_INFO, "%ld", overall_qps); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 90 | |
| 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 Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 95 | |
| 96 | return status.ok(); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int main(int argc, char** argv) { |
| 100 | grpc::testing::InitTest(&argc, &argv, true); |
| 101 | |
| 102 | // Make sure server_addresses flag is not empty |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 103 | if (FLAGS_metrics_server_address.empty()) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 104 | 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 Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 111 | std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel( |
| 112 | FLAGS_metrics_server_address, grpc::InsecureChannelCredentials())); |
| 113 | |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 114 | if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only, |
| 115 | FLAGS_deadline_secs)) { |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 116 | return 1; |
| 117 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 118 | |
| 119 | return 0; |
| 120 | } |