#include <userver/utils/statistics/testing.hpp>
A snapshot of metrics dumped via utils::statistics::Writer.
Returned utils::statistics::MetricView and utils::statistics::MetricValue objects refer to snapshot data, not to the vector returned by utils::statistics::Snapshot::AllMetrics.
Definition at line 126 of file testing.hpp.
Public Member Functions | |
| Snapshot (const HasWriterSupport auto &metric, const SnapshotBuildOptions &options={}) | |
| Create a snapshot by dumping metric via utils::statistics::Writer. | |
| Snapshot (WriterFuncRef writer, const SnapshotBuildOptions &options={}) | |
| Create a snapshot by invoking writer. | |
| template<std::convertible_to< std::string > Prefix = std::string> | |
| Snapshot (const HasWriterSupport auto &metric, Prefix &&prefix, std::vector< Label > require_labels={}) | |
| template<std::convertible_to< std::string > Prefix = std::string> | |
| Snapshot (WriterFuncRef writer, Prefix &&prefix, std::vector< Label > require_labels={}) | |
| std::vector< MetricView > | AllMetrics () const |
| Returns all metrics collected in this snapshot. | |
| MetricValue | SingleMetric (std::string path, std::vector< Label > require_labels={}) const |
| Find a single metric by the given filter. | |
| std::optional< MetricValue > | SingleMetricOptional (std::string path, std::vector< Label > require_labels={}) const |
| Find a single metric by the given filter. | |
| bool | EqualsNumerically (const Snapshot &other) const |
| Collected metrics compared with a reference. | |
Copying and moving | |
| |
| Snapshot (const Snapshot &other)=default | |
| Snapshot (Snapshot &&other) noexcept=default | |
| Snapshot & | operator= (const Snapshot &other)=default |
| Snapshot & | operator= (Snapshot &&other) noexcept=default |
Static Public Member Functions | |
| static Snapshot | FromJson (const formats::json::Value &json, const SnapshotLoadOptions &options={}) |
| Load metrics from the JSON object produced by utils::statistics::ToJsonFormat. | |
| static Snapshot | FromLayeredJson (const formats::json::Value &json, const SnapshotLoadOptions &options={}) |
| Load metrics from a layered JSON object. | |
|
inlineexplicit |
Create a snapshot by dumping metric via utils::statistics::Writer.
metric is any type with utils::statistics::Writer support. For simple metrics prefer a type with DumpMetric and pass it directly:
When metrics are split into several subgroups (different paths or label sets), dump metric once and take child snapshots with utils::statistics::SnapshotBuildOptions instead of repeating paths and labels in every check:
Prefer passing the concrete metric (a struct with DumpMetric) or utils::statistics::MetricsStorage. utils::statistics::Storage also satisfies utils::statistics::HasWriterSupport, but is awkward in tests: utils::statistics::Storage::RegisterWriter is usually more boilerplate than passing the metric directly. Use Storage when the code under test is a non-component type that registers writers on Storage internally — then snapshot Storage to exercise that wiring. Example:
If the metric exposes a custom dump routine instead of DumpMetric, use the WriterFuncRef overload below.
Unlike production, a metric with an empty full path is allowed.
| options | Filters and slicing mode; see utils::statistics::SnapshotBuildOptions. By default, the required prefix and labels are removed on collection. |
| utils::statistics::InvalidMetricError | if two metrics have the same path and labels, or one metric lists the same label name more than once. |
| std::invalid_argument | if utils::statistics::SnapshotBuildOptions::require_prefix contains an empty string, or, with slicing enabled, ends with ., or a selected path becomes ambiguous after removing the prefix and .. |
| std::exception | if the utils::statistics::Writer dump throws. |
Definition at line 157 of file testing.hpp.
|
explicit |
Create a snapshot by invoking writer.
Use this overload when the metric under test does not provide DumpMetric and you call a dedicated method (for example WriteStatistics) inside writer.
utils::statistics::SnapshotBuildOptions::require_prefix is not prepended to paths emitted by writer; it filters them according to utils::statistics::SnapshotBuildOptions::sliced. Unlike production, a metric with an empty full path is allowed.
| options | Filters and slicing mode; see utils::statistics::SnapshotBuildOptions. By default, the required prefix and labels are removed on collection. |
| utils::statistics::InvalidMetricError | if two metrics have the same path and labels, or one metric lists the same label name more than once. |
| std::invalid_argument | if utils::statistics::SnapshotBuildOptions::require_prefix contains an empty string, or, with slicing enabled, ends with ., or a selected path becomes ambiguous after removing the prefix and .. |
|
inlineexplicit |
Definition at line 183 of file testing.hpp.
|
inlineexplicit |
Definition at line 195 of file testing.hpp.
| std::vector< MetricView > utils::statistics::Snapshot::AllMetrics | ( | ) | const |
Returns all metrics collected in this snapshot.
Each element is a utils::statistics::MetricView; see statistics_snapshot_view_lifetime.
Returns stored paths and labels: when utils::statistics::SnapshotBuildOptions::sliced was true during collection, utils::statistics::SnapshotBuildOptions::require_prefix and utils::statistics::SnapshotBuildOptions::require_labels have already been removed. Otherwise, full paths and all labels are preserved.
Order: element order is not guaranteed; do not rely on it in tests or production checks.
The returned vector supports building a filtered utils::statistics::Snapshot through utils::statistics::Snapshot(WriterFuncRef), for example:
| bool utils::statistics::Snapshot::EqualsNumerically | ( | const Snapshot & | other | ) | const |
Collected metrics compared with a reference.
Stored paths, labels and values are compared with those of other. Order does not matter. Constructor filters are not retained: utils::statistics::SnapshotBuildOptions::sliced affects equality through the stored paths and labels.
std::int64_t, double and utils::statistics::Rate compare as numbers: 4, 4.0 and Rate{4} are equal. A snapshot loaded from JSON can therefore match metrics collected in a test.
utils::statistics::Snapshot::SingleMetric still returns the stored utils::statistics::MetricValue. Its operator== keeps the type, so an integer metric is not equal to 4.0.
In a gtest, compare a snapshot with a reference via utils::statistics::EqualsNumerically(const Snapshot& expected) rather than this method, so the failure message shows both snapshots.
|
static |
Load metrics from the JSON object produced by utils::statistics::ToJsonFormat.
| options | Prepended to every path and added to every metric. TEST(Snapshot, FromJson) {
"handler.requests": [
{
"type": "GAUGE",
"value": 120,
"labels": {"handler": "/orders", "http_code": "200"}
}
],
"handler.duration_seconds": [
{"type": "GAUGE", "value": 1.5, "labels": {}}
],
"db.queries": [
{"type": "RATE", "value": 3, "labels": {"query": "select_order"}}
],
"handler.timings": [
{
"type": "HIST_RATE",
"labels": {},
"value": {"bounds": [5.0, 10.0], "buckets": [2, 3], "inf": 1, "sum": 100.0}
}
]
})"));
EXPECT_EQ(
std::int64_t{120}
);
EXPECT_EQ(snapshot.SingleMetric("handler.duration_seconds"), 1.5);
EXPECT_EQ(snapshot.SingleMetric("db.queries", {{"query", "select_order"}}), utils::statistics::Rate{3});
EXPECT_EQ(timings.GetValueAt(0), 2);
EXPECT_EQ(timings.GetValueAt(1), 3);
EXPECT_EQ(timings.GetValueAtInf(), 1);
EXPECT_EQ(timings.GetTotalSum(), 100.0);
}
|
| utils::statistics::InvalidMetricError | if two metrics have the same path and labels, or one metric lists the same label name more than once. |
| formats::json::ExceptionWithPath | if json does not match the format above. |
|
static |
Load metrics from a layered JSON object.
| utils::statistics::InvalidMetricError | if two metrics have the same path and labels, or one metric lists the same label name more than once. |
| formats::json::ExceptionWithPath | if json does not match the format above. |
| MetricValue utils::statistics::Snapshot::SingleMetric | ( | std::string | path, |
| std::vector< Label > | require_labels = {} ) const |
Find a single metric by the given filter.
path and require_labels refer to stored metrics, as returned by utils::statistics::Snapshot::AllMetrics. When utils::statistics::SnapshotBuildOptions::sliced is true, do not repeat the prefix or labels removed during collection. When it is false, use full paths and original labels. The constructor's filter is never reapplied to queries.
| path | The path of the target metric. |
| require_labels | Labels that the target metric should have; additional labels are allowed. |
| utils::statistics::MetricQueryError | if none or multiple metrics are found. TEST(Snapshot, SlicedAndUnslicedQueries) {
out["api.requests"].ValueWithLabels(std::int64_t{2}, {{"service", "orders"}, {"code", "200"}});
};
sliced{writer, {.require_prefix = "api", .require_labels = {{"service", "orders"}}}};
unsliced{writer, {.require_prefix = "api", .require_labels = {{"service", "orders"}}, .sliced = false}};
EXPECT_EQ(unsliced.SingleMetric("api.requests", {{"service", "orders"}, {"code", "200"}}), std::int64_t{2});
}
|
| std::optional< MetricValue > utils::statistics::Snapshot::SingleMetricOptional | ( | std::string | path, |
| std::vector< Label > | require_labels = {} ) const |
Find a single metric by the given filter.
| path | The path of the target metric. |
| require_labels | Labels that the target metric should have; additional labels are allowed. |
| utils::statistics::MetricQueryError | when more than one metric matches. |
Writes snapshot metrics through writer.
Writes the stored paths and labels of snapshot as is. If utils::statistics::SnapshotBuildOptions::sliced was true during collection, emits shortened paths and remaining labels; otherwise, emits full paths and all labels. Removed prefixes and labels are not restored. Makes Snapshot satisfy utils::statistics::HasWriterSupport.
|
friend |
Support for gtest diagnostics for utils::statistics::Snapshot.
Valid usage:
Invalid usage: