prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
untyped.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
15 std::string_view name{};
16 std::string_view help{};
19 comms::DisplayInfo display{};
20};
21
24class Untyped : public MetricBase<Untyped> {
25public:
26 Untyped(const std::string_view name, const std::string_view help)
27 : MetricBase(MetricType::Untyped, name, help) {}
28
29 explicit Untyped(const UntypedSpec& spec)
30 : MetricBase(MetricType::Untyped, spec.name, spec.help) {
31 core_->base_labels = spec.labels;
32 core_->const_labels = spec.labels;
33 core_->base_display = spec.display;
34 core_->display = spec.display;
35 if (!spec.unit.empty()) {
36 core_->has_unit = true;
37 core_->unit_from_dimval = spec.unit.from_dimval;
38 core_->unit_name = std::string(spec.unit.name);
39 core_->unit_kind = std::string(spec.unit.kind);
40 core_->unit_symbol = std::string(spec.unit.symbol);
41 }
42 }
43
44 explicit Untyped(std::shared_ptr<MetricCore> core) : MetricBase(std::move(core)) {}
45
47 template <class T>
48 requires std::is_arithmetic_v<T>
49 void set(T value) noexcept {
50 record(normalize(value));
51 }
52
54 template <DimensionalValue V>
55 void set(const V& value) noexcept {
56 record(normalize(value));
57 }
58
59 [[nodiscard]] Untyped labels(const Labels& dynamic) const noexcept {
60 return make_child(dynamic);
61 }
62
63private:
64 void record(const NormalizedValue& nv) const noexcept {
65 const auto [adapter, handle] = this->bind();
66 if (!check_finite(nv.value, "set")) {
67 return;
68 }
69 if (!reconcile_unit(nv.unit, *adapter)) {
70 return;
71 }
72 adapter->set(handle, nv.value);
73 }
74};
75
76} // namespace prom
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
Untyped 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
An untyped metric: just a settable value.
Definition untyped.hpp:24
Untyped(const UntypedSpec &spec)
Definition untyped.hpp:29
void set(T value) noexcept
Set to a raw value.
Definition untyped.hpp:49
void set(const V &value) noexcept
Set to a dimensional value.
Definition untyped.hpp:55
Untyped labels(const Labels &dynamic) const noexcept
Definition untyped.hpp:59
Untyped(const std::string_view name, const std::string_view help)
Definition untyped.hpp:26
Untyped(std::shared_ptr< MetricCore > core)
Definition untyped.hpp:44
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
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
Declarative description of an untyped metric.
Definition untyped.hpp:14
Unit unit
Definition untyped.hpp:18
Labels labels
Definition untyped.hpp:17
std::string_view help
Definition untyped.hpp:16
std::string_view name
Definition untyped.hpp:15
comms::DisplayInfo display
Definition untyped.hpp:19