8#include <initializer_list>
11#include <unordered_map>
15#include <userver/engine/shared_mutex.hpp>
16#include <userver/formats/json/value_builder.hpp>
17#include <userver/utils/assert.hpp>
18#include <userver/utils/statistics/entry.hpp>
19#include <userver/utils/statistics/metric_value.hpp>
20#include <userver/utils/statistics/writer.hpp>
22USERVER_NAMESPACE_BEGIN
24namespace utils::statistics {
27struct StatisticsRequest
final {};
44 using AddLabels = std::unordered_map<std::string, std::string>;
52 MakeWithPrefix(
const std::string& prefix, AddLabels add_labels = {}, std::vector<Label> require_labels = {});
56 MakeWithPath(
const std::string& path, AddLabels add_labels = {}, std::vector<Label> require_labels = {});
79 std::string prefix_in,
81 std::vector<Label> require_labels_in,
82 AddLabels add_labels_in
86using ExtenderFunc = std::function<
formats::
json::ValueBuilder(
const StatisticsRequest&)>;
88using WriterFunc = std::function<
void(Writer&)>;
92struct MetricsSource
final {
93 std::string prefix_path;
94 std::vector<std::string> path_segments;
95 ExtenderFunc extender;
98 std::vector<Label> writer_labels;
101using StorageData = std::list<MetricsSource>;
102using StorageIterator = StorageData::iterator;
104inline constexpr bool kCheckSubscriptionUB =
utils::impl::kEnableAssert;
110 virtual ~BaseFormatBuilder();
112 virtual void HandleMetric(std::string_view path, LabelsSpan labels,
const MetricValue& value) = 0;
124 Storage(
const Storage&) =
delete;
138 void StopRegisteringExtenders();
143 Entry
RegisterWriter(std::string common_prefix, WriterFunc func, std::vector<Label> add_labels = {});
148 void UnregisterExtender(impl::StorageIterator iterator, impl::UnregisteringKind kind)
noexcept;
151 Entry DoRegisterExtender(impl::MetricsSource&& source);
153 std::atomic<
bool> may_register_extenders_;
154 impl::StorageData metrics_sources_;
155 mutable engine::SharedMutex mutex_;