24#include <unordered_map>
41using metric_labels = std::unordered_map<std::string, std::string>;
96 virtual void gauge(std::string_view name,
124 virtual void timing(std::string_view name,
125 std::chrono::nanoseconds duration,
133 return "IMetricCollector";
166 std::string_view name,
171 ,
start_(std::chrono::steady_clock::now()) {}
180 std::chrono::duration_cast<std::chrono::nanoseconds>(
elapsed),
194 [[nodiscard]] std::chrono::nanoseconds
elapsed()
const {
195 return std::chrono::duration_cast<std::chrono::nanoseconds>(
196 std::chrono::steady_clock::now() -
start_);
203 std::chrono::steady_clock::time_point
start_;
238 std::chrono::nanoseconds ,
242 return "null_metric_collector";
274 const std::string& prefix) = 0;
Interface for modules that provide metric collector implementations.
virtual std::shared_ptr< IMetricCollector > create_metric_collector(const std::string &prefix)=0
Create a new metric collector with specific prefix.
virtual ~IMetricCollectorProvider()=default
virtual std::shared_ptr< IMetricCollector > get_metric_collector()=0
Get the default metric collector instance.
Abstract interface for collecting metrics across modules.
virtual void gauge(std::string_view name, double value, const metric_labels &labels={})=0
Set a gauge metric to an absolute value.
virtual ~IMetricCollector()=default
virtual void increment(std::string_view name, double value=1.0, const metric_labels &labels={})=0
Increment a counter metric.
virtual void histogram(std::string_view name, double value, const metric_labels &labels={})=0
Record a histogram observation.
virtual std::string get_implementation_name() const
Get the implementation name for logging/debugging.
virtual void timing(std::string_view name, std::chrono::nanoseconds duration, const metric_labels &labels={})=0
Record a timing measurement.
No-op implementation for when metrics are disabled.
void gauge(std::string_view, double, const metric_labels &={}) override
Set a gauge metric to an absolute value.
void timing(std::string_view, std::chrono::nanoseconds, const metric_labels &={}) override
Record a timing measurement.
void histogram(std::string_view, double, const metric_labels &={}) override
Record a histogram observation.
std::string get_implementation_name() const override
Get the implementation name for logging/debugging.
void increment(std::string_view, double=1.0, const metric_labels &={}) override
Increment a counter metric.
RAII helper for automatic timing measurements.
scoped_timer & operator=(const scoped_timer &)=delete
std::chrono::steady_clock::time_point start_
std::chrono::nanoseconds elapsed() const
Get elapsed time since timer started.
scoped_timer(IMetricCollector &collector, std::string_view name, metric_labels labels={})
Construct a scoped timer.
~scoped_timer()
Destructor reports elapsed time to the collector.
IMetricCollector & collector_
scoped_timer & operator=(scoped_timer &&)=delete
scoped_timer(const scoped_timer &)=delete
scoped_timer(scoped_timer &&)=delete
std::function< std::shared_ptr< IMetricCollector >()> MetricCollectorFactory
Factory function type for creating metric collector instances.
std::unordered_map< std::string, std::string > metric_labels
Metric labels for dimensional data.