Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
MetricsFamily.hpp
1#pragma once
2
3#include "util/prometheus/Label.hpp"
4#include "util/prometheus/MetricBase.hpp"
5#include "util/prometheus/MetricBuilder.hpp"
6#include "util/prometheus/OStream.hpp"
7
8#include <cstdint>
9#include <memory>
10#include <optional>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15namespace util::prometheus {
16
21public:
22 static std::unique_ptr<MetricBuilderInterface>
24
34 std::string name,
35 std::optional<std::string> description,
36 MetricType type,
38 );
39
40 MetricsFamily(MetricsFamily const&) = delete;
41 MetricsFamily(MetricsFamily&&) = default;
43 operator=(MetricsFamily const&) = delete;
45 operator=(MetricsFamily&&) = delete;
46
56 getMetric(Labels labels, std::vector<std::int64_t> const& buckets = {});
57
69 getMetric(Labels labels, std::vector<double> const& buckets);
70
77 friend OStream&
78 operator<<(OStream& stream, MetricsFamily const& metricsFamily);
79
85 std::string const&
86 name() const;
87
93 MetricType
94 type() const;
95
96private:
97 std::string name_;
98 std::optional<std::string> description_;
99 std::unordered_map<std::string, std::unique_ptr<MetricBase>> metrics_;
100 MetricType type_;
101 MetricBuilderInterface& metricBuilder_;
102
103 template <typename ValueType>
104 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
106 getMetricImpl(Labels labels, std::vector<ValueType> const& buckets);
107};
108
109} // namespace util::prometheus
Class representing a collection of Prometheus labels.
Definition Label.hpp:41
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:12
Class representing a collection of Prometheus metric with the same name and type.
Definition MetricsFamily.hpp:20
MetricType type() const
Get the description of the metrics.
Definition MetricsFamily.cpp:70
MetricsFamily(std::string name, std::optional< std::string > description, MetricType type, MetricBuilderInterface &builder= *defaultMetricBuilder)
Construct a new MetricsFamily object.
Definition MetricsFamily.cpp:22
std::string const & name() const
Get the name of the metrics.
Definition MetricsFamily.cpp:64
MetricBase & getMetric(Labels labels, std::vector< std::int64_t > const &buckets={})
Get the metric with the given labels. If it does not exist, it will be created.
Definition MetricsFamily.cpp:36
friend OStream & operator<<(OStream &stream, MetricsFamily const &metricsFamily)
Serialize the metrics to a string in Prometheus format as one block.
Definition MetricsFamily.cpp:49
static std::unique_ptr< MetricBuilderInterface > defaultMetricBuilder
Definition MetricsFamily.hpp:23
A stream that can optionally compress its data.
Definition OStream.hpp:12
Interface to create a metric.
Definition MetricBuilder.hpp:15