noop.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/metrics/async_instruments.h"
  5. #include "opentelemetry/metrics/meter.h"
  6. #include "opentelemetry/metrics/meter_provider.h"
  7. #include "opentelemetry/metrics/observer_result.h"
  8. #include "opentelemetry/metrics/sync_instruments.h"
  9. #include "opentelemetry/version.h"
  10. OPENTELEMETRY_BEGIN_NAMESPACE
  11. namespace metrics
  12. {
  13. template <class T>
  14. class NoopCounter : public Counter<T>
  15. {
  16. public:
  17. NoopCounter(nostd::string_view /* name */,
  18. nostd::string_view /* description */,
  19. nostd::string_view /* unit */) noexcept
  20. {}
  21. void Add(T /* value */) noexcept override {}
  22. void Add(T /* value */, const context::Context & /* context */) noexcept override {}
  23. void Add(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {}
  24. void Add(T /* value */,
  25. const common::KeyValueIterable & /* attributes */,
  26. const context::Context & /* context */) noexcept override
  27. {}
  28. };
  29. template <class T>
  30. class NoopHistogram : public Histogram<T>
  31. {
  32. public:
  33. NoopHistogram(nostd::string_view /* name */,
  34. nostd::string_view /* description */,
  35. nostd::string_view /* unit */) noexcept
  36. {}
  37. void Record(T /* value */, const context::Context & /* context */) noexcept override {}
  38. void Record(T /* value */,
  39. const common::KeyValueIterable & /* attributes */,
  40. const context::Context & /* context */) noexcept override
  41. {}
  42. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  43. void Record(T /*value*/,
  44. const opentelemetry::common::KeyValueIterable & /*attributes*/) noexcept override
  45. {}
  46. void Record(T /*value*/) noexcept override {}
  47. #endif
  48. };
  49. template <class T>
  50. class NoopUpDownCounter : public UpDownCounter<T>
  51. {
  52. public:
  53. NoopUpDownCounter(nostd::string_view /* name */,
  54. nostd::string_view /* description */,
  55. nostd::string_view /* unit */) noexcept
  56. {}
  57. ~NoopUpDownCounter() override = default;
  58. void Add(T /* value */) noexcept override {}
  59. void Add(T /* value */, const context::Context & /* context */) noexcept override {}
  60. void Add(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {}
  61. void Add(T /* value */,
  62. const common::KeyValueIterable & /* attributes */,
  63. const context::Context & /* context */) noexcept override
  64. {}
  65. };
  66. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  67. template <class T>
  68. class NoopGauge : public Gauge<T>
  69. {
  70. public:
  71. NoopGauge(nostd::string_view /* name */,
  72. nostd::string_view /* description */,
  73. nostd::string_view /* unit */) noexcept
  74. {}
  75. ~NoopGauge() override = default;
  76. void Record(T /* value */) noexcept override {}
  77. void Record(T /* value */, const context::Context & /* context */) noexcept override {}
  78. void Record(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {}
  79. void Record(T /* value */,
  80. const common::KeyValueIterable & /* attributes */,
  81. const context::Context & /* context */) noexcept override
  82. {}
  83. };
  84. #endif
  85. class NoopObservableInstrument : public ObservableInstrument
  86. {
  87. public:
  88. NoopObservableInstrument(nostd::string_view /* name */,
  89. nostd::string_view /* description */,
  90. nostd::string_view /* unit */) noexcept
  91. {}
  92. void AddCallback(ObservableCallbackPtr, void * /* state */) noexcept override {}
  93. void RemoveCallback(ObservableCallbackPtr, void * /* state */) noexcept override {}
  94. };
  95. /**
  96. * No-op implementation of Meter.
  97. */
  98. class NoopMeter final : public Meter
  99. {
  100. public:
  101. nostd::unique_ptr<Counter<uint64_t>> CreateUInt64Counter(
  102. nostd::string_view name,
  103. nostd::string_view description = "",
  104. nostd::string_view unit = "") noexcept override
  105. {
  106. return nostd::unique_ptr<Counter<uint64_t>>{new NoopCounter<uint64_t>(name, description, unit)};
  107. }
  108. nostd::unique_ptr<Counter<double>> CreateDoubleCounter(
  109. nostd::string_view name,
  110. nostd::string_view description = "",
  111. nostd::string_view unit = "") noexcept override
  112. {
  113. return nostd::unique_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
  114. }
  115. nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableCounter(
  116. nostd::string_view name,
  117. nostd::string_view description = "",
  118. nostd::string_view unit = "") noexcept override
  119. {
  120. return nostd::shared_ptr<ObservableInstrument>(
  121. new NoopObservableInstrument(name, description, unit));
  122. }
  123. nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableCounter(
  124. nostd::string_view name,
  125. nostd::string_view description = "",
  126. nostd::string_view unit = "") noexcept override
  127. {
  128. return nostd::shared_ptr<ObservableInstrument>(
  129. new NoopObservableInstrument(name, description, unit));
  130. }
  131. nostd::unique_ptr<Histogram<uint64_t>> CreateUInt64Histogram(
  132. nostd::string_view name,
  133. nostd::string_view description = "",
  134. nostd::string_view unit = "") noexcept override
  135. {
  136. return nostd::unique_ptr<Histogram<uint64_t>>{
  137. new NoopHistogram<uint64_t>(name, description, unit)};
  138. }
  139. nostd::unique_ptr<Histogram<double>> CreateDoubleHistogram(
  140. nostd::string_view name,
  141. nostd::string_view description = "",
  142. nostd::string_view unit = "") noexcept override
  143. {
  144. return nostd::unique_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
  145. }
  146. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  147. nostd::unique_ptr<Gauge<int64_t>> CreateInt64Gauge(nostd::string_view name,
  148. nostd::string_view description = "",
  149. nostd::string_view unit = "") noexcept override
  150. {
  151. return nostd::unique_ptr<Gauge<int64_t>>{new NoopGauge<int64_t>(name, description, unit)};
  152. }
  153. nostd::unique_ptr<Gauge<double>> CreateDoubleGauge(nostd::string_view name,
  154. nostd::string_view description = "",
  155. nostd::string_view unit = "") noexcept override
  156. {
  157. return nostd::unique_ptr<Gauge<double>>{new NoopGauge<double>(name, description, unit)};
  158. }
  159. #endif
  160. nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableGauge(
  161. nostd::string_view name,
  162. nostd::string_view description = "",
  163. nostd::string_view unit = "") noexcept override
  164. {
  165. return nostd::shared_ptr<ObservableInstrument>(
  166. new NoopObservableInstrument(name, description, unit));
  167. }
  168. nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableGauge(
  169. nostd::string_view name,
  170. nostd::string_view description = "",
  171. nostd::string_view unit = "") noexcept override
  172. {
  173. return nostd::shared_ptr<ObservableInstrument>(
  174. new NoopObservableInstrument(name, description, unit));
  175. }
  176. nostd::unique_ptr<UpDownCounter<int64_t>> CreateInt64UpDownCounter(
  177. nostd::string_view name,
  178. nostd::string_view description = "",
  179. nostd::string_view unit = "") noexcept override
  180. {
  181. return nostd::unique_ptr<UpDownCounter<int64_t>>{
  182. new NoopUpDownCounter<int64_t>(name, description, unit)};
  183. }
  184. nostd::unique_ptr<UpDownCounter<double>> CreateDoubleUpDownCounter(
  185. nostd::string_view name,
  186. nostd::string_view description = "",
  187. nostd::string_view unit = "") noexcept override
  188. {
  189. return nostd::unique_ptr<UpDownCounter<double>>{
  190. new NoopUpDownCounter<double>(name, description, unit)};
  191. }
  192. nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableUpDownCounter(
  193. nostd::string_view name,
  194. nostd::string_view description = "",
  195. nostd::string_view unit = "") noexcept override
  196. {
  197. return nostd::shared_ptr<ObservableInstrument>(
  198. new NoopObservableInstrument(name, description, unit));
  199. }
  200. nostd::shared_ptr<ObservableInstrument> CreateDoubleObservableUpDownCounter(
  201. nostd::string_view name,
  202. nostd::string_view description = "",
  203. nostd::string_view unit = "") noexcept override
  204. {
  205. return nostd::shared_ptr<ObservableInstrument>(
  206. new NoopObservableInstrument(name, description, unit));
  207. }
  208. };
  209. /**
  210. * No-op implementation of a MeterProvider.
  211. */
  212. class NoopMeterProvider final : public MeterProvider
  213. {
  214. public:
  215. NoopMeterProvider() : meter_{nostd::shared_ptr<Meter>(new NoopMeter)} {}
  216. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  217. nostd::shared_ptr<Meter> GetMeter(
  218. nostd::string_view /* name */,
  219. nostd::string_view /* version */,
  220. nostd::string_view /* schema_url */,
  221. const common::KeyValueIterable * /* attributes */) noexcept override
  222. {
  223. return meter_;
  224. }
  225. #else
  226. nostd::shared_ptr<Meter> GetMeter(nostd::string_view /* name */,
  227. nostd::string_view /* version */,
  228. nostd::string_view /* schema_url */) noexcept override
  229. {
  230. return meter_;
  231. }
  232. #endif
  233. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  234. void RemoveMeter(nostd::string_view /* name */,
  235. nostd::string_view /* version */,
  236. nostd::string_view /* schema_url */) noexcept override
  237. {}
  238. #endif
  239. private:
  240. nostd::shared_ptr<Meter> meter_;
  241. };
  242. } // namespace metrics
  243. OPENTELEMETRY_END_NAMESPACE