14#include <boost/container_hash/hash.hpp>
15#include <boost/pfr/core.hpp>
16#include <boost/pfr/core_name.hpp>
17#include <boost/pfr/tuple_size.hpp>
19#include <userver/concurrent/impl/monotonic_concurrent_set.hpp>
20#include <userver/utils/assert.hpp>
21#include <userver/utils/fixed_array.hpp>
22#include <userver/utils/statistics/labels.hpp>
23#include <userver/utils/statistics/writer.hpp>
25USERVER_NAMESPACE_BEGIN
31template <
typename Metric>
34template <
typename Metric>
35concept ResettableMetric =
requires(Metric& m) { ResetMetric(m); };
37template <
typename Field>
38std::string_view FieldToStringView(
const Field& field) {
39 if constexpr (std::constructible_from<std::string_view,
const Field&>) {
40 return std::string_view{field};
43 return ToStringView(field);
47template <
typename Field>
48concept FieldConvertibleToStringView =
49 std::constructible_from<std::string_view,
const Field&> ||
requires(
const Field& f) {
52 } -> std::same_as<std::string_view>;
55template <
typename Field>
56concept FieldConstructibleFromStringView = std::constructible_from<Field, std::string_view>;
58template <
typename Labels>
59concept AllFieldsConvertibleToStringView = []<std::size_t... Is>(std::index_sequence<Is...>) {
60 return (FieldConvertibleToStringView<boost::pfr::tuple_element_t<Is, Labels>> && ...);
61}(std::make_index_sequence<boost::pfr::tuple_size_v<Labels>>{});
63template <
typename Labels>
64concept AllFieldsConstructibleFromStringView = []<std::size_t... Is>(std::index_sequence<Is...>) {
65 return (FieldConstructibleFromStringView<boost::pfr::tuple_element_t<Is, Labels>> && ...);
66}(std::make_index_sequence<boost::pfr::tuple_size_v<Labels>>{});
68template <
typename Labels>
69concept LabelsAggregate = std::is_aggregate_v<Labels> && AllFieldsConvertibleToStringView<Labels>;
71template <
typename Labels>
72auto LabelsStructToViewArray(
const Labels& labels) {
73 constexpr std::size_t kN = boost::pfr::tuple_size_v<Labels>;
74 std::array<std::string_view, kN> result{};
75 boost::pfr::for_each_field(labels, [&result](
const auto& field, std::size_t i) {
76 result[i] = FieldToStringView(field);
81template <
typename Labels>
82constexpr auto GetLabelNames()
noexcept {
83 return boost::pfr::names_as_array<Labels>();
86template <
typename Labels, std::size_t... Is>
87Labels LabelsArrayToStruct(
const utils::FixedArray<std::string>& arr, std::index_sequence<Is...>) {
88 UASSERT(arr.size() ==
sizeof...(Is));
89 return Labels{std::string_view{arr[Is]}...};
92template <
typename Labels>
93Labels LabelsArrayToStruct(
const utils::FixedArray<std::string>& arr) {
94 return LabelsArrayToStruct<Labels>(arr, std::make_index_sequence<boost::pfr::tuple_size_v<Labels>>{});
98struct FixedStringArrayHash {
99 using is_transparent [[maybe_unused]] =
void;
101 template <
typename StringViewRange>
102 std::size_t operator()(
const StringViewRange& arr)
const noexcept {
103 std::size_t hash = arr.size();
104 for (
const auto& s : arr) {
105 boost::hash_combine(hash, std::hash<std::string_view>{}(s));
112struct FixedStringArrayEqual {
113 using is_transparent [[maybe_unused]] =
void;
115 template <
typename StringViewRange1,
typename StringViewRange2>
116 bool operator()(
const StringViewRange1& a,
const StringViewRange2& b)
const noexcept {
118 for (std::size_t i = 0; i < a.size(); ++i) {
119 if (std::string_view{a[i]} != std::string_view{b[i]}) {
128template <
typename Metric>
130 utils::FixedArray<std::string> labels;
133 template <std::size_t N,
typename... Args>
134 explicit ByLabelEntry(
const std::array<std::string_view, N>& views, Args&&... args)
135 : labels(views.begin(), views.end()),
136 metric(std::forward<Args>(args)...)
141template <
typename Metric>
142struct ByLabelEntryHash {
143 using is_transparent [[maybe_unused]] =
void;
145 std::size_t operator()(
const ByLabelEntry<Metric>& entry)
const noexcept {
146 return FixedStringArrayHash{}(entry.labels);
149 template <std::size_t N>
150 std::size_t operator()(
const std::array<std::string_view, N>& key)
const noexcept {
151 return FixedStringArrayHash{}(key);
156template <
typename Metric>
157struct ByLabelEntryEqual {
158 using is_transparent [[maybe_unused]] =
void;
160 bool operator()(
const ByLabelEntry<Metric>& a,
const ByLabelEntry<Metric>& b)
const noexcept {
161 return FixedStringArrayEqual{}(a.labels, b.labels);
164 template <std::size_t N>
165 bool operator()(
const ByLabelEntry<Metric>& a,
const std::array<std::string_view, N>& b)
const noexcept {
166 return FixedStringArrayEqual{}(a.labels, b);
169 template <std::size_t N>
170 bool operator()(
const std::array<std::string_view, N>& a,
const ByLabelEntry<Metric>& b)
const noexcept {
171 return FixedStringArrayEqual{}(a, b.labels);
246template <
typename Labels,
typename Metric>
247requires impl::LabelsAggregate<Labels>
260 requires std::default_initializable<Metric>
262 return Emplace(labels);
269 template <
typename... Args>
270 requires std::constructible_from<Metric, Args...>
271 Metric&
Emplace(
const Labels& labels, Args&&... args) {
272 const auto view_array = impl::LabelsStructToViewArray(labels);
273 auto [entry, inserted] = set_.TryEmplace(view_array, view_array, std::forward<Args>(args)...);
281 const auto view_array = impl::LabelsStructToViewArray(labels);
282 auto ref = set_.Find(view_array);
303 requires impl::DumpableMetric<Metric>
305 static constexpr auto kLabelNames = impl::GetLabelNames<Labels>();
306 static constexpr std::size_t kFieldCount = boost::pfr::tuple_size_v<Labels>;
308 std::vector<LabelView> label_views;
309 label_views.reserve(kFieldCount);
311 storage.set_.Visit([&writer, &label_views](
const Entry& entry) {
313 for (std::size_t i = 0; i < kFieldCount; ++i) {
314 label_views.emplace_back(kLabelNames[i], entry.labels[i]);
316 writer.ValueWithLabels(entry.metric, label_views);
322 requires impl::ResettableMetric<Metric>
324 storage.set_.Visit([](Entry& entry) { ResetMetric(entry.metric); });
328 using Entry = impl::ByLabelEntry<Metric>;
329 using Set =
concurrent::impl::MonotonicConcurrentSet<
331 impl::ByLabelEntryHash<Metric>,
332 impl::ByLabelEntryEqual<Metric>>;