userver: userver/utils/statistics/metric_tag.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 {
28 public:
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)
32 : key_{typeid(Metric), path} {
33 impl::RegisterMetricInfo(
34 key_, impl::MakeMetricFactory<Metric>(std::forward<Args>(args)...));
35 }
36
37 std::string GetPath() const { return key_.path; }
38
39 private:
40 friend class MetricsStorage;
41
42 const impl::MetricKey key_;
43};
44
45} // namespace utils::statistics
46
47USERVER_NAMESPACE_END