prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
summary.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <array>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13namespace prom {
14
16inline constexpr std::array<double, 3> default_summary_quantiles = {0.5, 0.9, 0.99};
17
20 std::string_view name{};
21 std::string_view help{};
24 comms::DisplayInfo display{};
25 std::vector<double> quantiles{};
26};
27
29class Summary : public MetricBase<Summary> {
30public:
31 Summary(const std::string_view name, const std::string_view help)
33 use_default_quantiles();
34 }
35
36 explicit Summary(const SummarySpec& spec)
37 : MetricBase(MetricType::Summary, 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 core_->quantiles = spec.quantiles;
43 if (core_->quantiles.empty()) {
44 use_default_quantiles();
45 }
46 if (!spec.unit.empty()) {
47 core_->has_unit = true;
48 core_->unit_from_dimval = spec.unit.from_dimval;
49 core_->unit_name = std::string(spec.unit.name);
50 core_->unit_kind = std::string(spec.unit.kind);
51 core_->unit_symbol = std::string(spec.unit.symbol);
52 }
53 }
54
55 explicit Summary(std::shared_ptr<MetricCore> core) : MetricBase(std::move(core)) {}
56
58 template <class T>
59 requires std::is_arithmetic_v<T>
60 void observe(T value) noexcept {
61 record(normalize(value));
62 }
63
65 template <DimensionalValue V>
66 void observe(const V& value) noexcept {
67 record(normalize(value));
68 }
69
70 [[nodiscard]] Summary labels(const Labels& dynamic) const noexcept {
71 return make_child(dynamic);
72 }
73
74private:
75 void use_default_quantiles() const {
76 core_->quantiles.assign(default_summary_quantiles.begin(), default_summary_quantiles.end());
77 }
78
79 void record(const NormalizedValue& nv) const noexcept {
80 const auto [adapter, handle] = this->bind();
81 if (!check_finite(nv.value, "observe")) {
82 return;
83 }
84 if (!reconcile_unit(nv.unit, *adapter)) {
85 return;
86 }
87 adapter->observe(handle, nv.value);
88 }
89};
90
91} // 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
Summary 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
A summary. Like a histogram but tracks quantiles instead of fixed buckets.
Definition summary.hpp:29
Summary(const SummarySpec &spec)
Definition summary.hpp:36
Summary(std::shared_ptr< MetricCore > core)
Definition summary.hpp:55
void observe(T value) noexcept
Observe a raw value.
Definition summary.hpp:60
Summary labels(const Labels &dynamic) const noexcept
Definition summary.hpp:70
void observe(const V &value) noexcept
Observe a dimensional value.
Definition summary.hpp:66
Summary(const std::string_view name, const std::string_view help)
Definition summary.hpp:31
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
constexpr std::array< double, 3 > default_summary_quantiles
Default quantiles applied when a summary spec leaves quantiles empty.
Definition summary.hpp:16
Declarative description of a summary.
Definition summary.hpp:19
Unit unit
Definition summary.hpp:23
std::string_view name
Definition summary.hpp:20
std::string_view help
Definition summary.hpp:21
std::vector< double > quantiles
Target quantiles; default set used when empty.
Definition summary.hpp:25
comms::DisplayInfo display
Definition summary.hpp:24
Labels labels
Definition summary.hpp:22
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