metrics_client.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <memory>
  19. #include <string>
  20. #include "absl/flags/flag.h"
  21. #include <grpc/support/log.h>
  22. #include <grpcpp/grpcpp.h>
  23. #include "src/proto/grpc/testing/metrics.grpc.pb.h"
  24. #include "src/proto/grpc/testing/metrics.pb.h"
  25. #include "test/cpp/util/metrics_server.h"
  26. #include "test/cpp/util/test_config.h"
  27. int kDeadlineSecs = 10;
  28. ABSL_FLAG(std::string, metrics_server_address, "localhost:8081",
  29. "The metrics server addresses in the fomrat <hostname>:<port>");
  30. // TODO(Capstan): Consider using absl::Duration
  31. ABSL_FLAG(int32_t, deadline_secs, kDeadlineSecs,
  32. "The deadline (in seconds) for RCP call");
  33. ABSL_FLAG(bool, total_only, false,
  34. "If true, this prints only the total value of all gauges");
  35. using grpc::testing::EmptyMessage;
  36. using grpc::testing::GaugeResponse;
  37. using grpc::testing::MetricsService;
  38. // Do not log anything
  39. void BlackholeLogger(gpr_log_func_args* /*args*/) {}
  40. // Prints the values of all Gauges (unless total_only is set to 'true' in which
  41. // case this only prints the sum of all gauge values).
  42. bool PrintMetrics(std::unique_ptr<MetricsService::Stub> stub, bool total_only,
  43. int deadline_secs) {
  44. grpc::ClientContext context;
  45. EmptyMessage message;
  46. std::chrono::system_clock::time_point deadline =
  47. std::chrono::system_clock::now() + std::chrono::seconds(deadline_secs);
  48. context.set_deadline(deadline);
  49. std::unique_ptr<grpc::ClientReader<GaugeResponse>> reader(
  50. stub->GetAllGauges(&context, message));
  51. GaugeResponse gauge_response;
  52. long overall_qps = 0;
  53. while (reader->Read(&gauge_response)) {
  54. if (gauge_response.value_case() == GaugeResponse::kLongValue) {
  55. if (!total_only) {
  56. std::cout << gauge_response.name() << ": "
  57. << gauge_response.long_value() << std::endl;
  58. }
  59. overall_qps += gauge_response.long_value();
  60. } else {
  61. std::cout << "Gauge '" << gauge_response.name() << "' is not long valued"
  62. << std::endl;
  63. }
  64. }
  65. std::cout << overall_qps << std::endl;
  66. const grpc::Status status = reader->Finish();
  67. if (!status.ok()) {
  68. std::cout << "Error in getting metrics from the client" << std::endl;
  69. }
  70. return status.ok();
  71. }
  72. int main(int argc, char** argv) {
  73. grpc::testing::InitTest(&argc, &argv, true);
  74. // The output of metrics client is in some cases programmatically parsed (for
  75. // example by the stress test framework). So, we do not want any of the log
  76. // from the grpc library appearing on stdout.
  77. gpr_set_log_function(BlackholeLogger);
  78. std::shared_ptr<grpc::Channel> channel(
  79. grpc::CreateChannel(absl::GetFlag(FLAGS_metrics_server_address),
  80. grpc::InsecureChannelCredentials()));
  81. if (!PrintMetrics(MetricsService::NewStub(channel),
  82. absl::GetFlag(FLAGS_total_only),
  83. absl::GetFlag(FLAGS_deadline_secs))) {
  84. return 1;
  85. }
  86. return 0;
  87. }