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

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

Detailed Description

Class for writing metrics that is provided by utils::statistics::Storage.

Usage is quite straightforward:

// `storage` is utils::statistics::Storage; `scopes` is utils::ResourceScopeStorage that owns the writer
// lifetime.
storage.RegisterWriter(
scopes,
"path-begin",
[&](utils::statistics::Writer& writer) {
writer["metric1"] = 1;
writer["metric2"].ValueWithLabels(2, {"label_name1", "value1"});
writer["metric3"].ValueWithLabels(3, {{"label_name2", "value2"}, {"label_name3", "value3"}});
if (auto subpath = writer["a"]["b"]) {
subpath["mc4"] = HeavyComputation();
subpath["mc5"] = HeavyComputation2();
}
},
{{"label_for_all", "value_all"}}
);

The above sample would produce the following metrics:

("path-begin.metric1: label_for_all=value_all\tGAUGE\t1\n"
"path-begin.metric2: label_for_all=value_all, label_name1=value1\tGAUGE\t2\n"
"path-begin.metric3: label_for_all=value_all, label_name2=value2, label_name3=value3\tGAUGE\t3\n"
"path-begin.a.b.mc4: label_for_all=value_all\tGAUGE\t4\n"

The Writer can be customized for writing custom metric types by providing a void DumpMetric(utils::statistics::Writer& writer, const CustomMetric& value) function:

struct ComponentMetricsNested {
std::atomic<int> nested1{17};
std::atomic<int> nested2{18};
};
[[maybe_unused]] void DumpMetric(utils::statistics::Writer& writer, const ComponentMetricsNested& m) {
writer["nested1"] = m.nested1;
writer["nested2"].ValueWithLabels(m.nested2, {"label_name", "label_value"});
}

DumpMetric functions nest well, labels are propagated to the nested DumpMetric:

struct ComponentMetrics {
std::atomic<int> total{42};
ComponentMetricsNested summary;
std::unordered_map<std::string, std::shared_ptr<ComponentMetricsNested>> metrics;
};
[[maybe_unused]] void DumpMetric(utils::statistics::Writer& writer, const ComponentMetrics& m) {
writer["total"] = m.total;
writer["summary"] = m.summary;
for (const auto& [key, value] : m.metrics) {
if (!value) {
continue;
}
writer.ValueWithLabels(*value, {{"a_label", key}, {"b_label", "b"}});
}
}

To use the above writers register the metric writer in utils::statistics::Storage component:

const auto metrics = MakeSelfContainedMetricLambda(
storage,
"begin-on-the-metric-path",
std::vector<utils::statistics::Label>{{"label_for_all", "labell value"}},
[&](Writer& writer) {
writer["metric1"] = 42;
const ComponentMetrics& metrics_ref = component_metrics;
writer["more_metrics"] = metrics_ref;
}
);

The above metrics in Graphite format would look like:

begin-on-the-metric-path.metric1;label_for_all=labell_value 42 1674810371
begin-on-the-metric-path.more_metrics.nested1;label_for_all=labell_value;a_label=metric-in-map;b_label=b 17 1674810371
begin-on-the-metric-path.more_metrics.nested2;label_for_all=labell_value;a_label=metric-in-map;b_label=b;label_name=label_value 18 1674810371

The Writer is usable by the utils::statistics::MetricTag. For example, for the following structure:

struct Stats {
utils::statistics::RateCounter opened_sockets;
utils::statistics::RateCounter closed_sockets;
utils::statistics::RateCounter bytes_read;
};

The DumpMetric function may look like:

const utils::statistics::MetricTag<Stats> kTcpEchoTag{"tcp-echo"};
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) {
ResetMetric(stats.opened_sockets);
ResetMetric(stats.closed_sockets);
ResetMetric(stats.bytes_read);
}

A writer and its children share mutable path and label state. Destroy a child before writing through its parent or creating a sibling. Use a nested block to delimit the child's lifetime when necessary. Writers are neither copyable nor movable; store a child in a local variable before passing it as Writer&.

Emit each metric, identified by its path and labels, once per collection. Writing a metric again does not replace its previously exported value. Compute the final value before exporting it.

For introduction to metrics see Statistics and Metrics

For metrics testing in testsuite see Testsuite - Metrics. For metrics testing in unit tests see utils::statistics::Snapshot.

Definition at line 87 of file writer.hpp.

Public Member Functions

 Writer (Writer &&other)=delete
 Writer (const Writer &)=delete
Writer & operator= (Writer &&)=delete
Writer & operator= (const Writer &)=delete
Writer operator[] (std::string_view path) &
Writer operator[] (std::string_view path) &&
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
void operator= (const T &value)
template<class T>
void ValueWithLabels (const T &value, LabelsSpan labels)
template<class T>
void ValueWithLabels (const T &value, std::initializer_list< LabelView > il)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<class T>
void ValueWithLabels (const T &value, const LabelView &label)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 operator bool () const noexcept

Static Public Attributes

static constexpr char kDelimiter = '.'
 Path parts delimiter. In other words, writer["a"]["b"] becomes "a.b".

Member Function Documentation

◆ operator bool()

utils::statistics::Writer::operator bool ( ) const
inlineexplicitnoexcept

Returns true if this writer would actually write data. Returns false if the data is not required by request and metrics construction could be skipped.

Definition at line 176 of file writer.hpp.

◆ operator=()

template<class T>
void utils::statistics::Writer::operator= ( const T & value)
inline

Write metric value to metrics builder via using DumpMetric function.

Definition at line 112 of file writer.hpp.

◆ operator[]()

Writer utils::statistics::Writer::operator[] ( std::string_view path) &
nodiscard

Returns a child writer with path appended, separated by a dot. Dots in path are separators too: writer["a.b"] and writer["a"]["b"] produce the same path. Output formats handle escaping; do not replace dots with underscores manually. The path's contents are copied during the call. See utils::statistics::Writer for child lifetime restrictions.

◆ ValueWithLabels() [1/3]

template<class T>
void utils::statistics::Writer::ValueWithLabels ( const T & value,
const LabelView & label )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 148 of file writer.hpp.

◆ ValueWithLabels() [2/3]

template<class T>
void utils::statistics::Writer::ValueWithLabels ( const T & value,
LabelsSpan labels )
inline

Writes a metric value with additional labels. Labels are inherited by all metrics exported by the value's DumpMetric. Label strings are borrowed and must remain valid until this synchronous call returns, including any nested exports.

Definition at line 134 of file writer.hpp.

◆ ValueWithLabels() [3/3]

template<class T>
void utils::statistics::Writer::ValueWithLabels ( const T & value,
std::initializer_list< LabelView > il )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 142 of file writer.hpp.

Member Data Documentation

◆ kDelimiter

char utils::statistics::Writer::kDelimiter = '.'
staticconstexpr

Path parts delimiter. In other words, writer["a"]["b"] becomes "a.b".

Definition at line 90 of file writer.hpp.


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