TARAXA
metrics_group.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <prometheus/gauge.h>
4 #include <prometheus/registry.h>
5 
6 #include <iostream>
7 
8 namespace taraxa::metrics {
9 
13 #define ADD_GAUGE_METRIC(method, name, description) \
14  void method(double v) { \
15  static auto& label = addMetric<prometheus::Gauge>(group_name + "_" + name, description).Add({}); \
16  label.Set(v); \
17  }
18 
24 #define ADD_UPDATER_METHOD(method) \
25  void method##Updater(MetricGetter getter) { \
26  updaters_.push_back([this, getter]() { method(getter()); }); \
27  }
28 
32 #define ADD_GAUGE_METRIC_WITH_UPDATER(method, name, description) \
33  ADD_GAUGE_METRIC(method, name, description) \
34  ADD_UPDATER_METHOD(method)
35 
36 class MetricsGroup {
37  public:
38  using MetricGetter = std::function<double()>;
39  using MetricUpdater = std::function<void()>;
40  MetricsGroup(std::shared_ptr<prometheus::Registry> registry) : registry_(std::move(registry)) {}
41  virtual ~MetricsGroup() = default;
42 
51  template <class Type>
52  prometheus::Family<Type>& addMetric(const std::string& name, const std::string& help) {
53  return prometheus::detail::Builder<Type>().Name(name).Help(help).Register(*registry_);
54  }
58  void updateData() {
59  for (auto& update : updaters_) {
60  update();
61  }
62  }
63 
64  protected:
65  std::shared_ptr<prometheus::Registry> registry_;
66  std::vector<MetricUpdater> updaters_;
67 };
68 
69 using SharedMetricsGroup = std::shared_ptr<MetricsGroup>;
70 } // namespace taraxa::metrics
Definition: metrics_group.hpp:36
std::vector< MetricUpdater > updaters_
Definition: metrics_group.hpp:66
prometheus::Family< Type > & addMetric(const std::string &name, const std::string &help)
template method to add metric family. Family is a metric, but additional labels could be specified fo...
Definition: metrics_group.hpp:52
std::function< double()> MetricGetter
Definition: metrics_group.hpp:38
void updateData()
method that is used to call registered updaters for the specific class
Definition: metrics_group.hpp:58
MetricsGroup(std::shared_ptr< prometheus::Registry > registry)
Definition: metrics_group.hpp:40
std::function< void()> MetricUpdater
Definition: metrics_group.hpp:39
std::shared_ptr< prometheus::Registry > registry_
Definition: metrics_group.hpp:65
virtual ~MetricsGroup()=default
std::hash for asio::adress
Definition: FixedHash.h:483
Definition: node.hpp:24
std::shared_ptr< MetricsGroup > SharedMetricsGroup
Definition: metrics_group.hpp:69