sync_instruments.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/common/attribute_value.h"
  5. #include "opentelemetry/common/key_value_iterable_view.h"
  6. #include "opentelemetry/context/context.h"
  7. #include "opentelemetry/nostd/span.h"
  8. #include "opentelemetry/nostd/string_view.h"
  9. #include "opentelemetry/nostd/type_traits.h"
  10. #include "opentelemetry/version.h"
  11. OPENTELEMETRY_BEGIN_NAMESPACE
  12. namespace metrics
  13. {
  14. class SynchronousInstrument
  15. {
  16. public:
  17. SynchronousInstrument() = default;
  18. virtual ~SynchronousInstrument() = default;
  19. };
  20. /* A Counter instrument that adds values. */
  21. template <class T>
  22. class Counter : public SynchronousInstrument
  23. {
  24. public:
  25. /**
  26. * Record a value
  27. *
  28. * @param value The increment amount. MUST be non-negative.
  29. */
  30. virtual void Add(T value) noexcept = 0;
  31. /**
  32. * Record a value
  33. *
  34. * @param value The increment amount. MUST be non-negative.
  35. * @param context The explicit context to associate with this measurement.
  36. */
  37. virtual void Add(T value, const context::Context &context) noexcept = 0;
  38. /**
  39. * Record a value with a set of attributes.
  40. *
  41. * @param value The increment amount. MUST be non-negative.
  42. * @param attributes A set of attributes to associate with the value.
  43. */
  44. virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0;
  45. /**
  46. * Record a value with a set of attributes.
  47. *
  48. * @param value The increment amount. MUST be non-negative.
  49. * @param attributes A set of attributes to associate with the value.
  50. * @param context The explicit context to associate with this measurement.
  51. */
  52. virtual void Add(T value,
  53. const common::KeyValueIterable &attributes,
  54. const context::Context &context) noexcept = 0;
  55. template <class U,
  56. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  57. void Add(T value, const U &attributes) noexcept
  58. {
  59. this->Add(value, common::KeyValueIterableView<U>{attributes});
  60. }
  61. template <class U,
  62. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  63. void Add(T value, const U &attributes, const context::Context &context) noexcept
  64. {
  65. this->Add(value, common::KeyValueIterableView<U>{attributes}, context);
  66. }
  67. void Add(T value,
  68. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  69. attributes) noexcept
  70. {
  71. this->Add(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  72. attributes.begin(), attributes.end()});
  73. }
  74. void Add(T value,
  75. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
  76. const context::Context &context) noexcept
  77. {
  78. this->Add(value,
  79. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  80. attributes.begin(), attributes.end()},
  81. context);
  82. }
  83. };
  84. /** A histogram instrument that records values. */
  85. template <class T>
  86. class Histogram : public SynchronousInstrument
  87. {
  88. public:
  89. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  90. /**
  91. * @since ABI_VERSION 2
  92. * Records a value.
  93. *
  94. * @param value The measurement value. MUST be non-negative.
  95. */
  96. virtual void Record(T value) noexcept = 0;
  97. /**
  98. * @since ABI_VERSION 2
  99. * Records a value with a set of attributes.
  100. *
  101. * @param value The measurement value. MUST be non-negative.
  102. * @param attribute A set of attributes to associate with the value.
  103. */
  104. virtual void Record(T value, const common::KeyValueIterable &attribute) noexcept = 0;
  105. template <class U,
  106. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  107. void Record(T value, const U &attributes) noexcept
  108. {
  109. this->Record(value, common::KeyValueIterableView<U>{attributes});
  110. }
  111. void Record(T value,
  112. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  113. attributes) noexcept
  114. {
  115. this->Record(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  116. attributes.begin(), attributes.end()});
  117. }
  118. #endif
  119. /**
  120. * Records a value.
  121. *
  122. * @param value The measurement value. MUST be non-negative.
  123. * @param context The explicit context to associate with this measurement.
  124. */
  125. virtual void Record(T value, const context::Context &context) noexcept = 0;
  126. /**
  127. * Records a value with a set of attributes.
  128. *
  129. * @param value The measurement value. MUST be non-negative.
  130. * @param attributes A set of attributes to associate with the value..
  131. * @param context The explicit context to associate with this measurement.
  132. */
  133. virtual void Record(T value,
  134. const common::KeyValueIterable &attributes,
  135. const context::Context &context) noexcept = 0;
  136. template <class U,
  137. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  138. void Record(T value, const U &attributes, const context::Context &context) noexcept
  139. {
  140. this->Record(value, common::KeyValueIterableView<U>{attributes}, context);
  141. }
  142. void Record(
  143. T value,
  144. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
  145. const context::Context &context) noexcept
  146. {
  147. this->Record(value,
  148. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  149. attributes.begin(), attributes.end()},
  150. context);
  151. }
  152. };
  153. /** An up-down-counter instrument that adds or reduce values. */
  154. template <class T>
  155. class UpDownCounter : public SynchronousInstrument
  156. {
  157. public:
  158. /**
  159. * Record a value.
  160. *
  161. * @param value The increment amount. May be positive, negative or zero.
  162. */
  163. virtual void Add(T value) noexcept = 0;
  164. /**
  165. * Record a value.
  166. *
  167. * @param value The increment amount. May be positive, negative or zero.
  168. * @param context The explicit context to associate with this measurement.
  169. */
  170. virtual void Add(T value, const context::Context &context) noexcept = 0;
  171. /**
  172. * Record a value with a set of attributes.
  173. *
  174. * @param value The increment amount. May be positive, negative or zero.
  175. * @param attributes A set of attributes to associate with the count.
  176. */
  177. virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0;
  178. /**
  179. * Record a value with a set of attributes.
  180. *
  181. * @param value The increment amount. May be positive, negative or zero.
  182. * @param attributes A set of attributes to associate with the count.
  183. * @param context The explicit context to associate with this measurement.
  184. */
  185. virtual void Add(T value,
  186. const common::KeyValueIterable &attributes,
  187. const context::Context &context) noexcept = 0;
  188. template <class U,
  189. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  190. void Add(T value, const U &attributes) noexcept
  191. {
  192. this->Add(value, common::KeyValueIterableView<U>{attributes});
  193. }
  194. template <class U,
  195. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  196. void Add(T value, const U &attributes, const context::Context &context) noexcept
  197. {
  198. this->Add(value, common::KeyValueIterableView<U>{attributes}, context);
  199. }
  200. void Add(T value,
  201. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  202. attributes) noexcept
  203. {
  204. this->Add(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  205. attributes.begin(), attributes.end()});
  206. }
  207. void Add(T value,
  208. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
  209. const context::Context &context) noexcept
  210. {
  211. this->Add(value,
  212. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  213. attributes.begin(), attributes.end()},
  214. context);
  215. }
  216. };
  217. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  218. /* A Gauge instrument that records values. */
  219. template <class T>
  220. class Gauge : public SynchronousInstrument
  221. {
  222. public:
  223. /**
  224. * Record a value
  225. *
  226. * @param value The measurement value. May be positive, negative or zero.
  227. */
  228. virtual void Record(T value) noexcept = 0;
  229. /**
  230. * Record a value
  231. *
  232. * @param value The measurement value. May be positive, negative or zero.
  233. * @param context The explicit context to associate with this measurement.
  234. */
  235. virtual void Record(T value, const context::Context &context) noexcept = 0;
  236. /**
  237. * Record a value with a set of attributes.
  238. *
  239. * @param value The measurement value. May be positive, negative or zero.
  240. * @param attributes A set of attributes to associate with the value.
  241. */
  242. virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0;
  243. /**
  244. * Record a value with a set of attributes.
  245. *
  246. * @param value The measurement value. May be positive, negative or zero.
  247. * @param attributes A set of attributes to associate with the value.
  248. * @param context The explicit context to associate with this measurement.
  249. */
  250. virtual void Record(T value,
  251. const common::KeyValueIterable &attributes,
  252. const context::Context &context) noexcept = 0;
  253. template <class U,
  254. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  255. void Record(T value, const U &attributes) noexcept
  256. {
  257. this->Record(value, common::KeyValueIterableView<U>{attributes});
  258. }
  259. template <class U,
  260. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  261. void Record(T value, const U &attributes, const context::Context &context) noexcept
  262. {
  263. this->Record(value, common::KeyValueIterableView<U>{attributes}, context);
  264. }
  265. void Record(T value,
  266. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  267. attributes) noexcept
  268. {
  269. this->Record(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  270. attributes.begin(), attributes.end()});
  271. }
  272. void Record(
  273. T value,
  274. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
  275. const context::Context &context) noexcept
  276. {
  277. this->Record(value,
  278. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  279. attributes.begin(), attributes.end()},
  280. context);
  281. }
  282. };
  283. #endif
  284. } // namespace metrics
  285. OPENTELEMETRY_END_NAMESPACE