8 #include <prometheus/text_serializer.h> 15 const std::vector<std::weak_ptr<prometheus::Collectable>> &collectables) {
16 auto collected_metrics = std::vector<MetricFamily>{};
18 for (
auto &&wcollectable : collectables) {
19 auto collectable = wcollectable.lock();
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()));
30 return collected_metrics;
35 PrometheusHandler::PrometheusHandler()
39 std::shared_ptr<Session> session) {
40 std::vector<MetricFamily> metrics;
47 const TextSerializer serializer;
49 [[maybe_unused]]
auto size =
50 writeResponse(session, request, serializer.Serialize(metrics));
55 const std::string &body) {
57 res.set(boost::beast::http::field::content_type,
58 "text/plain; charset=utf-8");
59 res.content_length(body.size());
62 session->respond(res);
75 const std::weak_ptr<Collectable> &collectable) {
82 const std::weak_ptr<Collectable> &collectable) {
85 auto locked = collectable.lock();
86 auto same_pointer = [&locked](
const std::weak_ptr<Collectable> &candidate) {
87 return locked == candidate.lock();
97 std::vector<std::weak_ptr<Collectable>> &collectables) {
99 std::remove_if(std::begin(collectables),
100 std::end(collectables),
101 [](
const std::weak_ptr<Collectable> &candidate) {
102 return candidate.expired();
104 std::end(collectables));
std::size_t writeResponse(std::shared_ptr< Session > session, Session::Request request, const std::string &body)
boost::beast::http::request< Body > Request
std::mutex collectables_mutex_
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
std::vector< MetricFamily > CollectMetrics(const std::vector< std::weak_ptr< prometheus::Collectable >> &collectables)
Logger createLogger(const std::string &tag)
void registerCollectable(Registry ®istry) 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) ...