#include <userver/utils/statistics/writer.hpp>
Class for writing metrics that is provided by utils::statistics::Storage.
Usage is quite straightforward:
The above sample would produce the following metrics:
The Writer can be customized for writing custom metric types by providing a void DumpMetric(utils::statistics::Writer& writer, const CustomMetric& value) function:
DumpMetric functions nest well, labels are propagated to the nested DumpMetric:
To use the above writers register the metric writer in utils::statistics::Storage component:
The above metrics in Graphite format would look like:
The Writer is usable by the utils::statistics::MetricTag. For example, for the following structure:
The DumpMetric function may look like:
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". | |
|
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.
|
inline |
Write metric value to metrics builder via using DumpMetric function.
Definition at line 112 of file writer.hpp.
|
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.
|
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.
|
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.
|
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.
|
staticconstexpr |
Path parts delimiter. In other words, writer["a"]["b"] becomes "a.b".
Definition at line 90 of file writer.hpp.