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,
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
78 friend OStream&
79 operator<<(OStream& stream, MetricsFamily const& metricsFamily);
80
86 [[nodiscard]] std::string const&
87 name() const;
88
94 [[nodiscard]] MetricType
95 type() const;
96
97private:
98 std::string name_;
99 std::optional<std::string> description_;
100 std::unordered_map<std::string, std::unique_ptr<MetricBase>> metrics_;
101 MetricType type_;
102 MetricBuilderInterface& metricBuilder_;
103
104 template <typename ValueType>
105 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
107 getMetricImpl(Labels labels, std::vector<ValueType> const& buckets);
108};
109
110} // 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
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
MetricsFamily(std::string name, std::optional< std::string > description, MetricType type, MetricBuilderInterface &metricBuilder= *defaultMetricBuilder)
Construct a new MetricsFamily object.
Definition MetricsFamily.cpp:22
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
The default metric builder.
Definition MetricsFamily.hpp:23
A stream that can optionally compress its data.
Definition OStream.hpp:12
Prometheus metrics: counters, gauges, histograms and their registry and serialization.
Definition Bool.hpp:12
MetricType
The kind and value type of a Prometheus metric.
Definition MetricBase.hpp:72
Interface to create a metric.
Definition MetricBuilder.hpp:15