123 return std::shared_ptr<Registry>(
133 return std::shared_ptr<Registry>(
135 std::make_shared<detail::DecorationState>(std::move(
config.
prefix),
147 [[nodiscard]]
static std::shared_ptr<Registry>
global() {
148 static std::shared_ptr<Registry> instance(
155 return cell_->adapter();
163 cell_->set_adapter(std::move(
adapter));
174 [[nodiscard]] std::string
prefix()
const {
175 return decoration_->prefix();
178 decoration_->set_prefix(std::move(
prefix));
182 return decoration_->const_labels();
185 decoration_->set_const_labels(std::move(labels));
188 decoration_->add_const_label(std::move(
name), std::move(value));
191 [[nodiscard]] comms::DisplayInfo
display()
const {
192 return decoration_->display();
195 decoration_->set_display(std::move(
display));
201 decoration_->prefix(), decoration_->const_labels(), decoration_->display()};
205 decoration_->configure(
211 [[nodiscard]] std::vector<MetricInfo>
metrics()
const {
214 std::vector<std::shared_ptr<MetricCore>> live;
216 const std::scoped_lock lock(mutex_);
217 for (
auto it = metrics_.begin(); it != metrics_.end();) {
218 if (
auto core = it->lock()) {
219 live.push_back(std::move(core));
222 it = metrics_.erase(it);
226 std::vector<MetricInfo> out;
227 out.reserve(live.size());
228 for (
const auto& core : live) {
238 [[nodiscard]]
static std::shared_ptr<Scope>
scope(std::string_view
name);
292 std::vector<double>
buckets = spec.buckets;
296 if (
const auto err = validate_buckets(
buckets)) {
297 return std::unexpected(*err);
311 std::vector<double>
quantiles = spec.quantiles;
315 if (
const auto err = validate_quantiles(
quantiles)) {
316 return std::unexpected(*err);
347 if (spec.states.empty()) {
362 template <
class Metric>
364 const std::string_view
name,
365 const std::string_view help,
368 const comms::DisplayInfo&
display,
371 std::vector<std::string> states)
noexcept {
375 for (
const auto& [label_name, value] : labels.view()) {
381 auto core = std::make_shared<MetricCore>();
383 core->help = std::string(help);
384 core->buckets = std::move(
buckets);
386 core->states = std::move(states);
387 core->source = cell_;
389 core->has_unit =
true;
390 core->unit_from_dimval = unit.from_dimval;
391 core->unit_name = std::string(unit.name);
392 core->unit_kind = std::string(unit.kind);
393 core->unit_symbol = std::string(unit.symbol);
400 core->scope = decoration_;
401 core->base_name = std::string(
name);
402 core->base_labels = labels;
404 const std::uint64_t decoration_version = decoration_->version();
405 core->name = decoration_->full_name(
name);
406 core->const_labels = decoration_->effective_labels(labels);
407 core->display = decoration_->effective_display(
display);
413 const std::uint64_t adapter_version = cell_->version();
416 core->bound.store(std::make_shared<const MetricCore::Bound>(
417 MetricCore::Bound{.adapter =
adapter,
419 .adapter_version = adapter_version,
420 .scope_version = decoration_version}));
422 return Metric{std::move(core)};
426 void track(
const std::shared_ptr<MetricCore>& core)
const noexcept {
427 const std::scoped_lock lock(mutex_);
428 metrics_.push_back(core);
431 template <
class Metric>
432 [[nodiscard]]
static Metric unwrap(expected<Metric> result) {
434 throw Exception(std::move(result.error()));
436 return std::move(*result);
439 [[nodiscard]]
static std::optional<Error>
440 validate_buckets(
const std::vector<double>&
buckets)
noexcept {
444 for (std::size_t i = 0; i <
buckets.size(); ++i) {
445 if (!std::isfinite(
buckets[i])) {
455 [[nodiscard]]
static std::optional<Error>
456 validate_quantiles(
const std::vector<double>&
quantiles)
noexcept {
458 if (!std::isfinite(q) || q <= 0.0 || q >= 1.0) {
466 explicit Registry(std::shared_ptr<AdapterCell> cell) noexcept
467 :
Registry(std::move(cell), std::make_shared<detail::DecorationState>()) {}
471 Registry(std::shared_ptr<AdapterCell> cell,
472 std::shared_ptr<detail::DecorationState> decoration) noexcept
473 : cell_(std::move(cell)), decoration_(std::move(decoration)) {}
475 std::shared_ptr<AdapterCell> cell_;
476 mutable std::mutex mutex_;
477 mutable std::vector<std::weak_ptr<MetricCore>> metrics_;
480 std::shared_ptr<detail::DecorationState> decoration_;
::prometheus::Gauge * gauge
Definition adapter.cpp:61
::prometheus::Histogram::BucketBoundaries buckets
Definition adapter.cpp:65
::prometheus::Summary * summary
Definition adapter.cpp:63
::prometheus::Counter * counter
Definition adapter.cpp:60
std::string name
Metric name (state-set label key).
Definition adapter.cpp:68
::prometheus::Summary::Quantiles quantiles
Definition adapter.cpp:66
prom::MetricType type
Definition adapter.cpp:53
::prometheus::Histogram * histogram
Definition adapter.cpp:62
A counter.
Definition counter.hpp:28
A gauge.
Definition gauge.hpp:24
A histogram.
Definition histogram.hpp:32
An info metric: a single sample whose labels carry the payload (build version, commit,...
Definition info.hpp:25
An immutable-by-convention set of labels, kept sorted by name with duplicates collapsed last-wins.
Definition labels.hpp:55
MetricType type() const noexcept
The metric kind.
Definition metric_base.hpp:385
std::string_view name() const noexcept
The metric's fully-qualified name.
Definition metric_base.hpp:380
Creates registered metrics against a single adapter cell.
Definition registry.hpp:112
RegistryConfig config() const
A snapshot of the whole decoration.
Definition registry.hpp:199
Registry & operator=(Registry &&)=delete
Info info(const InfoSpec &spec)
Definition registry.hpp:258
static std::shared_ptr< Registry > global()
The process-wide registry, sharing the global adapter cell and the global decoration.
Definition registry.hpp:147
Registry(const Registry &)=delete
void set_const_labels(Labels labels)
Definition registry.hpp:184
Untyped untyped(const UntypedSpec &spec)
Definition registry.hpp:255
static std::shared_ptr< Scope > scope(std::string_view name)
Get-or-create the process-wide, named Scope (a per-library metrics instance with a shared prefix / de...
Definition scope.hpp:355
comms::DisplayInfo display() const
Definition registry.hpp:191
void configure(RegistryConfig config)
Replace the whole decoration at once (single reconfiguration).
Definition registry.hpp:204
expected< Gauge > try_gauge(const GaugeSpec &spec) noexcept
Definition registry.hpp:279
expected< Info > try_info(const InfoSpec &spec) noexcept
Definition registry.hpp:341
static std::shared_ptr< Registry > create(AdapterPtr adapter, RegistryConfig config)
Create a decorating registry: every metric built from it gets config's prefix, default constant label...
Definition registry.hpp:131
StateSet stateset(const StateSetSpec &spec)
Definition registry.hpp:261
expected< Summary > try_summary(const SummarySpec &spec) noexcept
Definition registry.hpp:310
Registry(Registry &&)=delete
expected< Histogram > try_histogram(const HistogramSpec &spec) noexcept
Definition registry.hpp:291
void set_prefix(std::string prefix)
Definition registry.hpp:177
expected< StateSet > try_stateset(const StateSetSpec &spec) noexcept
Definition registry.hpp:346
Gauge gauge(const GaugeSpec &spec)
Definition registry.hpp:246
Registry & operator=(const Registry &)=delete
Labels const_labels() const
Definition registry.hpp:181
Summary summary(const SummarySpec &spec)
Definition registry.hpp:252
Counter counter(const CounterSpec &spec)
Definition registry.hpp:243
static std::shared_ptr< Registry > create(AdapterPtr adapter=nullptr)
Create a registry with its own cell holding adapter (a fresh NullAdapter when null).
Definition registry.hpp:122
expected< Counter > try_counter(const CounterSpec &spec) noexcept
Definition registry.hpp:267
AdapterPtr adapter() const
The adapter this registry binds metrics to.
Definition registry.hpp:154
std::vector< MetricInfo > metrics() const
Snapshots describing every (still-alive) metric created from this registry, including declared-but-un...
Definition registry.hpp:211
void set_display(comms::DisplayInfo display)
Definition registry.hpp:194
void set_adapter(AdapterPtr adapter) const
Install adapter (or reset to a fresh NullAdapter when null) as this registry's backend.
Definition registry.hpp:162
expected< Untyped > try_untyped(const UntypedSpec &spec) noexcept
Definition registry.hpp:329
void add_const_label(std::string name, std::string value)
Definition registry.hpp:187
std::string prefix() const
Definition registry.hpp:174
Histogram histogram(const HistogramSpec &spec)
Definition registry.hpp:249
A state set: each declared state is a boolean, exposed as one series per state with value 0 or 1.
Definition stateset.hpp:27
A summary. Like a histogram but tracks quantiles instead of fixed buckets.
Definition summary.hpp:29
An untyped metric: just a settable value.
Definition untyped.hpp:24
Counter — a monotonically increasing cumulative metric, and its designated-initializable CounterSpec.
Error model for prom: ErrorCode, Error, expected, Exception.
Gauge — a value that can move in both directions, and GaugeSpec.
The process-wide adapter cell and its install/swap semantics.
Histogram — bucketed observation distribution, and HistogramSpec.
The backend boundary: MetricMeta, MetricState/MetricHandle, and the pure-virtual Adapter interface.
Info — static key/value metadata exposed as an *_info series.
Label vocabulary: Label, the sorted/deduped Labels set, name validation, and an std::hash<Labels> spe...
MetricCore (the shared per-series state) and the CRTP MetricBase that gives every metric type value s...
std::shared_ptr< DecorationState > global_decoration()
The process-wide decoration shared by Registry::global(), every standalone metric,...
Definition metric_base.hpp:238
Definition adapter.hpp:24
@ EmptyStateSet
A state set was declared with no states.
@ InvalidLabelName
A label name is invalid or uses the reserved __ prefix.
@ InvalidMetricName
Name violates [a-zA-Z_][a-zA-Z0-9_]*.
@ InvalidQuantiles
Summary quantiles fall outside the open interval (0, 1).
@ InvalidBuckets
Histogram buckets are unsorted, empty, or non-finite.
StateSet stateset(const StateSetSpec &spec)
Definition registry.hpp:509
MetricType
The OpenMetrics / Prometheus metric kinds prom understands.
Definition unit.hpp:15
@ Summary
Quantile summary of observations.
@ Info
Static key/value metadata (exposed as *_info).
@ Untyped
A bare value with no semantic constraints.
@ Counter
Monotonically increasing cumulative value.
@ Gauge
Value that can go up and down.
@ StateSet
A set of mutually-related boolean states.
@ Histogram
Bucketed distribution of observations.
constexpr bool is_valid_metric_name(std::string_view name) noexcept
A metric name is valid when it matches Prometheus's [a-zA-Z_][a-zA-Z0-9_]*.
Definition labels.hpp:28
constexpr bool is_valid_label_name(const std::string_view name) noexcept
A label name follows the metric-name charset but additionally rejects the __ prefix,...
Definition labels.hpp:44
constexpr std::array< double, 3 > default_summary_quantiles
Default quantiles applied when a summary spec leaves quantiles empty.
Definition summary.hpp:16
std::expected< T, Error > expected
std::expected<T, Error> — the result type of the Registry::try_* family.
Definition error.hpp:66
expected< StateSet > try_stateset(const StateSetSpec &spec) noexcept
Definition registry.hpp:531
std::shared_ptr< Adapter > AdapterPtr
Shared ownership of an Adapter.
Definition fwd.hpp:38
expected< Histogram > try_histogram(const HistogramSpec &spec) noexcept
Definition registry.hpp:519
MetricInfo describe(const MetricCore &core)
Build a MetricInfo from a core, applying scope decoration for a scoped metric's effective name and co...
Definition registry.hpp:83
expected< Gauge > try_gauge(const GaugeSpec &spec) noexcept
Definition registry.hpp:516
expected< Counter > try_counter(const CounterSpec &spec) noexcept
Definition registry.hpp:513
Info info(const InfoSpec &spec)
Definition registry.hpp:506
expected< Summary > try_summary(const SummarySpec &spec) noexcept
Definition registry.hpp:522
expected< Untyped > try_untyped(const UntypedSpec &spec) noexcept
Definition registry.hpp:525
Untyped untyped(const UntypedSpec &spec)
Definition registry.hpp:503
std::shared_ptr< MetricState > MetricHandle
Opaque, backend-owned handle to a registered metric family or a labeled child series.
Definition fwd.hpp:34
constexpr std::array< double, 11 > default_histogram_buckets
The default bucket bounds applied when a histogram spec leaves buckets empty — the canonical Promethe...
Definition histogram.hpp:17
expected< Info > try_info(const InfoSpec &spec) noexcept
Definition registry.hpp:528
StateSet — a set of mutually-related boolean states, and StateSetSpec.
Declarative description of a counter, for Registry::counter / Counter::Counter.
Definition counter.hpp:17
A validation failure: a machine code plus a human message.
Definition error.hpp:57
Declarative description of a gauge.
Definition gauge.hpp:14
Declarative description of a histogram.
Definition histogram.hpp:21
Declarative description of an info metric.
Definition info.hpp:16
Shared, reference-counted state behind every metric handle.
Definition metric_base.hpp:289
Unit unit_view() const
Build a non-owning view of the currently-known unit.
Definition metric_base.hpp:348
std::string help
Definition metric_base.hpp:294
std::string base_name
Definition metric_base.hpp:327
Labels base_labels
Definition metric_base.hpp:328
Labels const_labels
Definition metric_base.hpp:295
std::string name
Definition metric_base.hpp:293
MetricType type
Definition metric_base.hpp:290
std::shared_ptr< ScopeState > scope
Definition metric_base.hpp:326
A read-only snapshot describing one registered metric, returned by the enumeration APIs (Registry::me...
Definition registry.hpp:72
Labels const_labels
Definition registry.hpp:76
MetricType type
Definition registry.hpp:73
std::string name
Definition registry.hpp:74
bool scoped
Definition registry.hpp:78
std::string help
Definition registry.hpp:75
Unit unit
Definition registry.hpp:77
The decoration a Registry applies to every metric created through it: a name prefix,...
Definition registry.hpp:57
Labels const_labels
Merged into every metric's constant labels; a metric's own labels win on a name collision.
Definition registry.hpp:62
std::string prefix
Prepended to every metric name (e.g. "svc_").
Definition registry.hpp:59
comms::DisplayInfo display
Default display metadata; per-metric display fields override these.
Definition registry.hpp:64
The configuration a Scope applies to every metric created through it.
Definition scope.hpp:44
Declarative description of a state set.
Definition stateset.hpp:17
Declarative description of a summary.
Definition summary.hpp:19
OpenMetrics unit suffix plus optional dimensional metadata.
Definition unit.hpp:53
Declarative description of an untyped metric.
Definition untyped.hpp:14
Summary — streaming quantile summary, and SummarySpec.
Metric kinds (MetricType) and the OpenMetrics Unit descriptor.
Untyped — a bare value with no semantic constraints, and UntypedSpec.