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

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

Detailed Description

A snapshot of metrics dumped via Writer.

Definition at line 32 of file testing.hpp.

Public Member Functions

 Snapshot (const HasWriterSupport auto &metric, std::string prefix={}, std::vector< Label > require_labels={})
 Create a snapshot by dumping metric via Writer.
 Snapshot (WriterFuncRef writer, std::string prefix={}, std::vector< Label > require_labels={})
 Create a snapshot by invoking writer.
 Snapshot (const Snapshot &other)=default
 Snapshot (Snapshot &&other) noexcept=default
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
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Constructor & Destructor Documentation

◆ Snapshot() [1/2]

utils::statistics::Snapshot::Snapshot ( const HasWriterSupport auto & metric,
std::string prefix = {},
std::vector< Label > require_labels = {} )
inlineexplicit

Create a snapshot by dumping metric via Writer.

metric is any type with Writer support. For simple metrics prefer a type with DumpMetric and pass it directly:

namespace {
struct SampleMetrics {
std::atomic<int> foo{1};
std::atomic<int> bar{2};
};
void DumpMetric(utils::statistics::Writer& writer, const SampleMetrics& metrics) {
writer["foo"] = metrics.foo;
writer["bar"].ValueWithLabels(metrics.bar, {"kind", "x"});
}
} // namespace
TEST(Snapshot, FromMetric) {
const SampleMetrics metrics;
const utils::statistics::Snapshot snapshot{metrics};
EXPECT_EQ(snapshot.SingleMetric("foo"), std::int64_t{1});
EXPECT_EQ(snapshot.SingleMetric("bar", {{"kind", "x"}}), std::int64_t{2});
EXPECT_EQ(snapshot.SingleMetricOptional("missing"), std::nullopt);
}

utils::statistics::Storage also works — passing Storage is still a common usage when writers are registered there:

namespace {
class ToyMetrics {
public:
ToyMetrics(utils::ResourceScopeStorage& scopes, utils::statistics::Storage& storage) {
storage.RegisterWriter(scopes, "toy", [this](utils::statistics::Writer& writer) {
writer["foo"] = foo;
writer["bar"] = bar;
});
}
std::int64_t foo{1};
std::int64_t bar{2};
};
} // namespace
UTEST(Snapshot, FromStorage) {
const utils::WithResourceScopes<ToyMetrics> toy(std::in_place, storage);
const utils::statistics::Snapshot snapshot{storage};
EXPECT_EQ(snapshot.SingleMetric("toy.foo"), std::int64_t{1});
EXPECT_EQ(snapshot.SingleMetric("toy.bar"), std::int64_t{2});
}

Keeps metrics whose path starts with prefix and labels containing require_labels.

Exceptions
std::exceptionif the Writer dump throws.

Definition at line 47 of file testing.hpp.

◆ Snapshot() [2/2]

utils::statistics::Snapshot::Snapshot ( WriterFuncRef writer,
std::string prefix = {},
std::vector< Label > require_labels = {} )
explicit

Create a snapshot by invoking writer.

prefix and require_labels select metrics the same way as for the metric constructor. The callback writes paths relative to an internal test prefix; prefix is not prepended to what the callback writes.

Member Function Documentation

◆ SingleMetric()

MetricValue utils::statistics::Snapshot::SingleMetric ( std::string path,
std::vector< Label > require_labels = {} ) const

Find a single metric by the given filter.

Parameters
pathThe path of the target metric. prefix specified in the constructor is prepended to the path.
require_labelsLabels that the target metric should have.
Returns
The value of the single found metric.
Exceptions
MetricQueryErrorif none or multiple metrics are found.

◆ PrintTo

void PrintTo ( const Snapshot & data,
std::ostream *  )
friend

Support for gtest diagnostics for utils::statistics::Snapshot.

Warning
Never check the printed value programmatically! The string is not stable and may change, depending on the version of stdlib and userver.

Valid usage:

EXPECT_EQ(..., ...) << testing::PrintToString(snapshot);

Invalid usage:

std::ostringstream stream;
PrintTo(snapshot, &stream);
EXPECT_EQ(stream.str(), ...);

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