#include <userver/utils/statistics/by_label_storage.hpp>
Thread-safe monotonic storage of metrics indexed by label values.
See Statistics and Metrics .
Labels must be an aggregate type where all fields are convertible to std::string_view, by at least one of:
explicit) conversion operator to std::string_view;std::string_view ToStringView(const Field&) function.This includes std::string_view itself, utils::Required<std::string_view>, std::string, utils::StrongTypedef, code-generated enums, etc.
Label names are taken from Labels field names.
Items can only be added, never removed.
Define a labels struct with std::string_view fields:
Use utils::Required for fields without adequate defaults to make sure they are always provided.
Declare a utils::statistics::MetricTag for the storage:
Write to the storage via Emplace:
Any dumpable type is supported as Metric. When multiple metrics have the same labels, declare a struct with metrics and create a storage of structs instead of creating multiple storages.
Define the metric struct and its DumpMetric:
Define the labels struct:
Declare a utils::statistics::MetricTag for the storage:
Write to the storage:
MonotonicByLabelStorage is also composable the other way around, it can be included in larger metric structures as a field.
An enum can be used as a label as long as it has ToStringView defined. Usage example:
| Labels | An aggregate type whose fields are convertible to std::string_view. |
| Metric | The metric type. Must support DumpMetric(Writer&, const Metric&). |
Definition at line 247 of file by_label_storage.hpp.
Public Member Functions | |
| MonotonicByLabelStorage ()=default | |
| Create an empty storage. | |
| MonotonicByLabelStorage (MonotonicByLabelStorage &&)=delete | |
| MonotonicByLabelStorage & | operator= (MonotonicByLabelStorage &&)=delete |
| Metric & | operator[] (const Labels &labels) |
| Get or create a default-constructed metric for the given label values. | |
| template<typename... Args> requires std::constructible_from<Metric, Args...> | |
| Metric & | Emplace (const Labels &labels, Args &&... args) |
| Get or create a metric for the given label values. | |
| Metric * | GetIfExists (const Labels &labels) |
| Find a metric by label values without creating it. | |
| void | VisitAll (std::invocable< const Labels &, const Metric & > auto func) const |
| Visit all stored metrics. | |
|
inline |
Get or create a metric for the given label values.
| labels | Label values. |
| args | Arguments forwarded to Metric constructor on first insertion. |
Definition at line 270 of file by_label_storage.hpp.
|
inline |
Find a metric by label values without creating it.
Definition at line 279 of file by_label_storage.hpp.
|
inline |
Get or create a default-constructed metric for the given label values.
| labels | Label values. |
Definition at line 258 of file by_label_storage.hpp.
|
inline |
Visit all stored metrics.
Requires that all fields of Labels are constructible from std::string_view.
| func | Callable accepting (const Labels&, const Metric&). |
Definition at line 293 of file by_label_storage.hpp.
|
friend |
Dump all metrics to a Writer, using label names from Labels fields.
Definition at line 301 of file by_label_storage.hpp.
|
friend |
Reset all metrics (only available if Metric has ADL-found ResetMetric).
Definition at line 320 of file by_label_storage.hpp.