TARAXA
Loading...
Searching...
No Matches
metrics_group.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <prometheus/gauge.h>
4#include <prometheus/histogram.h>
5#include <prometheus/registry.h>
6
7namespace taraxa::metrics {
8
12#define ADD_GAUGE_METRIC(method, name, description) \
13 void method(double v) { \
14 static auto& label = addMetric<prometheus::Gauge>(group_name + "_" + name, description).Add({}); \
15 label.Set(v); \
16 }
17
21#define ADD_HISTOGRAM_METRIC(method, name, description, buckets) \
22 void method(double v, std::map<std::string, std::string> labels) { \
23 static auto& label = addMetric<prometheus::Histogram>(group_name + "_" + name, description); \
24 label.Add(labels, prometheus::Histogram::BucketBoundaries{buckets.begin(), buckets.end()}).Observe(v); \
25 }
26
32#define ADD_UPDATER_METHOD(method) \
33 void method##Updater(MetricGetter getter) { \
34 updaters_.push_back([this, getter]() { method(getter()); }); \
35 }
36
40#define ADD_GAUGE_METRIC_WITH_UPDATER(method, name, description) \
41 ADD_GAUGE_METRIC(method, name, description) \
42 ADD_UPDATER_METHOD(method)
43
45 public:
46 using MetricGetter = std::function<double()>;
47 using MetricUpdater = std::function<void()>;
48 MetricsGroup(std::shared_ptr<prometheus::Registry> registry) : registry_(std::move(registry)) {}
49 virtual ~MetricsGroup() = default;
50
59 template <class Type>
60 prometheus::Family<Type>& addMetric(const std::string& name, const std::string& help) {
61 return prometheus::detail::Builder<Type>().Name(name).Help(help).Register(*registry_);
62 }
66 void updateData() {
67 for (auto& update : updaters_) {
68 update();
69 }
70 }
71
72 protected:
73 std::shared_ptr<prometheus::Registry> registry_;
74 std::vector<MetricUpdater> updaters_;
75};
76
77using SharedMetricsGroup = std::shared_ptr<MetricsGroup>;
78} // namespace taraxa::metrics
Definition metrics_group.hpp:44
std::vector< MetricUpdater > updaters_
Definition metrics_group.hpp:74
std::function< double()> MetricGetter
Definition metrics_group.hpp:46
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:60
void updateData()
method that is used to call registered updaters for the specific class
Definition metrics_group.hpp:66
MetricsGroup(std::shared_ptr< prometheus::Registry > registry)
Definition metrics_group.hpp:48
std::function< void()> MetricUpdater
Definition metrics_group.hpp:47
std::shared_ptr< prometheus::Registry > registry_
Definition metrics_group.hpp:73
virtual ~MetricsGroup()=default
std::hash for asio::adress
Definition FixedHash.h:483
Definition app_base.hpp:29
std::shared_ptr< MetricsGroup > SharedMetricsGroup
Definition metrics_group.hpp:77