prom 0.1.0
Client-independent C++23 Prometheus/OpenMetrics metric abstraction
Loading...
Searching...
No Matches
stateset.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include <string>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13namespace prom {
14
18 std::string_view name{};
19 std::string_view help{};
21 comms::DisplayInfo display{};
22 std::vector<std::string> states{};
23};
24
27class StateSet : public MetricBase<StateSet> {
28public:
29 StateSet(const std::string_view name, const std::string_view help)
31
32 explicit StateSet(const StateSetSpec& spec)
33 : MetricBase(MetricType::StateSet, spec.name, spec.help) {
34 core_->base_labels = spec.labels;
35 core_->const_labels = spec.labels;
36 core_->base_display = spec.display;
37 core_->display = spec.display;
38 core_->states = spec.states;
39 }
40
41 explicit StateSet(std::shared_ptr<MetricCore> core) : MetricBase(std::move(core)) {}
42
44 void set(const std::string_view state, const bool active) const noexcept {
45 const auto [adapter, handle] = this->bind();
46 adapter->set_state(handle, state, active);
47 }
48
49 [[nodiscard]] StateSet labels(const Labels& dynamic) const noexcept {
50 return make_child(dynamic);
51 }
52};
53
54} // 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
StateSet make_child(const Labels &dynamic) const noexcept
Resolve a labeled child of the same metric type.
Definition metric_base.hpp:458
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
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 state set: each declared state is a boolean, exposed as one series per state with value 0 or 1.
Definition stateset.hpp:27
StateSet(std::shared_ptr< MetricCore > core)
Definition stateset.hpp:41
StateSet(const std::string_view name, const std::string_view help)
Definition stateset.hpp:29
StateSet(const StateSetSpec &spec)
Definition stateset.hpp:32
StateSet labels(const Labels &dynamic) const noexcept
Definition stateset.hpp:49
void set(const std::string_view state, const bool active) const noexcept
Set the boolean value of one state.
Definition stateset.hpp:44
MetricCore (the shared per-series state) and the CRTP MetricBase that gives every metric type value s...
Definition adapter.hpp:24
MetricType
The OpenMetrics / Prometheus metric kinds prom understands.
Definition unit.hpp:15
Declarative description of a state set.
Definition stateset.hpp:17
std::vector< std::string > states
Definition stateset.hpp:22
std::string_view name
Definition stateset.hpp:18
comms::DisplayInfo display
Definition stateset.hpp:21
std::string_view help
Definition stateset.hpp:19
Labels labels
Definition stateset.hpp:20