Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
MetricsFamily.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/prometheus/Label.hpp"
23#include "util/prometheus/MetricBase.hpp"
24#include "util/prometheus/MetricBuilder.hpp"
25#include "util/prometheus/OStream.hpp"
26
27#include <cstdint>
28#include <memory>
29#include <optional>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
34namespace util::prometheus {
35
40public:
41 static std::unique_ptr<MetricBuilderInterface> defaultMetricBuilder;
52 std::string name,
53 std::optional<std::string> description,
54 MetricType type,
56 );
57
58 MetricsFamily(MetricsFamily const&) = delete;
59 MetricsFamily(MetricsFamily&&) = default;
61 operator=(MetricsFamily const&) = delete;
63 operator=(MetricsFamily&&) = delete;
64
73 getMetric(Labels labels, std::vector<std::int64_t> const& buckets = {});
74
85 getMetric(Labels labels, std::vector<double> const& buckets);
86
93 friend OStream&
94 operator<<(OStream& stream, MetricsFamily const& metricsFamily);
95
101 std::string const&
102 name() const;
103
109 MetricType
110 type() const;
111
112private:
113 std::string name_;
114 std::optional<std::string> description_;
115 std::unordered_map<std::string, std::unique_ptr<MetricBase>> metrics_;
116 MetricType type_;
117 MetricBuilderInterface& metricBuilder_;
118
119 template <typename ValueType>
120 requires std::same_as<ValueType, std::int64_t> || std::same_as<ValueType, double>
122 getMetricImpl(Labels labels, std::vector<ValueType> const& buckets);
123};
124
125} // namespace util::prometheus
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
Base class for a Prometheus metric containing a name and labels.
Definition MetricBase.hpp:31
Class representing a collection of Prometheus metric with the same name and type.
Definition MetricsFamily.hpp:39
MetricType type() const
Get the description of the metrics.
Definition MetricsFamily.cpp:85
MetricsFamily(std::string name, std::optional< std::string > description, MetricType type, MetricBuilderInterface &builder= *defaultMetricBuilder)
Construct a new MetricsFamily object.
Definition MetricsFamily.cpp:40
std::string const & name() const
Get the name of the metrics.
Definition MetricsFamily.cpp:79
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:51
friend OStream & operator<<(OStream &stream, MetricsFamily const &metricsFamily)
Serialize the metrics to a string in Prometheus format as one block.
Definition MetricsFamily.cpp:64
static std::unique_ptr< MetricBuilderInterface > defaultMetricBuilder
Definition MetricsFamily.hpp:41
A stream that can optionally compress its data.
Definition OStream.hpp:31
Interface to create a metric.
Definition MetricBuilder.hpp:34