diff --git a/src/telemetry/Counter.h b/src/telemetry/Counter.h index 8c0f8aae4d..0165dd2ff5 100644 --- a/src/telemetry/Counter.h +++ b/src/telemetry/Counter.h @@ -31,7 +31,7 @@ public: * Increments the value by @p amount. * @pre `amount >= 0` */ - void Inc(BaseType amount) noexcept { handle->Increment(amount); } + void Inc(BaseType amount) noexcept { handle.Increment(amount); } /** * Increments the value by 1. @@ -43,10 +43,14 @@ public: } BaseType Value() const noexcept { - // Use Collect() here instead of Value() to correctly handle metrics with - // callbacks. - auto metric = handle->Collect(); - return static_cast(metric.counter.value); + if ( has_callback ) { + // Use Collect() here instead of Value() to correctly handle metrics with + // callbacks. + auto metric = handle.Collect(); + return static_cast(metric.counter.value); + } + + return handle.Value(); } bool operator==(const BaseCounter& rhs) const noexcept { return &handle == &rhs.handle; } @@ -57,13 +61,16 @@ public: protected: explicit BaseCounter(FamilyType* family, const prometheus::Labels& labels, prometheus::CollectCallbackPtr callback = nullptr) noexcept - : handle(&(family->Add(labels))), labels(labels) { - if ( callback ) - handle->AddCollectCallback(callback); + : handle(family->Add(labels)), labels(labels) { + if ( callback ) { + handle.AddCollectCallback(callback); + has_callback = true; + } } - Handle* handle; + Handle& handle; prometheus::Labels labels; + bool has_callback = false; }; /** diff --git a/src/telemetry/Gauge.h b/src/telemetry/Gauge.h index dfe2904161..74e79ed051 100644 --- a/src/telemetry/Gauge.h +++ b/src/telemetry/Gauge.h @@ -61,10 +61,14 @@ public: } BaseType Value() const noexcept { - // Use Collect() here instead of Value() to correctly handle metrics - // with callbacks. - auto metric = handle.Collect(); - return static_cast(metric.gauge.value); + if ( has_callback ) { + // Use Collect() here instead of Value() to correctly handle metrics + // with callbacks. + auto metric = handle.Collect(); + return static_cast(metric.gauge.value); + } + + return handle.Value(); } /** @@ -81,12 +85,15 @@ protected: explicit BaseGauge(FamilyType* family, const prometheus::Labels& labels, prometheus::CollectCallbackPtr callback = nullptr) noexcept : handle(family->Add(labels)), labels(labels) { - if ( callback ) + if ( callback ) { handle.AddCollectCallback(callback); + has_callback = true; + } } Handle& handle; prometheus::Labels labels; + bool has_callback = false; }; /**