• 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/core/telemetry/stats.h"
20 
21 #include <grpc/support/port_platform.h>
22 #include <stddef.h>
23 
24 #include <algorithm>
25 #include <vector>
26 
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/str_join.h"
29 
30 namespace grpc_core {
31 
32 namespace stats_detail {
33 
34 namespace {
35 template <typename I>
ArrayToJson(absl::Span<const I> values)36 std::string ArrayToJson(absl::Span<const I> values) {
37   std::vector<std::string> parts;
38   for (auto value : values) {
39     parts.push_back(absl::StrCat(value));
40   }
41   return absl::StrCat("[", absl::StrJoin(parts, ","), "]");
42 }
43 }  // namespace
44 
StatsAsJson(absl::Span<const uint64_t> counters,absl::Span<const absl::string_view> counter_name,absl::Span<const HistogramView> histograms,absl::Span<const absl::string_view> histogram_name)45 std::string StatsAsJson(absl::Span<const uint64_t> counters,
46                         absl::Span<const absl::string_view> counter_name,
47                         absl::Span<const HistogramView> histograms,
48                         absl::Span<const absl::string_view> histogram_name) {
49   std::vector<std::string> parts;
50   for (size_t i = 0; i < counters.size(); i++) {
51     parts.push_back(absl::StrCat("\"", counter_name[i], "\": ", counters[i]));
52   }
53   for (size_t i = 0; i < histograms.size(); i++) {
54     parts.push_back(
55         absl::StrCat("\"", histogram_name[i], "\": ",
56                      ArrayToJson(absl::Span<const uint64_t>(
57                          histograms[i].buckets, histograms[i].num_buckets))));
58     parts.push_back(absl::StrCat(
59         "\"", histogram_name[i], "_bkt\": ",
60         ArrayToJson(absl::Span<const int>(histograms[i].bucket_boundaries,
61                                           histograms[i].num_buckets))));
62   }
63   return absl::StrCat("{", absl::StrJoin(parts, ", "), "}");
64 }
65 
66 }  // namespace stats_detail
67 }  // namespace grpc_core
68