Kagome
Polkadot Runtime Engine in C++17
handler_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <prometheus/text_serializer.h>
9 #include "log/logger.hpp"
10 #include "registry_impl.hpp"
11 
12 using namespace prometheus;
13 
14 std::vector<MetricFamily> CollectMetrics(
15  const std::vector<std::weak_ptr<prometheus::Collectable>> &collectables) {
16  auto collected_metrics = std::vector<MetricFamily>{};
17 
18  for (auto &&wcollectable : collectables) {
19  auto collectable = wcollectable.lock();
20  if (!collectable) {
21  continue;
22  }
23 
24  auto &&metrics = collectable->Collect();
25  collected_metrics.insert(collected_metrics.end(),
26  std::make_move_iterator(metrics.begin()),
27  std::make_move_iterator(metrics.end()));
28  }
29 
30  return collected_metrics;
31 }
32 
33 namespace kagome::metrics {
34 
35  PrometheusHandler::PrometheusHandler()
36  : logger_{log::createLogger("PrometheusHandler", "metrics")} {}
37 
39  std::shared_ptr<Session> session) {
40  std::vector<MetricFamily> metrics;
41 
42  {
43  std::lock_guard<std::mutex> lock{collectables_mutex_};
44  metrics = CollectMetrics(collectables_);
45  }
46 
47  const TextSerializer serializer;
48 
49  [[maybe_unused]] auto size =
50  writeResponse(session, request, serializer.Serialize(metrics));
51  }
52 
53  std::size_t PrometheusHandler::writeResponse(std::shared_ptr<Session> session,
54  Session::Request request,
55  const std::string &body) {
56  Session::Response res{boost::beast::http::status::ok, request.version()};
57  res.set(boost::beast::http::field::content_type,
58  "text/plain; charset=utf-8");
59  res.content_length(body.size());
60  res.keep_alive(true);
61  res.body() = body;
62  session->respond(res);
63  return body.size();
64  }
65 
66  // it is called once on init
68  auto *pregistry = dynamic_cast<PrometheusRegistry *>(&registry);
69  if (pregistry) {
70  registerCollectable(pregistry->registry());
71  }
72  }
73 
75  const std::weak_ptr<Collectable> &collectable) {
76  std::lock_guard<std::mutex> lock{collectables_mutex_};
78  collectables_.push_back(collectable);
79  }
80 
82  const std::weak_ptr<Collectable> &collectable) {
83  std::lock_guard<std::mutex> lock{collectables_mutex_};
84 
85  auto locked = collectable.lock();
86  auto same_pointer = [&locked](const std::weak_ptr<Collectable> &candidate) {
87  return locked == candidate.lock();
88  };
89 
90  collectables_.erase(
91  std::remove_if(
92  std::begin(collectables_), std::end(collectables_), same_pointer),
93  std::end(collectables_));
94  }
95 
97  std::vector<std::weak_ptr<Collectable>> &collectables) {
98  collectables.erase(
99  std::remove_if(std::begin(collectables),
100  std::end(collectables),
101  [](const std::weak_ptr<Collectable> &candidate) {
102  return candidate.expired();
103  }),
104  std::end(collectables));
105  }
106 
107 } // namespace kagome::metrics
std::size_t writeResponse(std::shared_ptr< Session > session, Session::Request request, const std::string &body)
boost::beast::http::request< Body > Request
Definition: session.hpp:22
std::vector< std::weak_ptr< prometheus::Collectable > > collectables_
void removeCollectable(const std::weak_ptr< prometheus::Collectable > &collectable)
void onSessionRequest(Session::Request request, std::shared_ptr< Session > session) override
main interface for session request handling
boost::beast::http::response< Body > Response
Definition: session.hpp:23
std::vector< MetricFamily > CollectMetrics(const std::vector< std::weak_ptr< prometheus::Collectable >> &collectables)
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
void registerCollectable(Registry &registry) override
registers general type metrics registry for metrics collection
static void cleanupStalePointers(std::vector< std::weak_ptr< prometheus::Collectable >> &collectables)
the class stores metrics, provides interface to create metrics and families of metrics TODO(sanblch) ...
Definition: registry.hpp:30