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 #ifndef GRPC_SRC_CORE_LIB_DEBUG_STATS_H
20 #define GRPC_SRC_CORE_LIB_DEBUG_STATS_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stdint.h>
25
26 #include <string>
27 #include <vector>
28
29 #include "absl/strings/string_view.h"
30 #include "absl/types/span.h"
31
32 #include "src/core/lib/debug/histogram_view.h"
33 #include "src/core/lib/debug/stats_data.h"
34 #include "src/core/lib/gprpp/no_destruct.h"
35
36 namespace grpc_core {
37
global_stats()38 inline GlobalStatsCollector& global_stats() {
39 return *NoDestructSingleton<GlobalStatsCollector>::Get();
40 }
41
42 namespace stats_detail {
43 std::string StatsAsJson(absl::Span<const uint64_t> counters,
44 absl::Span<const absl::string_view> counter_name,
45 absl::Span<const HistogramView> histograms,
46 absl::Span<const absl::string_view> histogram_name);
47 }
48
49 template <typename T>
StatsAsJson(T * data)50 std::string StatsAsJson(T* data) {
51 std::vector<HistogramView> histograms;
52 for (int i = 0; i < static_cast<int>(T::Histogram::COUNT); i++) {
53 histograms.push_back(
54 data->histogram(static_cast<typename T::Histogram>(i)));
55 }
56 return stats_detail::StatsAsJson(
57 absl::Span<const uint64_t>(data->counters,
58 static_cast<int>(T::Counter::COUNT)),
59 T::counter_name, histograms, T::histogram_name);
60 }
61
62 } // namespace grpc_core
63
64 #endif // GRPC_SRC_CORE_LIB_DEBUG_STATS_H
65