prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
gauge.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <string_view>
9#include <utility>
10
11namespace prom {
12
14struct GaugeSpec {
15 std::string_view name{};
16 std::string_view help{};
19 comms::DisplayInfo display{};
20};
21
24class Gauge : public MetricBase<Gauge> {
25public:
26 Gauge(const std::string_view name, const std::string_view help)
27 : MetricBase(MetricType::Gauge, name, help) {}
28
29 explicit Gauge(const GaugeSpec& spec) : MetricBase(MetricType::Gauge, spec.name, spec.help) {
30 core_->base_labels = spec.labels;
31 core_->const_labels = spec.labels;
32 core_->base_display = spec.display;
33 core_->display = spec.display;
34 if (!spec.unit.empty()) {
35 core_->has_unit = true;
36 core_->unit_from_dimval = spec.unit.from_dimval;
37 core_->unit_name = std::string(spec.unit.name);
38 core_->unit_kind = std::string(spec.unit.kind);
39 core_->unit_symbol = std::string(spec.unit.symbol);
40 }
41 }
42
43 explicit Gauge(std::shared_ptr<MetricCore> core) : MetricBase(std::move(core)) {}
44
46 template <class T>
47 requires std::is_arithmetic_v<T>
48 void set(T value) noexcept {
49 apply(&Adapter::set, normalize(value), "set");
50 }
51
53 template <DimensionalValue V>
54 void set(const V& value) noexcept {
55 apply(&Adapter::set, normalize(value), "set");
56 }
57
59 void inc() noexcept {
60 apply(&Adapter::inc, NormalizedValue{1.0, Unit{}}, "inc");
61 }
62
64 template <class T>
65 requires std::is_arithmetic_v<T>
66 void inc(T amount) noexcept {
67 apply(&Adapter::inc, normalize(amount), "inc");
68 }
69
71 template <DimensionalValue V>
72 void inc(const V& amount) noexcept {
73 apply(&Adapter::inc, normalize(amount), "inc");
74 }
75
77 void dec() noexcept {
78 apply(&Adapter::dec, NormalizedValue{1.0, Unit{}}, "dec");
79 }
80
82 template <class T>
83 requires std::is_arithmetic_v<T>
84 void dec(T amount) noexcept {
85 apply(&Adapter::dec, normalize(amount), "dec");
86 }
87
89 template <DimensionalValue V>
90 void dec(const V& amount) noexcept {
91 apply(&Adapter::dec, normalize(amount), "dec");
92 }
93
94 [[nodiscard]] Gauge labels(const Labels& dynamic) const noexcept {
95 return make_child(dynamic);
96 }
97
98private:
99 using Op = void (Adapter::*)(const MetricHandle&, double) noexcept;
100
101 void apply(const Op op, const NormalizedValue& nv, const std::string_view name) const noexcept {
102 const auto [adapter, handle] = this->bind();
103 if (!check_finite(nv.value, name)) {
104 return;
105 }
106 if (!reconcile_unit(nv.unit, *adapter)) {
107 return;
108 }
109 ((*adapter).*op)(handle, nv.value);
110 }
111};
112
113} // namespace prom
The pluggable backend.
Definition adapter.hpp:62
virtual void dec(const MetricHandle &handle, double amount) noexcept=0
Decrease the series behind handle by amount (gauge).
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).
A gauge.
Definition gauge.hpp:24
void inc(T amount) noexcept
Increment by a raw amount.
Definition gauge.hpp:66
void dec(const V &amount) noexcept
Decrement by a dimensional amount.
Definition gauge.hpp:90
Gauge(const std::string_view name, const std::string_view help)
Definition gauge.hpp:26
Gauge(const GaugeSpec &spec)
Definition gauge.hpp:29
void dec(T amount) noexcept
Decrement by a raw amount.
Definition gauge.hpp:84
void inc() noexcept
Increment by one.
Definition gauge.hpp:59
Gauge labels(const Labels &dynamic) const noexcept
Definition gauge.hpp:94
Gauge(std::shared_ptr< MetricCore > core)
Definition gauge.hpp:43
void set(T value) noexcept
Set the gauge to a raw value.
Definition gauge.hpp:48
void inc(const V &amount) noexcept
Increment by a dimensional amount.
Definition gauge.hpp:72
void dec() noexcept
Decrement by one.
Definition gauge.hpp:77
void set(const V &value) noexcept
Set the gauge to a dimensional value.
Definition gauge.hpp:54
An immutable-by-convention set of labels, kept sorted by name with duplicates collapsed last-wins.
Definition labels.hpp:55
CRTP base shared by every metric type.
Definition metric_base.hpp:377
Gauge make_child(const Labels &dynamic) const noexcept
Resolve a labeled child of the same metric type.
Definition metric_base.hpp:458
bool reconcile_unit(const Unit &observed, Adapter &adapter) const noexcept
Reconcile an observed unit against the family's known unit.
Definition metric_base.hpp:475
std::string_view name() const noexcept
The metric's fully-qualified name.
Definition metric_base.hpp:380
const std::shared_ptr< MetricCore > & core() const noexcept
Definition metric_base.hpp:449
bool check_finite(const double value, std::string_view op) const noexcept
Drop-and-log guard for a non-finite sample.
Definition metric_base.hpp:514
std::shared_ptr< MetricCore > core_
Definition metric_base.hpp:530
Binding bind() const noexcept
Resolve the adapter and backend handle this metric should record against.
Definition metric_base.hpp:435
MetricCore (the shared per-series state) and the CRTP MetricBase that gives every metric type value s...
Definition adapter.hpp:24
NormalizedValue normalize(const V &value)
Reduce a dimval value to a NormalizedValue.
Definition dimval.hpp:47
MetricType
The OpenMetrics / Prometheus metric kinds prom understands.
Definition unit.hpp:15
std::shared_ptr< MetricState > MetricHandle
Opaque, backend-owned handle to a registered metric family or a labeled child series.
Definition fwd.hpp:34
Declarative description of a gauge.
Definition gauge.hpp:14
comms::DisplayInfo display
Definition gauge.hpp:19
Unit unit
Definition gauge.hpp:18
std::string_view name
Definition gauge.hpp:15
Labels labels
Definition gauge.hpp:17
std::string_view help
Definition gauge.hpp:16
A plain magnitude paired with the unit it was carrying (empty for raw arithmetic).
Definition dimval.hpp:36
OpenMetrics unit suffix plus optional dimensional metadata.
Definition unit.hpp:53
std::string_view kind
Dimensional compatibility group (e.g. "time").
Definition unit.hpp:55
bool from_dimval
True when inferred from a dimval value.
Definition unit.hpp:57
std::string_view name
Human-readable unit name (e.g. "seconds").
Definition unit.hpp:54
constexpr bool empty() const noexcept
A unit carries no information when it has neither a name nor a kind.
Definition unit.hpp:60
std::string_view symbol
Display symbol (e.g. "s").
Definition unit.hpp:56