userver: userver/utils/statistics/metric_tag.hpp Source File
Loading...
Searching...
No Matches
metric_tag.hpp
1#pragma once
2
3#include <string>
4#include <typeinfo>
5#include <utility>
6
7#include <userver/utils/statistics/metric_tag_impl.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace utils::statistics {
12
13/// @brief Metric description
14///
15/// Use `MetricTag<Metric>` for declarative style of metric registration and
16/// call `MetricStorage::GetMetric` for accessing metric data. Please
17/// note that metrics can be accessed from multiple coroutines, so `Metric` must
18/// be thread-safe (e.g. std::atomic<T>, rcu::Variable<T>, rcu::RcuMap<T>,
19/// concurrent::Variable<T>, etc.).
20///
21/// A custom metric type must be default-constructible and have the following
22/// free function defined:
23/// @code
24/// void DumpMetric(utils::statistics::Writer&, const Metric&)
25/// @endcode
26template <typename Metric>
27class MetricTag final {
28public:
29 /// Register metric, passing a copy of `args` to the constructor of `Metric`
30 template <typename... Args>
31 explicit MetricTag(const std::string& path, Args&&... args) : key_{typeid(Metric), path} {
32 impl::RegisterMetricInfo(key_, impl::MakeMetricFactory<Metric>(std::forward<Args>(args)...));
33 }
34
35 std::string GetPath() const { return key_.path; }
36
37private:
38 friend class MetricsStorage;
39
40 const impl::MetricKey key_;
41};
42
43} // namespace utils::statistics
44
45USERVER_NAMESPACE_END