TARAXA
metrics_service.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <prometheus/exposer.h>
4 #include <prometheus/gauge.h>
5 #include <prometheus/registry.h>
6 
7 #include <thread>
8 
10 
11 namespace taraxa::metrics {
12 
17  public:
18  MetricsService(const std::string& host, uint16_t port, uint16_t polling_interval_ms);
20 
24  void start();
25 
32  template <class T>
33  std::shared_ptr<T> getMetrics() {
34  if (!exposer_) {
35  return nullptr;
36  }
37  auto metrics = metrics_.find(T::group_name);
38  if (metrics == metrics_.end()) {
39  auto emplace_result = metrics_.emplace(T::group_name, std::make_shared<T>(registry_)).first;
40  metrics = emplace_result;
41  }
42  return dynamic_pointer_cast<T>(metrics->second);
43  }
44 
45  private:
46  const uint16_t kPollingIntervalMs = 0;
47  std::unique_ptr<prometheus::Exposer> exposer_;
48  std::shared_ptr<prometheus::Registry> registry_;
49  std::map<std::string, SharedMetricsGroup> metrics_;
50  std::unique_ptr<std::thread> thread_;
51 };
52 } // namespace taraxa::metrics
class for metrics collecting. Registering specific metrics classes and creating prometheus server(exp...
Definition: metrics_service.hpp:16
const uint16_t kPollingIntervalMs
Definition: metrics_service.hpp:46
~MetricsService()
Definition: metrics_service.cpp:18
std::unique_ptr< prometheus::Exposer > exposer_
Definition: metrics_service.hpp:47
void start()
method to start thread that collecting data from special classes
Definition: metrics_service.cpp:23
std::unique_ptr< std::thread > thread_
Definition: metrics_service.hpp:50
std::shared_ptr< T > getMetrics()
method to get specific metrics instance. Creates and registers instance if it is called for first tim...
Definition: metrics_service.hpp:33
std::shared_ptr< prometheus::Registry > registry_
Definition: metrics_service.hpp:48
std::map< std::string, SharedMetricsGroup > metrics_
Definition: metrics_service.hpp:49
MetricsService(const std::string &host, uint16_t port, uint16_t polling_interval_ms)
Definition: metrics_service.cpp:11
Definition: node.hpp:24