10#include <userver/utils/statistics/labels.hpp>
11#include <userver/utils/statistics/rate.hpp>
13USERVER_NAMESPACE_BEGIN
15namespace utils::statistics {
23template <
class Metric>
24constexpr auto HasDumpMetricWriter()
noexcept
25 ->
decltype(DumpMetric(std::declval<Writer&>(),
26 std::declval<
const Metric&>()),
31template <
class Metric,
class... Args>
32constexpr auto HasDumpMetricWriter(Args...)
noexcept {
33 return std::is_arithmetic_v<Metric>;
89 Writer(Writer&& other) =
delete;
90 Writer(
const Writer&) =
delete;
91 Writer& operator=(Writer&&) =
delete;
92 Writer& operator=(
const Writer&) =
delete;
97 [[nodiscard]] Writer
operator[](std::string_view path) &;
100 [[nodiscard]] Writer
operator[](std::string_view path) &&;
106 if constexpr (std::is_arithmetic_v<T> ||
107 std::is_same_v<std::decay_t<T>, Rate>) {
111 static_assert(kHasWriterSupport<T>,
112 "Cast the metric to an arithmetic type or provide a "
113 "`void DumpMetric(utils::statistics::Writer& writer, "
114 "const Metric& value)` function for the `Metric` type");
115 DumpMetric(*
this, value);
123 auto new_writer = MakeChild();
124 new_writer.AppendLabelsSpan(labels);
131 ValueWithLabels(value, LabelsSpan{il});
137 ValueWithLabels(value, LabelsSpan{&label, &label + 1});
143 explicit operator bool()
const noexcept {
return !!state_; }
146 explicit Writer(impl::WriterState* state)
noexcept;
147 explicit Writer(impl::WriterState& state, LabelsSpan labels);
151 using ULongLong =
unsigned long long;
153 using PathSizeType = std::uint16_t;
154 using LabelsSizeType = std::uint8_t;
156 void Write(
unsigned long long value);
157 void Write(
long long value);
158 void Write(
double value);
159 void Write(
Rate value);
161 void Write(
float value) { Write(
static_cast<
double>(value)); }
163 void Write(
unsigned long value) { Write(
static_cast<ULongLong>(value)); }
164 void Write(
long value) { Write(
static_cast<
long long>(value)); }
165 void Write(
unsigned int value) { Write(
static_cast<
long long>(value)); }
166 void Write(
int value) { Write(
static_cast<
long long>(value)); }
167 void Write(
unsigned short value) { Write(
static_cast<
long long>(value)); }
168 void Write(
short value) { Write(
static_cast<
long long>(value)); }
173 Writer(Writer& other, MoveTag)
noexcept;
175 Writer MoveOut()
noexcept {
return Writer{*
this, MoveTag{}}; }
177 void ResetState()
noexcept;
178 void ValidateUsage();
180 void AppendPath(std::string_view path);
181 void AppendLabelsSpan(LabelsSpan labels);
183 impl::WriterState* state_;
184 const PathSizeType initial_path_size_;
185 PathSizeType current_path_size_;
186 const LabelsSizeType initial_labels_size_;
187 LabelsSizeType current_labels_size_;
190template <
class Metric>
191void DumpMetric(Writer& writer,
const std::atomic<Metric>& m) {
192 static_assert(std::atomic<Metric>::is_always_lock_free,
"std::atomic misuse");