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 | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 59 | // Do not log anything |
| 60 | void BlackholeLogger(gpr_log_func_args* args) {} |
| 61 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 62 | // 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 Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 64 | bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only, |
| 65 | int deadline_secs) { |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 66 | grpc::ClientContext context; |
| 67 | EmptyMessage message; |
| 68 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 69 | std::chrono::system_clock::time_point deadline = |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 70 | std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs); |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 71 | |
| 72 | context.set_deadline(deadline); |
| 73 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 74 | std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader( |
| 75 | stub->GetAllGauges(&context, message)); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 76 | |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 77 | GaugeResponse gauge_response; |
| 78 | long overall_qps = 0; |
Sree Kuchibhotla | 4d0f2f9 | 2015-11-03 15:55:43 -0800 | [diff] [blame] | 79 | while (reader->Read(&gauge_response)) { |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 80 | if (gauge_response.value_case() == GaugeResponse::kLongValue) { |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 81 | if (!total_only) { |
Sree Kuchibhotla | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 82 | std::cout << gauge_response.name() << ": " |
| 83 | << gauge_response.long_value() << std::endl; |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 84 | } |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 85 | overall_qps += gauge_response.long_value(); |
| 86 | } else { |
Sree Kuchibhotla | 18a0e47 | 2016-06-29 12:04:09 -0700 | [diff] [blame] | 87 | std::cout << "Gauge '" << gauge_response.name() << "' is not long valued" |
Sree Kuchibhotla | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 88 | << std::endl; |
Sree Kuchibhotla | b047c0f | 2015-11-16 11:52:54 -0800 | [diff] [blame] | 89 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Sree Kuchibhotla | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 92 | std::cout << overall_qps << std::endl; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 93 | |
| 94 | const grpc::Status status = reader->Finish(); |
| 95 | if (!status.ok()) { |
Sree Kuchibhotla | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 96 | std::cout << "Error in getting metrics from the client" << std::endl; |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 97 | } |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 98 | |
| 99 | return status.ok(); |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int main(int argc, char** argv) { |
| 103 | grpc::testing::InitTest(&argc, &argv, true); |
| 104 | |
Sree Kuchibhotla | ad57b26 | 2016-06-27 20:46:37 -0700 | [diff] [blame] | 105 | // 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 Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 109 | |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 110 | std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel( |
| 111 | FLAGS_metrics_server_address, grpc::InsecureChannelCredentials())); |
| 112 | |
Sree Kuchibhotla | eaa3072 | 2016-05-17 17:18:23 -0700 | [diff] [blame] | 113 | if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only, |
| 114 | FLAGS_deadline_secs)) { |
Sree Kuchibhotla | 44ca2c2 | 2016-02-16 09:48:36 -0800 | [diff] [blame] | 115 | return 1; |
| 116 | } |
Sree Kuchibhotla | b5e98c5 | 2015-10-27 22:55:26 -0700 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |