OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 40 #include "src/proto/grpc/testing/metrics.grpc.pb.h" |
| 41 #include "src/proto/grpc/testing/metrics.pb.h" |
| 42 #include "test/cpp/util/metrics_server.h" |
| 43 #include "test/cpp/util/test_config.h" |
| 44 |
| 45 DEFINE_string(metrics_server_address, "", |
| 46 "The metrics server addresses in the fomrat <hostname>:<port>"); |
| 47 DEFINE_bool(total_only, false, |
| 48 "If true, this prints only the total value of all gauges"); |
| 49 |
| 50 int kDeadlineSecs = 10; |
| 51 |
| 52 using grpc::testing::EmptyMessage; |
| 53 using grpc::testing::GaugeResponse; |
| 54 using grpc::testing::MetricsService; |
| 55 using grpc::testing::MetricsServiceImpl; |
| 56 |
| 57 // Prints the values of all Gauges (unless total_only is set to 'true' in which |
| 58 // case this only prints the sum of all gauge values). |
| 59 bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only) { |
| 60 grpc::ClientContext context; |
| 61 EmptyMessage message; |
| 62 |
| 63 std::chrono::system_clock::time_point deadline = |
| 64 std::chrono::system_clock::now() + std::chrono::seconds(kDeadlineSecs); |
| 65 |
| 66 context.set_deadline(deadline); |
| 67 |
| 68 std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader( |
| 69 stub->GetAllGauges(&context, message)); |
| 70 |
| 71 GaugeResponse gauge_response; |
| 72 long overall_qps = 0; |
| 73 while (reader->Read(&gauge_response)) { |
| 74 if (gauge_response.value_case() == GaugeResponse::kLongValue) { |
| 75 if (!total_only) { |
| 76 gpr_log(GPR_INFO, "%s: %ld", gauge_response.name().c_str(), |
| 77 gauge_response.long_value()); |
| 78 } |
| 79 overall_qps += gauge_response.long_value(); |
| 80 } else { |
| 81 gpr_log(GPR_INFO, "Gauge %s is not a long value", |
| 82 gauge_response.name().c_str()); |
| 83 } |
| 84 } |
| 85 |
| 86 gpr_log(GPR_INFO, "%ld", overall_qps); |
| 87 |
| 88 const grpc::Status status = reader->Finish(); |
| 89 if (!status.ok()) { |
| 90 gpr_log(GPR_ERROR, "Error in getting metrics from the client"); |
| 91 } |
| 92 |
| 93 return status.ok(); |
| 94 } |
| 95 |
| 96 int main(int argc, char** argv) { |
| 97 grpc::testing::InitTest(&argc, &argv, true); |
| 98 |
| 99 // Make sure server_addresses flag is not empty |
| 100 if (FLAGS_metrics_server_address.empty()) { |
| 101 gpr_log( |
| 102 GPR_ERROR, |
| 103 "Cannot connect to the Metrics server. Please pass the address of the" |
| 104 "metrics server to connect to via the 'metrics_server_address' flag"); |
| 105 return 1; |
| 106 } |
| 107 |
| 108 std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel( |
| 109 FLAGS_metrics_server_address, grpc::InsecureChannelCredentials())); |
| 110 |
| 111 if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only)) { |
| 112 return 1; |
| 113 } |
| 114 |
| 115 return 0; |
| 116 } |
OLD | NEW |