]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | #ifdef ENABLE_METRICS_PREVIEW | |
6 | ||
7 | # include <map> | |
8 | # include <memory> | |
9 | # include <sstream> | |
10 | # include <stdexcept> | |
11 | # include <vector> | |
12 | ||
13 | # include "opentelemetry/_metrics/sync_instruments.h" | |
14 | # include "opentelemetry/common/macros.h" | |
15 | # include "opentelemetry/sdk/_metrics/aggregator/counter_aggregator.h" | |
16 | # include "opentelemetry/sdk/_metrics/aggregator/min_max_sum_count_aggregator.h" | |
17 | # include "opentelemetry/sdk/_metrics/instrument.h" | |
18 | ||
19 | OPENTELEMETRY_BEGIN_NAMESPACE | |
20 | namespace sdk | |
21 | { | |
22 | namespace metrics | |
23 | { | |
24 | ||
25 | # if defined(_MSC_VER) | |
26 | # pragma warning(push) | |
27 | # pragma warning(disable : 4250) // inheriting methods via dominance | |
28 | # endif | |
29 | ||
30 | template <class T> | |
31 | class BoundCounter final : public BoundSynchronousInstrument<T>, | |
32 | public opentelemetry::metrics::BoundCounter<T> | |
33 | { | |
34 | ||
35 | public: | |
36 | BoundCounter() = default; | |
37 | ||
38 | BoundCounter(nostd::string_view name, | |
39 | nostd::string_view description, | |
40 | nostd::string_view unit, | |
41 | bool enabled) | |
42 | : BoundSynchronousInstrument<T>( | |
43 | name, | |
44 | description, | |
45 | unit, | |
46 | enabled, | |
47 | opentelemetry::metrics::InstrumentKind::Counter, | |
48 | std::shared_ptr<Aggregator<T>>(new CounterAggregator<T>( | |
49 | opentelemetry::metrics::InstrumentKind::Counter))) // Aggregator is chosen here | |
50 | {} | |
51 | ||
52 | /* | |
53 | * Add adds the value to the counter's sum. The labels are already linked to the instrument | |
54 | * and are not specified. | |
55 | * | |
56 | * @param value the numerical representation of the metric being captured | |
57 | * @param labels the set of labels, as key-value pairs | |
58 | */ | |
59 | virtual void add(T value) override | |
60 | { | |
61 | if (value < 0) | |
62 | { | |
63 | # if __EXCEPTIONS | |
64 | throw std::invalid_argument("Counter instrument updates must be non-negative."); | |
65 | # else | |
66 | std::terminate(); | |
67 | # endif | |
68 | } | |
69 | else | |
70 | { | |
71 | this->update(value); | |
72 | } | |
73 | } | |
74 | }; | |
75 | ||
76 | template <class T> | |
77 | class Counter final : public SynchronousInstrument<T>, public opentelemetry::metrics::Counter<T> | |
78 | { | |
79 | ||
80 | public: | |
81 | Counter() = default; | |
82 | ||
83 | Counter(nostd::string_view name, | |
84 | nostd::string_view description, | |
85 | nostd::string_view unit, | |
86 | bool enabled) | |
87 | : SynchronousInstrument<T>(name, | |
88 | description, | |
89 | unit, | |
90 | enabled, | |
91 | opentelemetry::metrics::InstrumentKind::Counter) | |
92 | {} | |
93 | ||
94 | /* | |
95 | * Bind creates a bound instrument for this counter. The labels are | |
96 | * associated with values recorded via subsequent calls to Record. | |
97 | * | |
98 | * @param labels the set of labels, as key-value pairs. | |
99 | * @return a BoundCounter tied to the specified labels | |
100 | */ | |
101 | ||
102 | virtual nostd::shared_ptr<opentelemetry::metrics::BoundCounter<T>> bindCounter( | |
103 | const opentelemetry::common::KeyValueIterable &labels) override | |
104 | { | |
105 | this->mu_.lock(); | |
106 | std::string labelset = KvToString(labels); | |
107 | if (boundInstruments_.find(labelset) == boundInstruments_.end()) | |
108 | { | |
109 | auto sp1 = nostd::shared_ptr<opentelemetry::metrics::BoundCounter<T>>( | |
110 | new BoundCounter<T>(this->name_, this->description_, this->unit_, this->enabled_)); | |
111 | boundInstruments_[labelset] = sp1; | |
112 | this->mu_.unlock(); | |
113 | return sp1; | |
114 | } | |
115 | else | |
116 | { | |
117 | boundInstruments_[labelset]->inc_ref(); | |
118 | auto ret = boundInstruments_[labelset]; | |
119 | this->mu_.unlock(); | |
120 | return ret; | |
121 | } | |
122 | } | |
123 | ||
124 | /* | |
125 | * Add adds the value to the counter's sum. The labels should contain | |
126 | * the keys and values to be associated with this value. Counters only | |
127 | * accept positive valued updates. | |
128 | * | |
129 | * @param value the numerical representation of the metric being captured | |
130 | * @param labels the set of labels, as key-value pairs | |
131 | */ | |
132 | virtual void add(T value, const opentelemetry::common::KeyValueIterable &labels) override | |
133 | { | |
134 | if (value < 0) | |
135 | { | |
136 | # if __EXCEPTIONS | |
137 | throw std::invalid_argument("Counter instrument updates must be non-negative."); | |
138 | # else | |
139 | std::terminate(); | |
140 | # endif | |
141 | } | |
142 | else | |
143 | { | |
144 | auto sp = bindCounter(labels); | |
145 | sp->update(value); | |
146 | sp->unbind(); | |
147 | } | |
148 | } | |
149 | ||
150 | virtual std::vector<Record> GetRecords() override | |
151 | { | |
152 | this->mu_.lock(); | |
153 | std::vector<Record> ret; | |
154 | std::vector<std::string> toDelete; | |
155 | for (const auto &x : boundInstruments_) | |
156 | { | |
157 | if (x.second->get_ref() == 0) | |
158 | { | |
159 | toDelete.push_back(x.first); | |
160 | } | |
161 | # ifdef OPENTELEMETRY_RTTI_ENABLED | |
162 | auto agg_ptr = dynamic_cast<BoundCounter<T> *>(x.second.get())->GetAggregator(); | |
163 | # else | |
164 | auto agg_ptr = static_cast<BoundCounter<T> *>(x.second.get())->GetAggregator(); | |
165 | # endif | |
166 | if (agg_ptr->is_updated()) | |
167 | { | |
168 | agg_ptr->checkpoint(); | |
169 | ret.push_back(Record(x.second->GetName(), x.second->GetDescription(), x.first, agg_ptr)); | |
170 | } | |
171 | } | |
172 | for (const auto &x : toDelete) | |
173 | { | |
174 | boundInstruments_.erase(x); | |
175 | } | |
176 | this->mu_.unlock(); | |
177 | return ret; | |
178 | } | |
179 | ||
180 | virtual void update(T val, const opentelemetry::common::KeyValueIterable &labels) override | |
181 | { | |
182 | add(val, labels); | |
183 | } | |
184 | ||
185 | // A collection of the bound instruments created by this unbound instrument identified by their | |
186 | // labels. | |
187 | std::unordered_map<std::string, nostd::shared_ptr<opentelemetry::metrics::BoundCounter<T>>> | |
188 | boundInstruments_; | |
189 | }; | |
190 | ||
191 | template <class T> | |
192 | class BoundUpDownCounter final : public BoundSynchronousInstrument<T>, | |
193 | virtual public opentelemetry::metrics::BoundUpDownCounter<T> | |
194 | { | |
195 | ||
196 | public: | |
197 | BoundUpDownCounter() = default; | |
198 | ||
199 | BoundUpDownCounter(nostd::string_view name, | |
200 | nostd::string_view description, | |
201 | nostd::string_view unit, | |
202 | bool enabled) | |
203 | : BoundSynchronousInstrument<T>(name, | |
204 | description, | |
205 | unit, | |
206 | enabled, | |
207 | opentelemetry::metrics::InstrumentKind::UpDownCounter, | |
208 | std::shared_ptr<Aggregator<T>>(new CounterAggregator<T>( | |
209 | opentelemetry::metrics::InstrumentKind::UpDownCounter))) | |
210 | {} | |
211 | ||
212 | /* | |
213 | * Add adds the value to the counter's sum. The labels are already linked to the instrument | |
214 | * and are not specified. | |
215 | * | |
216 | * @param value the numerical representation of the metric being captured | |
217 | * @param labels the set of labels, as key-value pairs | |
218 | */ | |
219 | virtual void add(T value) override { this->update(value); } | |
220 | }; | |
221 | ||
222 | template <class T> | |
223 | class UpDownCounter final : public SynchronousInstrument<T>, | |
224 | public opentelemetry::metrics::UpDownCounter<T> | |
225 | { | |
226 | ||
227 | public: | |
228 | UpDownCounter() = default; | |
229 | ||
230 | UpDownCounter(nostd::string_view name, | |
231 | nostd::string_view description, | |
232 | nostd::string_view unit, | |
233 | bool enabled) | |
234 | : SynchronousInstrument<T>(name, | |
235 | description, | |
236 | unit, | |
237 | enabled, | |
238 | opentelemetry::metrics::InstrumentKind::UpDownCounter) | |
239 | {} | |
240 | ||
241 | /* | |
242 | * Bind creates a bound instrument for this counter. The labels are | |
243 | * associated with values recorded via subsequent calls to Record. | |
244 | * | |
245 | * @param labels the set of labels, as key-value pairs. | |
246 | * @return a BoundIntCounter tied to the specified labels | |
247 | */ | |
248 | nostd::shared_ptr<opentelemetry::metrics::BoundUpDownCounter<T>> bindUpDownCounter( | |
249 | const opentelemetry::common::KeyValueIterable &labels) override | |
250 | { | |
251 | this->mu_.lock(); | |
252 | std::string labelset = KvToString(labels); | |
253 | if (boundInstruments_.find(labelset) == boundInstruments_.end()) | |
254 | { | |
255 | auto sp1 = nostd::shared_ptr<opentelemetry::metrics::BoundUpDownCounter<T>>( | |
256 | new BoundUpDownCounter<T>(this->name_, this->description_, this->unit_, this->enabled_)); | |
257 | boundInstruments_[labelset] = sp1; | |
258 | this->mu_.unlock(); | |
259 | return sp1; | |
260 | } | |
261 | else | |
262 | { | |
263 | boundInstruments_[labelset]->inc_ref(); | |
264 | auto ret = boundInstruments_[labelset]; | |
265 | this->mu_.unlock(); | |
266 | return ret; | |
267 | } | |
268 | } | |
269 | ||
270 | /* | |
271 | * Add adds the value to the counter's sum. The labels should contain | |
272 | * the keys and values to be associated with this value. Counters only | |
273 | * accept positive valued updates. | |
274 | * | |
275 | * @param value the numerical representation of the metric being captured | |
276 | * @param labels the set of labels, as key-value pairs | |
277 | */ | |
278 | void add(T value, const opentelemetry::common::KeyValueIterable &labels) override | |
279 | { | |
280 | auto sp = bindUpDownCounter(labels); | |
281 | sp->update(value); | |
282 | sp->unbind(); | |
283 | } | |
284 | ||
285 | virtual std::vector<Record> GetRecords() override | |
286 | { | |
287 | this->mu_.lock(); | |
288 | std::vector<Record> ret; | |
289 | std::vector<std::string> toDelete; | |
290 | for (const auto &x : boundInstruments_) | |
291 | { | |
292 | if (x.second->get_ref() == 0) | |
293 | { | |
294 | toDelete.push_back(x.first); | |
295 | } | |
296 | # ifdef OPENTELEMETRY_RTTI_ENABLED | |
297 | auto agg_ptr = dynamic_cast<BoundUpDownCounter<T> *>(x.second.get())->GetAggregator(); | |
298 | # else | |
299 | auto agg_ptr = static_cast<BoundUpDownCounter<T> *>(x.second.get())->GetAggregator(); | |
300 | # endif | |
301 | if (agg_ptr->is_updated()) | |
302 | { | |
303 | agg_ptr->checkpoint(); | |
304 | ret.push_back(Record(x.second->GetName(), x.second->GetDescription(), x.first, agg_ptr)); | |
305 | } | |
306 | } | |
307 | for (const auto &x : toDelete) | |
308 | { | |
309 | boundInstruments_.erase(x); | |
310 | } | |
311 | this->mu_.unlock(); | |
312 | return ret; | |
313 | } | |
314 | ||
315 | virtual void update(T val, const opentelemetry::common::KeyValueIterable &labels) override | |
316 | { | |
317 | add(val, labels); | |
318 | } | |
319 | ||
320 | std::unordered_map<std::string, nostd::shared_ptr<opentelemetry::metrics::BoundUpDownCounter<T>>> | |
321 | boundInstruments_; | |
322 | }; | |
323 | ||
324 | template <class T> | |
325 | class BoundValueRecorder final : public BoundSynchronousInstrument<T>, | |
326 | public opentelemetry::metrics::BoundValueRecorder<T> | |
327 | { | |
328 | ||
329 | public: | |
330 | BoundValueRecorder() = default; | |
331 | ||
332 | BoundValueRecorder(nostd::string_view name, | |
333 | nostd::string_view description, | |
334 | nostd::string_view unit, | |
335 | bool enabled) | |
336 | : BoundSynchronousInstrument<T>( | |
337 | name, | |
338 | description, | |
339 | unit, | |
340 | enabled, | |
341 | opentelemetry::metrics::InstrumentKind::ValueRecorder, | |
342 | std::shared_ptr<Aggregator<T>>(new MinMaxSumCountAggregator<T>( | |
343 | opentelemetry::metrics::InstrumentKind::ValueRecorder))) | |
344 | {} | |
345 | ||
346 | /* | |
347 | * Add adds the value to the counter's sum. The labels are already linked to the instrument | |
348 | * and are not specified. | |
349 | * | |
350 | * @param value the numerical representation of the metric being captured | |
351 | * @param labels the set of labels, as key-value pairs | |
352 | */ | |
353 | void record(T value) { this->update(value); } | |
354 | }; | |
355 | ||
356 | template <class T> | |
357 | class ValueRecorder final : public SynchronousInstrument<T>, | |
358 | public opentelemetry::metrics::ValueRecorder<T> | |
359 | { | |
360 | ||
361 | public: | |
362 | ValueRecorder() = default; | |
363 | ||
364 | ValueRecorder(nostd::string_view name, | |
365 | nostd::string_view description, | |
366 | nostd::string_view unit, | |
367 | bool enabled) | |
368 | : SynchronousInstrument<T>(name, | |
369 | description, | |
370 | unit, | |
371 | enabled, | |
372 | opentelemetry::metrics::InstrumentKind::ValueRecorder) | |
373 | {} | |
374 | ||
375 | /* | |
376 | * Bind creates a bound instrument for this counter. The labels are | |
377 | * associated with values recorded via subsequent calls to Record. | |
378 | * | |
379 | * @param labels the set of labels, as key-value pairs. | |
380 | * @return a BoundIntCounter tied to the specified labels | |
381 | */ | |
382 | nostd::shared_ptr<opentelemetry::metrics::BoundValueRecorder<T>> bindValueRecorder( | |
383 | const opentelemetry::common::KeyValueIterable &labels) override | |
384 | { | |
385 | this->mu_.lock(); | |
386 | std::string labelset = KvToString(labels); | |
387 | if (boundInstruments_.find(labelset) == boundInstruments_.end()) | |
388 | { | |
389 | auto sp1 = nostd::shared_ptr<opentelemetry::metrics::BoundValueRecorder<T>>( | |
390 | new BoundValueRecorder<T>(this->name_, this->description_, this->unit_, this->enabled_)); | |
391 | boundInstruments_[labelset] = sp1; | |
392 | this->mu_.unlock(); | |
393 | return sp1; | |
394 | } | |
395 | else | |
396 | { | |
397 | boundInstruments_[labelset]->inc_ref(); | |
398 | auto ret = boundInstruments_[labelset]; | |
399 | this->mu_.unlock(); | |
400 | return ret; | |
401 | } | |
402 | } | |
403 | ||
404 | /* | |
405 | * Add adds the value to the counter's sum. The labels should contain | |
406 | * the keys and values to be associated with this value. Counters only | |
407 | * accept positive valued updates. | |
408 | * | |
409 | * @param value the numerical representation of the metric being captured | |
410 | * @param labels the set of labels, as key-value pairs | |
411 | */ | |
412 | void record(T value, const opentelemetry::common::KeyValueIterable &labels) override | |
413 | { | |
414 | auto sp = bindValueRecorder(labels); | |
415 | sp->update(value); | |
416 | sp->unbind(); | |
417 | } | |
418 | ||
419 | virtual std::vector<Record> GetRecords() override | |
420 | { | |
421 | this->mu_.lock(); | |
422 | std::vector<Record> ret; | |
423 | std::vector<std::string> toDelete; | |
424 | for (const auto &x : boundInstruments_) | |
425 | { | |
426 | if (x.second->get_ref() == 0) | |
427 | { | |
428 | toDelete.push_back(x.first); | |
429 | } | |
430 | # ifdef OPENTELEMETRY_RTTI_ENABLED | |
431 | auto agg_ptr = dynamic_cast<BoundValueRecorder<T> *>(x.second.get())->GetAggregator(); | |
432 | # else | |
433 | auto agg_ptr = static_cast<BoundValueRecorder<T> *>(x.second.get())->GetAggregator(); | |
434 | # endif | |
435 | if (agg_ptr->is_updated()) | |
436 | { | |
437 | agg_ptr->checkpoint(); | |
438 | ret.push_back(Record(x.second->GetName(), x.second->GetDescription(), x.first, agg_ptr)); | |
439 | } | |
440 | } | |
441 | for (const auto &x : toDelete) | |
442 | { | |
443 | boundInstruments_.erase(x); | |
444 | } | |
445 | this->mu_.unlock(); | |
446 | return ret; | |
447 | } | |
448 | ||
449 | virtual void update(T value, const opentelemetry::common::KeyValueIterable &labels) override | |
450 | { | |
451 | record(value, labels); | |
452 | } | |
453 | ||
454 | std::unordered_map<std::string, nostd::shared_ptr<opentelemetry::metrics::BoundValueRecorder<T>>> | |
455 | boundInstruments_; | |
456 | }; | |
457 | ||
458 | # if defined(_MSC_VER) | |
459 | # pragma warning(pop) | |
460 | # endif | |
461 | ||
462 | } // namespace metrics | |
463 | } // namespace sdk | |
464 | OPENTELEMETRY_END_NAMESPACE | |
465 | #endif |