Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
metric_collector_interface.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
20#include <chrono>
21#include <memory>
22#include <string>
23#include <string_view>
24#include <unordered_map>
25
26namespace kcenon::common {
27namespace interfaces {
28
41using metric_labels = std::unordered_map<std::string, std::string>;
42
69public:
70 virtual ~IMetricCollector() = default;
71
82 virtual void increment(std::string_view name,
83 double value = 1.0,
84 const metric_labels& labels = {}) = 0;
85
96 virtual void gauge(std::string_view name,
97 double value,
98 const metric_labels& labels = {}) = 0;
99
110 virtual void histogram(std::string_view name,
111 double value,
112 const metric_labels& labels = {}) = 0;
113
124 virtual void timing(std::string_view name,
125 std::chrono::nanoseconds duration,
126 const metric_labels& labels = {}) = 0;
127
132 [[nodiscard]] virtual std::string get_implementation_name() const {
133 return "IMetricCollector";
134 }
135};
136
158public:
166 std::string_view name,
167 metric_labels labels = {})
168 : collector_(collector)
169 , name_(name)
170 , labels_(std::move(labels))
171 , start_(std::chrono::steady_clock::now()) {}
172
177 auto elapsed = std::chrono::steady_clock::now() - start_;
179 name_,
180 std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed),
181 labels_);
182 }
183
184 // Non-copyable and non-movable
185 scoped_timer(const scoped_timer&) = delete;
189
194 [[nodiscard]] std::chrono::nanoseconds elapsed() const {
195 return std::chrono::duration_cast<std::chrono::nanoseconds>(
196 std::chrono::steady_clock::now() - start_);
197 }
198
199private:
201 std::string name_;
203 std::chrono::steady_clock::time_point start_;
204};
205
224public:
225 void increment(std::string_view /*name*/,
226 double /*value*/ = 1.0,
227 const metric_labels& /*labels*/ = {}) override {}
228
229 void gauge(std::string_view /*name*/,
230 double /*value*/,
231 const metric_labels& /*labels*/ = {}) override {}
232
233 void histogram(std::string_view /*name*/,
234 double /*value*/,
235 const metric_labels& /*labels*/ = {}) override {}
236
237 void timing(std::string_view /*name*/,
238 std::chrono::nanoseconds /*duration*/,
239 const metric_labels& /*labels*/ = {}) override {}
240
241 [[nodiscard]] std::string get_implementation_name() const override {
242 return "null_metric_collector";
243 }
244};
245
249using MetricCollectorFactory = std::function<std::shared_ptr<IMetricCollector>()>;
250
259public:
260 virtual ~IMetricCollectorProvider() = default;
261
266 virtual std::shared_ptr<IMetricCollector> get_metric_collector() = 0;
267
273 virtual std::shared_ptr<IMetricCollector> create_metric_collector(
274 const std::string& prefix) = 0;
275};
276
277} // namespace interfaces
278} // namespace kcenon::common
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 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 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.
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.
Definition monitoring.h:38
Core interfaces.
Definition adapter.h:21