userver: utils::statistics::MetricTag< Metric > Class Template Reference
Loading...
Searching...
No Matches
utils::statistics::MetricTag< Metric > Class Template Referencefinal

#include <userver/utils/statistics/metric_tag.hpp>

Detailed Description

template<typename Metric>
class utils::statistics::MetricTag< Metric >

Metric description and registration in a declarative way.

Use MetricTag<Metric> for declarative style of metric registration and call MetricStorage::GetMetric for accessing metric data. Please note that metrics can be accessed from multiple coroutines, so Metric must be thread-safe (e.g. std::atomic<T>, rcu::Variable<T>, rcu::RcuMap<T>, concurrent::Variable<T>, etc.).

A custom metric type must be default-constructible and have the following free function defined:

void DumpMetric(utils::statistics::Writer&, const Metric&)

For alerts consider using alerts::Source.

Example usage:

void DumpMetric(utils::statistics::Writer& writer, const Stats& stats) {
writer["sockets"]["opened"] = stats.opened_sockets;
writer["sockets"]["closed"] = stats.closed_sockets;
writer["bytes"]["read"] = stats.bytes_read;
}
void ResetMetric(Stats& stats) {
stats.opened_sockets = 0;
stats.closed_sockets = 0;
stats.bytes_read = 0;
}

where Stats are defined in a following way:

struct Stats {
std::atomic<std::uint64_t> opened_sockets{0};
std::atomic<std::uint64_t> closed_sockets{0};
std::atomic<std::uint64_t> bytes_read{0};
};

For a full usage example see samples/tcp_full_duplex_service/main.cpp

Examples
samples/tcp_full_duplex_service/main.cpp, and samples/testsuite-support/src/metrics.cpp.

Definition at line 40 of file metric_tag.hpp.

Public Member Functions

template<typename... Args>
 MetricTag (std::string path, Args &&... args)
 Register metric, passing a copy of args to the constructor of Metric
 
std::string GetPath () const
 

Constructor & Destructor Documentation

◆ MetricTag()

template<typename Metric >
template<typename... Args>
utils::statistics::MetricTag< Metric >::MetricTag ( std::string  path,
Args &&...  args 
)
inlineexplicit

Register metric, passing a copy of args to the constructor of Metric

Definition at line 44 of file metric_tag.hpp.

Member Function Documentation

◆ GetPath()

template<typename Metric >
std::string utils::statistics::MetricTag< Metric >::GetPath ( ) const
inline

Definition at line 48 of file metric_tag.hpp.

Friends And Related Symbol Documentation

◆ MetricsStorage

template<typename Metric >
friend class MetricsStorage
friend

Definition at line 51 of file metric_tag.hpp.


The documentation for this class was generated from the following file: