Kagome
Polkadot Runtime Engine in C++17
registry_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_METRICS_IMPL_PROMETHEUS_REGISTRY_IMPL_HPP
7 #define KAGOME_CORE_METRICS_IMPL_PROMETHEUS_REGISTRY_IMPL_HPP
8 
9 #include <forward_list>
10 #include <functional>
11 #include <memory>
12 #include <tuple>
13 #include <type_traits>
14 
15 #include <prometheus/counter.h>
16 #include <prometheus/family.h>
17 #include <prometheus/gauge.h>
18 #include <prometheus/histogram.h>
19 #include <prometheus/registry.h>
20 #include <prometheus/summary.h>
22 
23 namespace kagome::metrics {
24  class Handler;
25 
26  namespace {
27  // Helper template structure with specializations
28  // that maps metric interfaces to:
29  // * prometheus countertypes
30  // * implementations
31  // * index in Registry::metrics_ tuple
32  template <typename T>
33  struct MetricInfo {
34  using type = prometheus::Counter;
35  using dtype = PrometheusCounter;
36  static const unsigned index = 0;
37  };
38 
39  template <>
40  struct MetricInfo<Counter> {
41  using type = prometheus::Counter;
42  using dtype = PrometheusCounter;
43  static const unsigned index = 0;
44  };
45 
46  template <>
47  struct MetricInfo<Gauge> {
48  using type = prometheus::Gauge;
49  using dtype = PrometheusGauge;
50  static const unsigned index = 1;
51  };
52 
53  template <>
54  struct MetricInfo<Histogram> {
55  using type = prometheus::Histogram;
56  using dtype = PrometheusHistogram;
57  static const unsigned index = 2;
58  };
59 
60  template <>
61  struct MetricInfo<Summary> {
62  using type = prometheus::Summary;
63  using dtype = PrometheusSummary;
64  static const unsigned index = 3;
65  };
66 
67  } // namespace
68 
69  class PrometheusRegistry : public Registry {
70  friend class PrometheusHandler;
71  // prometheus owns families, returns reference
72  // sort of cache, not to search every time in prometheus vector of families
73  std::unordered_map<std::string,
74  std::reference_wrapper<prometheus::Collectable>>
76  // main storage of metrics
77  std::tuple<std::forward_list<PrometheusCounter>,
78  std::forward_list<PrometheusGauge>,
79  std::forward_list<PrometheusHistogram>,
80  std::forward_list<PrometheusSummary>>
82 
83  // create family for metrics type
84  template <typename T>
85  void registerFamily(const std::string &name,
86  const std::string &help = "",
87  const std::map<std::string, std::string> &labels = {}) {
88  family_.emplace(
89  name,
90  prometheus::detail::Builder<typename MetricInfo<T>::type>()
91  .Name(name)
92  .Help(help)
93  .Labels(labels)
94  .Register(*registry().get()));
95  }
96 
97  template <typename T, typename... Args>
98  T *registerMetric(const std::string &name,
99  const std::map<std::string, std::string> &labels,
100  Args... args) {
101  auto &var =
102  dynamic_cast<prometheus::Family<typename MetricInfo<T>::type> &>(
103  family_.at(name).get())
104  .Add(labels, args...);
105  return &std::get<MetricInfo<T>::index>(metrics_).emplace_front(
106  typename MetricInfo<T>::dtype(var));
107  }
108 
109  static std::shared_ptr<prometheus::Registry> registry() {
110  static auto registry = std::make_shared<prometheus::Registry>();
111  return registry;
112  }
113 
114  public:
115  // Handler has access to internal prometheus registry and gathers metrics,
116  // prepares them for sending by http
117  void setHandler(Handler &handler) override;
118 
119  void registerCounterFamily(
120  const std::string &name,
121  const std::string &help,
122  const std::map<std::string, std::string> &labels) override;
123 
124  void registerGaugeFamily(
125  const std::string &name,
126  const std::string &help,
127  const std::map<std::string, std::string> &labels) override;
128 
129  void registerHistogramFamily(
130  const std::string &name,
131  const std::string &help,
132  const std::map<std::string, std::string> &labels) override;
133 
134  void registerSummaryFamily(
135  const std::string &name,
136  const std::string &help,
137  const std::map<std::string, std::string> &labels) override;
138 
139  Counter *registerCounterMetric(
140  const std::string &name,
141  const std::map<std::string, std::string> &labels) override;
142 
143  Gauge *registerGaugeMetric(
144  const std::string &name,
145  const std::map<std::string, std::string> &labels) override;
146 
147  Histogram *registerHistogramMetric(
148  const std::string &name,
149  const std::vector<double> &bucket_boundaries,
150  const std::map<std::string, std::string> &labels) override;
151 
152  Summary *registerSummaryMetric(
153  const std::string &name,
154  const std::vector<std::pair<double, double>> &quantiles,
155  std::chrono::milliseconds max_age,
156  int age_buckets,
157  const std::map<std::string, std::string> &labels) override;
158 
159  // it is used for test purposes
160  template <typename T>
161  static typename MetricInfo<T>::type *internalMetric(T *metric) {
162  return &dynamic_cast<typename MetricInfo<T>::dtype *>(metric)->m_;
163  }
164  };
165 
166 } // namespace kagome::metrics
167 
168 #endif // KAGOME_CORE_METRICS_IMPL_PROMETHEUS_REGISTRY_IMPL_HPP
static MetricInfo< T >::type * internalMetric(T *metric)
an interface to add request handler for metrics::Exposer implementation generally will contain metric...
Definition: handler.hpp:22
std::tuple< std::forward_list< PrometheusCounter >, std::forward_list< PrometheusGauge >, std::forward_list< PrometheusHistogram >, std::forward_list< PrometheusSummary > > metrics_
static std::shared_ptr< prometheus::Registry > registry()
A histogram metric to represent aggregatable distributions of events.
Definition: metrics.hpp:108
void registerFamily(const std::string &name, const std::string &help="", const std::map< std::string, std::string > &labels={})
kagome::api::Method< Request, AuthorApi > Handler
A summary metric samples observations over a sliding window of time.
Definition: metrics.hpp:91
T * registerMetric(const std::string &name, const std::map< std::string, std::string > &labels, Args...args)
std::unordered_map< std::string, std::reference_wrapper< prometheus::Collectable > > family_
A gauge metric to represent a value that can arbitrarily go up and down.
Definition: metrics.hpp:49
A counter metric to represent a monotonically increasing value.
Definition: metrics.hpp:27
the class stores metrics, provides interface to create metrics and families of metrics TODO(sanblch) ...
Definition: registry.hpp:30