13#include <fmt/format.h>
14#include <fmt/ranges.h>
16#include <userver/utils/fmt_compat.hpp>
17#include <userver/utils/overloaded.hpp>
18#include <userver/utils/statistics/labels.hpp>
19#include <userver/utils/statistics/metric_value.hpp>
20#include <userver/utils/statistics/rate.hpp>
23struct fmt::formatter<USERVER_NAMESPACE::utils::statistics::LabelView> {
24 constexpr static auto parse(format_parse_context& ctx) {
return ctx.begin(); }
26 template <
typename FormatContext>
27 auto format(USERVER_NAMESPACE::utils::statistics::LabelView value, FormatContext& ctx)
const {
28 return fmt::format_to(ctx.out(),
"{}={}", value.Name(), value.Value());
33struct fmt::formatter<USERVER_NAMESPACE::utils::statistics::Label>
34 :
public fmt::formatter<USERVER_NAMESPACE::utils::statistics::LabelView> {
35 template <
typename FormatContext>
36 auto format(
const USERVER_NAMESPACE::utils::statistics::Label& value, FormatContext& ctx)
const {
37 return formatter<USERVER_NAMESPACE::utils::statistics::LabelView>::format(
38 USERVER_NAMESPACE::utils::statistics::LabelView{value}, ctx
44struct fmt::formatter<USERVER_NAMESPACE::utils::statistics::LabelsSpan> {
45 constexpr static auto parse(format_parse_context& ctx) {
return ctx.begin(); }
47 template <
typename FormatContext>
48 auto format(USERVER_NAMESPACE::utils::statistics::LabelsSpan value, FormatContext& ctx)
const {
49 return fmt::format_to(ctx.out(),
"{}", fmt::join(value,
";"));
54class fmt::formatter<USERVER_NAMESPACE::utils::statistics::Rate> {
56 constexpr auto parse(format_parse_context& ctx) {
return rate_format_.parse(ctx); }
58 template <
typename FormatCtx>
59 auto format(
const USERVER_NAMESPACE::utils::statistics::Rate& rate, FormatCtx& ctx)
const {
60 return rate_format_.format(rate.value, ctx);
64 fmt::formatter<USERVER_NAMESPACE::utils::statistics::Rate::ValueType> rate_format_;
68struct fmt::formatter<USERVER_NAMESPACE::utils::statistics::HistogramView> {
70 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
72 template <
typename FormatCtx>
73 auto format(USERVER_NAMESPACE::utils::statistics::HistogramView histogram, FormatCtx& ctx)
const {
74 const auto bucket_count = histogram.GetBucketCount();
75 for (std::size_t i = 0; i < bucket_count; ++i) {
76 ctx.advance_to(fmt::format_to(ctx.out(),
"[{}]={}", histogram.GetUpperBoundAt(i), histogram.GetValueAt(i)));
77 ctx.advance_to(fmt::format_to(ctx.out(),
","));
79 ctx.advance_to(fmt::format_to(ctx.out(),
"[inf]={}", histogram.GetValueAtInf()));
85class fmt::formatter<USERVER_NAMESPACE::utils::statistics::MetricValue> {
87 constexpr auto parse(format_parse_context& ctx) {
89 const auto max = [](
auto a,
auto b) {
return a > b ? a : b; };
91 int_format_.parse(ctx),
92 max(float_format_.parse(ctx), max(rate_format_.parse(ctx), histogram_format_.parse(ctx)))
96 template <
typename FormatContext>
97 auto format(USERVER_NAMESPACE::utils::statistics::MetricValue value, FormatContext& ctx)
const {
98 return value.Visit(USERVER_NAMESPACE::utils::Overloaded{
99 [&](std::int64_t x) {
return int_format_.format(x, ctx); },
100 [&](USERVER_NAMESPACE::utils::statistics::Rate x) {
return rate_format_.format(x.value, ctx); },
101 [&](
double x) {
return float_format_.format(x, ctx); },
102 [&](USERVER_NAMESPACE::utils::statistics::HistogramView x) {
return histogram_format_.format(x, ctx); }});
107 fmt::formatter<std::int64_t> int_format_;
108 fmt::formatter<USERVER_NAMESPACE::utils::statistics::Rate::ValueType> rate_format_;
109 fmt::formatter<
double> float_format_;
110 fmt::formatter<USERVER_NAMESPACE::utils::statistics::HistogramView> histogram_format_;