prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
counter.hpp
Go to the documentation of this file.
1#pragma once
2
6
8
9#include <string_view>
10#include <utility>
11
12namespace prom {
13
18 std::string_view name{};
19 std::string_view help{};
22 comms::DisplayInfo display{};
23};
24
28class Counter : public MetricBase<Counter> {
29public:
31 Counter(const std::string_view name, const std::string_view help)
32 : MetricBase(MetricType::Counter, name, help) {}
33
36 explicit Counter(const CounterSpec& spec)
37 : MetricBase(MetricType::Counter, spec.name, spec.help) {
38 core_->base_labels = spec.labels;
39 core_->const_labels = spec.labels;
40 core_->base_display = spec.display;
41 core_->display = spec.display;
42 if (!spec.unit.empty()) {
43 core_->has_unit = true;
44 core_->unit_from_dimval = spec.unit.from_dimval;
45 core_->unit_name = std::string(spec.unit.name);
46 core_->unit_kind = std::string(spec.unit.kind);
47 core_->unit_symbol = std::string(spec.unit.symbol);
48 }
49 }
50
52 explicit Counter(std::shared_ptr<MetricCore> core) : MetricBase(std::move(core)) {}
53
55 void inc() const noexcept {
56 record(NormalizedValue{1.0, Unit{}});
57 }
58
60 template <class T>
61 requires std::is_arithmetic_v<T>
62 void inc(T amount) noexcept {
63 record(normalize(amount));
64 }
65
68 template <DimensionalValue V>
69 void inc(const V& amount) noexcept {
70 record(normalize(amount));
71 }
72
75 [[nodiscard]] Counter labels(const Labels& dynamic) const noexcept {
76 return make_child(dynamic);
77 }
78
79private:
80 void record(NormalizedValue nv) const noexcept {
81 const auto [adapter, handle] = this->bind();
82 if (!check_finite(nv.value, "inc")) {
83 return;
84 }
85 if (nv.value < 0.0) {
86 if (auto* lg = logger()) {
87 lg->warn("counter '{}' dropping negative increment {}", core_->name, nv.value);
88 }
89 return;
90 }
91 if (!reconcile_unit(nv.unit, *adapter)) {
92 return;
93 }
94 adapter->inc(handle, nv.value);
95 }
96};
97
98} // namespace prom
A counter.
Definition counter.hpp:28
void inc(T amount) noexcept
Increment by a raw arithmetic amount (must be >= 0 and finite).
Definition counter.hpp:62
Counter(std::shared_ptr< MetricCore > core)
Internal: adopt a core prepared by a Registry or labels().
Definition counter.hpp:52
void inc() const noexcept
Increment by one.
Definition counter.hpp:55
Counter(const std::string_view name, const std::string_view help)
Standalone, unbound counter. Binds to the default adapter on first use.
Definition counter.hpp:31
void inc(const V &amount) noexcept
Increment by a dimensional amount; its unit is reconciled with the metric's declared/latched unit.
Definition counter.hpp:69
Counter labels(const Labels &dynamic) const noexcept
A same-type child series bound to dynamic labels (overlaid on the family's constant labels by the bac...
Definition counter.hpp:75
Counter(const CounterSpec &spec)
Standalone counter from a spec (unbound).
Definition counter.hpp:36
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
Counter 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
static spdlog::logger * logger() noexcept
The shared per-process metrics logger.
Definition metric_base.hpp:525
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
Declarative description of a counter, for Registry::counter / Counter::Counter.
Definition counter.hpp:17
std::string_view help
Definition counter.hpp:19
Labels labels
Definition counter.hpp:20
std::string_view name
Definition counter.hpp:18
comms::DisplayInfo display
Definition counter.hpp:22
Unit unit
Definition counter.hpp:21
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