prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
adapter.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <prom/fwd.hpp>
15#include <prom/labels.hpp>
16#include <prom/unit.hpp>
17
18#include <commons/display_info.hpp>
19
20#include <cstdint>
21#include <span>
22#include <string_view>
23
24namespace prom {
25
31struct MetricMeta {
33 std::string_view name{};
34 std::string_view help{};
37 comms::DisplayInfo display{};
38 std::span<const double> buckets{};
39 std::span<const double> quantiles{};
40 std::span<const std::string> states{};
41};
42
47public:
48 MetricState() = default;
49 MetricState(const MetricState&) = default;
50 MetricState& operator=(const MetricState&) = default;
53 virtual ~MetricState() = default;
54};
55
62class Adapter {
63public:
64 Adapter() = default;
65 Adapter(const Adapter&) = delete;
66 Adapter& operator=(const Adapter&) = delete;
67 Adapter(Adapter&&) = delete;
69 virtual ~Adapter() = default;
70
72 [[nodiscard]] virtual std::string_view backend_name() const noexcept = 0;
73
76 [[nodiscard]] virtual MetricHandle register_metric(const MetricMeta& meta) noexcept = 0;
77
81 [[nodiscard]] virtual MetricHandle resolve(const MetricHandle& family,
82 const Labels& dynamic) noexcept = 0;
83
85 virtual void inc(const MetricHandle& handle, double amount) noexcept = 0;
87 virtual void dec(const MetricHandle& handle, double amount) noexcept = 0;
89 virtual void set(const MetricHandle& handle, double value) noexcept = 0;
91 virtual void observe(const MetricHandle& handle, double value) noexcept = 0;
92
94 virtual void set_info(const MetricHandle& handle, std::span<const Label> labels) noexcept = 0;
96 virtual void
97 set_state(const MetricHandle& handle, std::string_view state, bool active) noexcept = 0;
98
102 virtual void set_unit(const MetricHandle& /*handle*/, const Unit& /*unit*/) noexcept {}
103};
104
115public:
116 AdapterSource() = default;
117 AdapterSource(const AdapterSource&) = delete;
121 virtual ~AdapterSource() = default;
122
124 [[nodiscard]] virtual std::uint64_t version() const noexcept = 0;
125
128 [[nodiscard]] virtual AdapterPtr adapter() const = 0;
129};
130
131} // namespace prom
The source a metric reads its current adapter from, decoupled from Registry so that metric_base can r...
Definition adapter.hpp:114
virtual ~AdapterSource()=default
AdapterSource()=default
virtual AdapterPtr adapter() const =0
The current adapter, never null.
AdapterSource & operator=(const AdapterSource &)=delete
AdapterSource(AdapterSource &&)=delete
virtual std::uint64_t version() const noexcept=0
Monotonic counter, bumped on every adapter swap.
AdapterSource & operator=(AdapterSource &&)=delete
AdapterSource(const AdapterSource &)=delete
The pluggable backend.
Definition adapter.hpp:62
virtual ~Adapter()=default
virtual void set_unit(const MetricHandle &, const Unit &) noexcept
Late unit inference hook.
Definition adapter.hpp:102
Adapter & operator=(const Adapter &)=delete
virtual void observe(const MetricHandle &handle, double value) noexcept=0
Record an observation against handle (histogram/summary).
Adapter()=default
virtual void set_info(const MetricHandle &handle, std::span< const Label > labels) noexcept=0
Replace the label set carried by an info metric.
Adapter & operator=(Adapter &&)=delete
virtual void dec(const MetricHandle &handle, double amount) noexcept=0
Decrease the series behind handle by amount (gauge).
Adapter(const Adapter &)=delete
virtual std::string_view backend_name() const noexcept=0
Stable identifier of the backend (e.g. "null", "prometheus-cpp").
virtual MetricHandle resolve(const MetricHandle &family, const Labels &dynamic) noexcept=0
Resolve the labeled child of family for the given dynamic labels, creating it on first request.
virtual MetricHandle register_metric(const MetricMeta &meta) noexcept=0
Register a metric family.
virtual void set_state(const MetricHandle &handle, std::string_view state, bool active) noexcept=0
Set the boolean value of one member of a state set.
virtual void inc(const MetricHandle &handle, double amount) noexcept=0
Increase the series behind handle by amount (counter/gauge).
virtual void set(const MetricHandle &handle, double value) noexcept=0
Set the series behind handle to value (gauge/untyped).
Adapter(Adapter &&)=delete
An immutable-by-convention set of labels, kept sorted by name with duplicates collapsed last-wins.
Definition labels.hpp:55
Opaque backend state for a registered family or a labeled child.
Definition adapter.hpp:46
MetricState & operator=(MetricState &&)=default
virtual ~MetricState()=default
MetricState()=default
MetricState & operator=(const MetricState &)=default
MetricState(MetricState &&)=default
MetricState(const MetricState &)=default
Forward declarations and shared_ptr aliases for the prom public API.
Label vocabulary: Label, the sorted/deduped Labels set, name validation, and an std::hash<Labels> spe...
Definition adapter.hpp:24
MetricType
The OpenMetrics / Prometheus metric kinds prom understands.
Definition unit.hpp:15
std::shared_ptr< Adapter > AdapterPtr
Shared ownership of an Adapter.
Definition fwd.hpp:38
std::shared_ptr< MetricState > MetricHandle
Opaque, backend-owned handle to a registered metric family or a labeled child series.
Definition fwd.hpp:34
A single name="value" label pair.
Definition labels.hpp:20
The complete, backend-agnostic description of a metric family handed to Adapter::register_metric.
Definition adapter.hpp:31
Unit unit
Declared unit (may be empty).
Definition adapter.hpp:35
std::span< const std::string > states
State-set member names (else empty).
Definition adapter.hpp:40
std::span< const double > quantiles
Summary quantiles (else empty).
Definition adapter.hpp:39
comms::DisplayInfo display
Optional UI metadata.
Definition adapter.hpp:37
std::span< const double > buckets
Histogram bucket bounds (else empty).
Definition adapter.hpp:38
std::string_view name
Fully-qualified metric name.
Definition adapter.hpp:33
MetricType type
Which kind of metric this is.
Definition adapter.hpp:32
std::string_view help
One-line description.
Definition adapter.hpp:34
Labels const_labels
Labels fixed for the whole family.
Definition adapter.hpp:36
OpenMetrics unit suffix plus optional dimensional metadata.
Definition unit.hpp:53
Metric kinds (MetricType) and the OpenMetrics Unit descriptor.