prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
null_adapter.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <prom/adapter.hpp>
12
13#include <logman/logman.hpp>
14
15#include <memory>
16#include <span>
17#include <string_view>
18
19namespace prom {
20
24class NullAdapter final : public Adapter {
25public:
26 NullAdapter() : logger_(logman::get("prom.null")), inert_(std::make_shared<MetricState>()) {}
27
28 [[nodiscard]] std::string_view backend_name() const noexcept override {
29 return "null";
30 }
31
32 [[nodiscard]] MetricHandle register_metric(const MetricMeta& meta) noexcept override {
33 if (logger_) {
34 logger_->debug(
35 "register_metric type={} name={} (no-op)", to_string(meta.type), meta.name);
36 }
37 return inert_;
38 }
39
40 [[nodiscard]] MetricHandle resolve(const MetricHandle& /*family*/,
41 const Labels& dynamic) noexcept override {
42 if (logger_) {
43 logger_->trace("resolve labels={} (no-op)", dynamic.size());
44 }
45 return inert_;
46 }
47
48 void inc(const MetricHandle& /*handle*/, const double amount) noexcept override {
49 trace("inc", amount);
50 }
51
52 void dec(const MetricHandle& /*handle*/, const double amount) noexcept override {
53 trace("dec", amount);
54 }
55
56 void set(const MetricHandle& /*handle*/, const double value) noexcept override {
57 trace("set", value);
58 }
59
60 void observe(const MetricHandle& /*handle*/, const double value) noexcept override {
61 trace("observe", value);
62 }
63
64 void set_info(const MetricHandle& /*handle*/,
65 const std::span<const Label> labels) noexcept override {
66 if (logger_) {
67 logger_->trace("set_info labels={} (no-op)", labels.size());
68 }
69 }
70
71 void set_state(const MetricHandle& /*handle*/,
72 std::string_view state,
73 bool active) noexcept override {
74 if (logger_) {
75 logger_->trace("set_state {}={} (no-op)", state, active);
76 }
77 }
78
79 void set_unit(const MetricHandle& /*handle*/, const Unit& unit) noexcept override {
80 if (logger_) {
81 logger_->trace("set_unit {} (no-op)", unit.name);
82 }
83 }
84
85private:
86 void trace(std::string_view op, double value) const noexcept {
87 if (logger_) {
88 logger_->trace("{} {} (no-op)", op, value);
89 }
90 }
91
92 std::shared_ptr<spdlog::logger> logger_;
93 MetricHandle inert_;
94};
95
96} // namespace prom
The pluggable backend.
Definition adapter.hpp:62
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
A backend that records nothing.
Definition null_adapter.hpp:24
void dec(const MetricHandle &, const double amount) noexcept override
Decrease the series behind handle by amount (gauge).
Definition null_adapter.hpp:52
MetricHandle register_metric(const MetricMeta &meta) noexcept override
Register a metric family.
Definition null_adapter.hpp:32
void set_info(const MetricHandle &, const std::span< const Label > labels) noexcept override
Replace the label set carried by an info metric.
Definition null_adapter.hpp:64
void observe(const MetricHandle &, const double value) noexcept override
Record an observation against handle (histogram/summary).
Definition null_adapter.hpp:60
void set_state(const MetricHandle &, std::string_view state, bool active) noexcept override
Set the boolean value of one member of a state set.
Definition null_adapter.hpp:71
MetricHandle resolve(const MetricHandle &, const Labels &dynamic) noexcept override
Resolve the labeled child of family for the given dynamic labels, creating it on first request.
Definition null_adapter.hpp:40
void inc(const MetricHandle &, const double amount) noexcept override
Increase the series behind handle by amount (counter/gauge).
Definition null_adapter.hpp:48
NullAdapter()
Definition null_adapter.hpp:26
void set_unit(const MetricHandle &, const Unit &unit) noexcept override
Late unit inference hook.
Definition null_adapter.hpp:79
void set(const MetricHandle &, const double value) noexcept override
Set the series behind handle to value (gauge/untyped).
Definition null_adapter.hpp:56
std::string_view backend_name() const noexcept override
Stable identifier of the backend (e.g. "null", "prometheus-cpp").
Definition null_adapter.hpp:28
The backend boundary: MetricMeta, MetricState/MetricHandle, and the pure-virtual Adapter interface.
Definition adapter.hpp:24
constexpr std::string_view to_string(const ErrorCode code) noexcept
Human-readable spelling of an ErrorCode.
Definition error.hpp:36
std::shared_ptr< MetricState > MetricHandle
Opaque, backend-owned handle to a registered metric family or a labeled child series.
Definition fwd.hpp:34
The complete, backend-agnostic description of a metric family handed to Adapter::register_metric.
Definition adapter.hpp:31
OpenMetrics unit suffix plus optional dimensional metadata.
Definition unit.hpp:53