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/impl/metric_tag.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
39///
40/// For introduction to metrics see @ref scripts/docs/en/userver/metrics.md
41template <typename Metric>
42class MetricTag final {
43public:
44 /// Register metric, passing a copy of `args` to the constructor of `Metric`
45 template <typename... Args>
46 explicit MetricTag(std::string path, Args&&... args)
47 : key_{typeid(Metric), std::move(path)}
48 {
49 impl::RegisterMetricInfo(key_, impl::MakeMetricFactory<Metric>(std::forward<Args>(args)...));
50 }
51
52 std::string GetPath() const { return key_.path; }
53
54private:
55 friend class MetricsStorage;
56
57 const impl::MetricKey key_;
58};
59
60} // namespace utils::statistics
61
62USERVER_NAMESPACE_END