userver: userver/utils/statistics/writer.hpp Source File
Loading...
Searching...
No Matches
writer.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <atomic>
8#include <cstdint>
9#include <initializer_list>
10#include <string_view>
11#include <type_traits>
12
13#include <userver/utils/impl/internal_tag.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace utils::statistics {
21
22class Writer;
23class MetricValue;
24
25namespace impl {
26struct WriterState;
27} // namespace impl
28
33template <class Metric>
34concept HasWriterSupport = std::is_arithmetic_v<Metric> || requires(Writer& writer, const Metric& metric) {
35 DumpMetric(writer, metric);
36};
37
87class Writer final {
88public:
90 static constexpr char kDelimiter = '.';
91
92 Writer() = delete;
93 Writer(Writer&& other) = delete;
94 Writer(const Writer&) = delete;
95 Writer& operator=(Writer&&) = delete;
96 Writer& operator=(const Writer&) = delete;
97
98 ~Writer();
99
104 [[nodiscard]] Writer operator[](std::string_view path) &;
105
107 [[nodiscard]] Writer operator[](std::string_view path) &&;
108
111 template <class T>
112 void operator=(const T& value) {
113 if constexpr (std::is_arithmetic_v<T> || std::is_same_v<std::decay_t<T>, Rate> ||
114 std::is_same_v<std::decay_t<T>, HistogramView> || std::is_same_v<std::decay_t<T>, MetricValue>)
115 {
116 Write(value);
117 } else {
118 if (state_) {
119 static_assert(
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"
124 );
125 DumpMetric(*this, value);
126 }
127 }
128 }
129
133 template <class T>
134 void ValueWithLabels(const T& value, LabelsSpan labels) {
135 auto new_writer = MakeChild();
136 new_writer.AppendLabelsSpan(labels);
137 new_writer = value;
138 }
139
141 template <class T>
142 void ValueWithLabels(const T& value, std::initializer_list<LabelView> il) {
143 ValueWithLabels(value, LabelsSpan{il});
144 }
145
147 template <class T>
148 void ValueWithLabels(const T& value, const LabelView& label) {
149 ValueWithLabels(value, LabelsSpan{&label, &label + 1});
150 }
151
155 template <class Func>
156 void WithLabels(utils::impl::InternalTag, LabelsSpan labels, Func func) {
157 auto new_writer = MakeChild();
158 new_writer.AppendLabelsSpan(labels);
159 func(new_writer);
160 }
161
162 template <class Func>
163 void WithLabels(utils::impl::InternalTag, std::initializer_list<LabelView> il, Func func) {
164 WithLabels(utils::impl::InternalTag{}, LabelsSpan{il}, func);
165 }
166
167 template <class Func>
168 void WithLabels(utils::impl::InternalTag, const LabelView& label, Func func) {
169 WithLabels(utils::impl::InternalTag{}, LabelsSpan{&label, &label + 1}, func);
170 }
172
176 explicit operator bool() const noexcept { return !!state_; }
177
179 explicit Writer(impl::WriterState* state) noexcept;
180 explicit Writer(impl::WriterState& state, LabelsSpan labels);
182
183private:
184 using ULongLong = unsigned long long;
185
186 using PathSizeType = std::uint16_t;
187 using LabelsSizeType = std::uint8_t;
188
189 void Write(unsigned long long value);
190 void Write(long long value);
191 void Write(double value);
192 void Write(Rate value);
193 void Write(HistogramView value);
194 void Write(MetricValue value);
195
196 void Write(float value) { Write(static_cast<double>(value)); }
197
198 void Write(unsigned long value) { Write(static_cast<ULongLong>(value)); }
199 void Write(long value) { Write(static_cast<long long>(value)); }
200 void Write(unsigned int value) { Write(static_cast<long long>(value)); }
201 void Write(int value) { Write(static_cast<long long>(value)); }
202 void Write(unsigned short value) { Write(static_cast<long long>(value)); }
203 void Write(short value) { Write(static_cast<long long>(value)); }
204
205 Writer MakeChild();
206
207 struct MoveTag {};
208 Writer(Writer& other, MoveTag) noexcept;
209
210 Writer MoveOut() noexcept { return Writer{*this, MoveTag{}}; }
211
212 void ResetState() noexcept;
213 void ValidateUsage();
214
215 void AppendPath(std::string_view path);
216 void AppendLabelsSpan(LabelsSpan labels);
217
218 impl::WriterState* state_;
219 const PathSizeType initial_path_size_;
220 PathSizeType current_path_size_;
221 const LabelsSizeType initial_labels_size_;
222 LabelsSizeType current_labels_size_;
223};
224
225template <class Metric>
226void DumpMetric(Writer& writer, const std::atomic<Metric>& m) {
227 static_assert(std::atomic<Metric>::is_always_lock_free, "std::atomic misuse");
228 writer = m.load();
229}
230
231} // namespace utils::statistics
232
233USERVER_NAMESPACE_END