1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 *is % allowed in string 17 */ 18 19 #include <memory> 20 #include <string> 21 22 #include <gflags/gflags.h> 23 #include <grpc/support/log.h> 24 #include <grpcpp/grpcpp.h> 25 26 #include "src/proto/grpc/testing/metrics.grpc.pb.h" 27 #include "src/proto/grpc/testing/metrics.pb.h" 28 #include "test/cpp/util/metrics_server.h" 29 #include "test/cpp/util/test_config.h" 30 31 int kDeadlineSecs = 10; 32 33 DEFINE_string(metrics_server_address, "localhost:8081", 34 "The metrics server addresses in the fomrat <hostname>:<port>"); 35 DEFINE_int32(deadline_secs, kDeadlineSecs, 36 "The deadline (in seconds) for RCP call"); 37 DEFINE_bool(total_only, false, 38 "If true, this prints only the total value of all gauges"); 39 40 using grpc::testing::EmptyMessage; 41 using grpc::testing::GaugeResponse; 42 using grpc::testing::MetricsService; 43 using grpc::testing::MetricsServiceImpl; 44 45 // Do not log anything 46 void BlackholeLogger(gpr_log_func_args* args) {} 47 48 // Prints the values of all Gauges (unless total_only is set to 'true' in which 49 // case this only prints the sum of all gauge values). 50 bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only, 51 int deadline_secs) { 52 grpc::ClientContext context; 53 EmptyMessage message; 54 55 std::chrono::system_clock::time_point deadline = 56 std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs); 57 58 context.set_deadline(deadline); 59 60 std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader( 61 stub->GetAllGauges(&context, message)); 62 63 GaugeResponse gauge_response; 64 long overall_qps = 0; 65 while (reader->Read(&gauge_response)) { 66 if (gauge_response.value_case() == GaugeResponse::kLongValue) { 67 if (!total_only) { 68 std::cout << gauge_response.name() << ": " 69 << gauge_response.long_value() << std::endl; 70 } 71 overall_qps += gauge_response.long_value(); 72 } else { 73 std::cout << "Gauge '" << gauge_response.name() << "' is not long valued" 74 << std::endl; 75 } 76 } 77 78 std::cout << overall_qps << std::endl; 79 80 const grpc::Status status = reader->Finish(); 81 if (!status.ok()) { 82 std::cout << "Error in getting metrics from the client" << std::endl; 83 } 84 85 return status.ok(); 86 } 87 88 int main(int argc, char** argv) { 89 grpc::testing::InitTest(&argc, &argv, true); 90 91 // The output of metrics client is in some cases programatically parsed (for 92 // example by the stress test framework). So, we do not want any of the log 93 // from the grpc library appearing on stdout. 94 gpr_set_log_function(BlackholeLogger); 95 96 std::shared_ptr<grpc::Channel> channel(grpc::CreateChannel( 97 FLAGS_metrics_server_address, grpc::InsecureChannelCredentials())); 98 99 if (!PrintMetrics(MetricsService::NewStub(channel), FLAGS_total_only, 100 FLAGS_deadline_secs)) { 101 return 1; 102 } 103 104 return 0; 105 } 106