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