meter_provider.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/common/key_value_iterable.h"
  5. #include "opentelemetry/common/key_value_iterable_view.h"
  6. #include "opentelemetry/nostd/shared_ptr.h"
  7. #include "opentelemetry/nostd/string_view.h"
  8. #include "opentelemetry/nostd/type_traits.h"
  9. #include "opentelemetry/version.h"
  10. OPENTELEMETRY_BEGIN_NAMESPACE
  11. namespace metrics
  12. {
  13. class Meter;
  14. /**
  15. * Creates new Meter instances.
  16. */
  17. class MeterProvider
  18. {
  19. public:
  20. virtual ~MeterProvider() = default;
  21. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  22. /**
  23. * Gets or creates a named Meter instance (ABI).
  24. *
  25. * @since ABI_VERSION 2
  26. *
  27. * @param[in] name Meter instrumentation scope
  28. * @param[in] version Instrumentation scope version
  29. * @param[in] schema_url Instrumentation scope schema URL
  30. * @param[in] attributes Instrumentation scope attributes (optional, may be nullptr)
  31. */
  32. virtual nostd::shared_ptr<Meter> GetMeter(
  33. nostd::string_view name,
  34. nostd::string_view version,
  35. nostd::string_view schema_url,
  36. const common::KeyValueIterable *attributes) noexcept = 0;
  37. /**
  38. * Gets or creates a named Meter instance (API helper).
  39. *
  40. * @since ABI_VERSION 2
  41. *
  42. * @param[in] name Meter instrumentation scope
  43. * @param[in] version Instrumentation scope version, optional
  44. * @param[in] schema_url Instrumentation scope schema URL, optional
  45. */
  46. nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
  47. nostd::string_view version = "",
  48. nostd::string_view schema_url = "")
  49. {
  50. return GetMeter(name, version, schema_url, nullptr);
  51. }
  52. /**
  53. * Gets or creates a named Meter instance (API helper).
  54. *
  55. * @since ABI_VERSION 2
  56. *
  57. * @param[in] name Meter instrumentation scope
  58. * @param[in] version Instrumentation scope version
  59. * @param[in] schema_url Instrumentation scope schema URL
  60. * @param[in] attributes Instrumentation scope attributes
  61. */
  62. nostd::shared_ptr<Meter> GetMeter(
  63. nostd::string_view name,
  64. nostd::string_view version,
  65. nostd::string_view schema_url,
  66. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes)
  67. {
  68. /* Build a container from std::initializer_list. */
  69. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes_span{
  70. attributes.begin(), attributes.end()};
  71. /* Build a view on the container. */
  72. common::KeyValueIterableView<
  73. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
  74. iterable_attributes{attributes_span};
  75. /* Add attributes using the view. */
  76. return GetMeter(name, version, schema_url, &iterable_attributes);
  77. }
  78. /**
  79. * Gets or creates a named Meter instance (API helper).
  80. *
  81. * @since ABI_VERSION 2
  82. *
  83. * @param[in] name Meter instrumentation scope
  84. * @param[in] version Instrumentation scope version
  85. * @param[in] schema_url Instrumentation scope schema URL
  86. * @param[in] attributes Instrumentation scope attributes container
  87. */
  88. template <class T,
  89. nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
  90. nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
  91. nostd::string_view version,
  92. nostd::string_view schema_url,
  93. const T &attributes)
  94. {
  95. /* Build a view on the container. */
  96. common::KeyValueIterableView<T> iterable_attributes(attributes);
  97. /* Add attributes using the view. */
  98. return GetMeter(name, version, schema_url, &iterable_attributes);
  99. }
  100. #else
  101. /**
  102. * Gets or creates a named Meter instance (ABI)
  103. *
  104. * @since ABI_VERSION 1
  105. *
  106. * @param[in] name Meter instrumentation scope
  107. * @param[in] version Instrumentation scope version, optional
  108. * @param[in] schema_url Instrumentation scope schema URL, optional
  109. */
  110. virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
  111. nostd::string_view version = "",
  112. nostd::string_view schema_url = "") noexcept = 0;
  113. #endif
  114. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  115. /**
  116. * Remove a named Meter instance (ABI).
  117. *
  118. * This API is experimental, see
  119. * https://github.com/open-telemetry/opentelemetry-specification/issues/2232
  120. *
  121. * @since ABI_VERSION 2
  122. *
  123. * @param[in] name Meter instrumentation scope
  124. * @param[in] version Instrumentation scope version, optional
  125. * @param[in] schema_url Instrumentation scope schema URL, optional
  126. */
  127. virtual void RemoveMeter(nostd::string_view name,
  128. nostd::string_view version = "",
  129. nostd::string_view schema_url = "") noexcept = 0;
  130. #endif
  131. };
  132. } // namespace metrics
  133. OPENTELEMETRY_END_NAMESPACE