10#include <userver/utils/impl/internal_tag.hpp>
11#include <userver/utils/statistics/histogram_view.hpp>
12#include <userver/utils/statistics/labels.hpp>
13#include <userver/utils/statistics/rate.hpp>
15USERVER_NAMESPACE_BEGIN
17namespace utils::statistics {
26template <
class Metric>
27constexpr auto HasDumpMetricWriter()
noexcept
28 ->
decltype(DumpMetric(std::declval<Writer&>(), std::declval<
const Metric&>()), std::true_type{}) {
32template <
class Metric,
class... Args>
33constexpr auto HasDumpMetricWriter(Args...)
noexcept {
34 return std::is_arithmetic_v<Metric>;
98 Writer(Writer&& other) =
delete;
99 Writer(
const Writer&) =
delete;
100 Writer& operator=(Writer&&) =
delete;
101 Writer& operator=(
const Writer&) =
delete;
106 [[nodiscard]] Writer
operator[](std::string_view path) &;
109 [[nodiscard]] Writer
operator[](std::string_view path) &&;
115 if constexpr (std::is_arithmetic_v<T> || std::is_same_v<std::decay_t<T>, Rate> || std::is_same_v<std::decay_t<T>, HistogramView> || std::is_same_v<std::decay_t<T>, MetricValue>) {
120 kHasWriterSupport<T>,
121 "Cast the metric to an arithmetic type or provide a "
122 "`void DumpMetric(utils::statistics::Writer& writer, "
123 "const Metric& value)` function for the `Metric` type"
125 DumpMetric(*
this, value);
133 auto new_writer = MakeChild();
134 new_writer.AppendLabelsSpan(labels);
141 ValueWithLabels(value, LabelsSpan{il});
147 ValueWithLabels(value, LabelsSpan{&label, &label + 1});
153 template <
class Func>
154 void WithLabels(
utils::impl::InternalTag, LabelsSpan labels, Func func) {
155 auto new_writer = MakeChild();
156 new_writer.AppendLabelsSpan(labels);
160 template <
class Func>
161 void WithLabels(
utils::impl::InternalTag, std::initializer_list<LabelView> il, Func func) {
162 WithLabels(utils::impl::InternalTag{}, LabelsSpan{il}, func);
165 template <
class Func>
166 void WithLabels(
utils::impl::InternalTag,
const LabelView& label, Func func) {
167 WithLabels(
utils::impl::InternalTag{}, LabelsSpan{&label, &label + 1}, func);
174 explicit operator bool()
const noexcept {
return !!state_; }
177 explicit Writer(impl::WriterState* state)
noexcept;
178 explicit Writer(impl::WriterState& state, LabelsSpan labels);
182 using ULongLong =
unsigned long long;
184 using PathSizeType = std::uint16_t;
185 using LabelsSizeType = std::uint8_t;
187 void Write(
unsigned long long value);
188 void Write(
long long value);
189 void Write(
double value);
190 void Write(
Rate value);
191 void Write(HistogramView value);
192 void Write(MetricValue value);
194 void Write(
float value) { Write(
static_cast<
double>(value)); }
196 void Write(
unsigned long value) { Write(
static_cast<ULongLong>(value)); }
197 void Write(
long value) { Write(
static_cast<
long long>(value)); }
198 void Write(
unsigned int value) { Write(
static_cast<
long long>(value)); }
199 void Write(
int value) { Write(
static_cast<
long long>(value)); }
200 void Write(
unsigned short value) { Write(
static_cast<
long long>(value)); }
201 void Write(
short value) { Write(
static_cast<
long long>(value)); }
206 Writer(Writer& other, MoveTag)
noexcept;
208 Writer MoveOut()
noexcept {
return Writer{*
this, MoveTag{}}; }
210 void ResetState()
noexcept;
211 void ValidateUsage();
213 void AppendPath(std::string_view path);
214 void AppendLabelsSpan(LabelsSpan labels);
216 impl::WriterState* state_;
217 const PathSizeType initial_path_size_;
218 PathSizeType current_path_size_;
219 const LabelsSizeType initial_labels_size_;
220 LabelsSizeType current_labels_size_;
223template <
class Metric>
224void DumpMetric(Writer& writer,
const std::atomic<Metric>& m) {
225 static_assert(std::atomic<Metric>::is_always_lock_free,
"std::atomic misuse");