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