userver: userver/utils/statistics/metric_tag.hpp Source File
Loading...
Searching...
No Matches
metric_tag.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/metric_tag.hpp
4/// @brief @copybrief utils::statistics::MetricTag
5
6#include <string>
7#include <typeinfo>
8#include <utility>
9
10#include <userver/utils/statistics/metric_tag_impl.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::statistics {
15
16/// @brief Metric description and registration in a declarative way
17///
18/// Use `MetricTag<Metric>` for declarative style of metric registration and
19/// call `MetricStorage::GetMetric` for accessing metric data. Please
20/// note that metrics can be accessed from multiple coroutines, so `Metric` must
21/// be thread-safe (e.g. std::atomic<T>, rcu::Variable<T>, rcu::RcuMap<T>,
22/// concurrent::Variable<T>, etc.).
23///
24/// A custom metric type must be default-constructible and have the following
25/// free function defined:
26/// @code
27/// void DumpMetric(utils::statistics::Writer&, const Metric&)
28/// @endcode
29///
30/// For alerts consider using alerts::Source.
31///
32/// ## Example usage:
33///
34/// @snippet samples/tcp_full_duplex_service/main.cpp TCP sample - Stats tag
35/// where `Stats` are defined in a following way:
36/// @snippet samples/tcp_full_duplex_service/main.cpp TCP sample - Stats definition
37///
38/// For a full usage example see @ref samples/tcp_full_duplex_service/main.cpp
39template <typename Metric>
40class MetricTag final {
41public:
42 /// Register metric, passing a copy of `args` to the constructor of `Metric`
43 template <typename... Args>
44 explicit MetricTag(std::string path, Args&&... args) : key_{typeid(Metric), std::move(path)} {
45 impl::RegisterMetricInfo(key_, impl::MakeMetricFactory<Metric>(std::forward<Args>(args)...));
46 }
47
48 std::string GetPath() const { return key_.path; }
49
50private:
51 friend class MetricsStorage;
52
53 const impl::MetricKey key_;
54};
55
56} // namespace utils::statistics
57
58USERVER_NAMESPACE_END