core_stats.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Copyright 2017 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. *
  17. */
  18. #include "src/cpp/util/core_stats.h"
  19. #include <grpc/support/log.h>
  20. using grpc::core::Bucket;
  21. using grpc::core::Histogram;
  22. using grpc::core::Metric;
  23. using grpc::core::Stats;
  24. namespace grpc {
  25. void CoreStatsToProto(const grpc_stats_data& core, Stats* proto) {
  26. for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
  27. Metric* m = proto->add_metrics();
  28. m->set_name(grpc_stats_counter_name[i]);
  29. m->set_count(core.counters[i]);
  30. }
  31. for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
  32. Metric* m = proto->add_metrics();
  33. m->set_name(grpc_stats_histogram_name[i]);
  34. Histogram* h = m->mutable_histogram();
  35. for (int j = 0; j < grpc_stats_histo_buckets[i]; j++) {
  36. Bucket* b = h->add_buckets();
  37. b->set_start(grpc_stats_histo_bucket_boundaries[i][j]);
  38. b->set_count(core.histograms[grpc_stats_histo_start[i] + j]);
  39. }
  40. }
  41. }
  42. void ProtoToCoreStats(const grpc::core::Stats& proto, grpc_stats_data* core) {
  43. memset(core, 0, sizeof(*core));
  44. for (const auto& m : proto.metrics()) {
  45. switch (m.value_case()) {
  46. case Metric::VALUE_NOT_SET:
  47. break;
  48. case Metric::kCount:
  49. for (int i = 0; i < GRPC_STATS_COUNTER_COUNT; i++) {
  50. if (m.name() == grpc_stats_counter_name[i]) {
  51. core->counters[i] = m.count();
  52. break;
  53. }
  54. }
  55. break;
  56. case Metric::kHistogram:
  57. for (int i = 0; i < GRPC_STATS_HISTOGRAM_COUNT; i++) {
  58. if (m.name() == grpc_stats_histogram_name[i]) {
  59. const auto& h = m.histogram();
  60. bool valid = true;
  61. if (grpc_stats_histo_buckets[i] != h.buckets_size()) valid = false;
  62. for (int j = 0; valid && j < h.buckets_size(); j++) {
  63. if (grpc_stats_histo_bucket_boundaries[i][j] !=
  64. h.buckets(j).start()) {
  65. valid = false;
  66. }
  67. }
  68. if (!valid) {
  69. gpr_log(GPR_ERROR,
  70. "Found histogram %s but shape is different from proto",
  71. m.name().c_str());
  72. }
  73. for (int j = 0; valid && j < h.buckets_size(); j++) {
  74. core->histograms[grpc_stats_histo_start[i] + j] =
  75. h.buckets(j).count();
  76. }
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. }
  83. } // namespace grpc