userver: utils::statistics::MetricsStorage Class Reference
Loading...
Searching...
No Matches
utils::statistics::MetricsStorage Class Referencefinal

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

Detailed Description

Storage of metrics registered with MetricTag<Metric>.

Note
The class is thread-safe. See also the note about thread-safety on MetricTag<Metric>.

Definition at line 28 of file metrics_storage.hpp.

Public Member Functions

 MetricsStorage ()
 Creates MetricStorage that considers all MetricTag<Metric> instances.
 MetricsStorage (const std::optional< std::vector< std::string > > &allowed_metric_paths)
 Creates MetricStorage that considers only MetricTag<Metric> instances whose paths are listed in allowed_metric_paths. If allowed_metric_paths equals std::nullopt, then all MetricTag<Metric> instances considered.
 MetricsStorage (const MetricsStorage &)=delete
 MetricsStorage (MetricsStorage &&other) noexcept
MetricsStorage & operator= (const MetricsStorage &)=delete
MetricsStorage & operator= (MetricsStorage &&other) noexcept
template<typename Metric>
Metric & GetMetric (const MetricTag< Metric > &tag)
 Get metric data by type.
void ResetMetrics ()

Constructor & Destructor Documentation

◆ MetricsStorage()

utils::statistics::MetricsStorage::MetricsStorage ( const std::optional< std::vector< std::string > > & allowed_metric_paths)
explicit

Creates MetricStorage that considers only MetricTag<Metric> instances whose paths are listed in allowed_metric_paths. If allowed_metric_paths equals std::nullopt, then all MetricTag<Metric> instances considered.

Parameters
allowed_metric_pathspaths of MetricTag<Metric> instances that should be considered or std::nullopt if all MetricTag<Metric> instances should be considered

Member Function Documentation

◆ GetMetric()

template<typename Metric>
Metric & utils::statistics::MetricsStorage::GetMetric ( const MetricTag< Metric > & tag)
inline

Get metric data by type.

Definition at line 48 of file metrics_storage.hpp.

◆ DumpMetric

void DumpMetric ( Writer & writer,
const MetricsStorage & storage )
friend

Dumps all the metrics of storage through writer, each under its MetricTag path.

Makes MetricsStorage satisfy utils::statistics::HasWriterSupport, so it can be passed directly to utils::statistics::Snapshot.

Note
A MetricsStorage instantiates every MetricTag<Metric> registered in the binary, not only the ones the test cares about. When building a utils::statistics::Snapshot, always pass utils::statistics::SnapshotBuildOptions::require_prefix to filter out unrelated metrics; otherwise the snapshot collects all MetricTag instances of the binary, which slows the test down and makes it depend on unrelated registrations:
UTEST(MetricsStorage, SnapshotRequirePrefix) {
// Fill the metric after creating MetricsStorage and before taking the Snapshot
metrics_storage.GetMetric(kSampleMetric).store(42);
// MetricsStorage instantiates all MetricTag instances registered in the
// binary. require_prefix filters out the unrelated ones.
metrics_storage,
utils::statistics::SnapshotBuildOptions{.require_prefix = "sample-metrics"},
};
// With the default sliced=true the prefix is removed from paths:
EXPECT_EQ(snapshot.SingleMetric("total").AsInt(), 42);
}

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