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