userver: userver/utils/statistics/metric_tag.hpp Source File
Loading...
Searching...
No Matches
metric_tag.hpp
1#pragma once
2
3#include <string>
4
5#include <userver/utils/statistics/metric_tag_impl.hpp>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace utils::statistics {
10
11/// @brief Metric description
12///
13/// Use `MetricTag<Metric>` for declarative style of metric registration and
14/// call `MetricStorage::GetMetric<Metric>()` for accessing metric data. Please
15/// note that metrics can be accessed from multiple coroutines, so `Metric` must
16/// be thread-safe (e.g. std::atomic<T>, rcu::Variable<T>, rcu::RcuMap<T>,
17/// concurrent::Variable<T>, etc.).
18///
19/// For custom type of `Metric` you have to define method to dump your type to
20/// JSON:
21///
22/// ```
23/// formats::json::ValueBuilder DumpMetric(const Metric& m);
24/// ```
25template <typename Metric>
26class MetricTag final {
27 public:
28 /// Register metric
29 explicit MetricTag(const std::string& path) : key_{typeid(Metric), path} {
30 impl::RegisterMetricInfo(key_, &impl::CreateAnyMetric<Metric>);
31 }
32
33 std::string GetPath() const { return key_.path; }
34
35 private:
36 friend class MetricsStorage;
37
38 const impl::MetricKey key_;
39};
40
41} // namespace utils::statistics
42
43USERVER_NAMESPACE_END