tracer_provider.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/version.h"
  9. OPENTELEMETRY_BEGIN_NAMESPACE
  10. namespace trace
  11. {
  12. class Tracer;
  13. /**
  14. * Creates new Tracer instances.
  15. */
  16. class OPENTELEMETRY_EXPORT TracerProvider
  17. {
  18. public:
  19. virtual ~TracerProvider() = default;
  20. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  21. /**
  22. * Gets or creates a named Tracer instance (ABI).
  23. *
  24. * @since ABI_VERSION 2
  25. *
  26. * @param[in] name Tracer instrumentation scope
  27. * @param[in] version Instrumentation scope version
  28. * @param[in] schema_url Instrumentation scope schema URL
  29. * @param[in] attributes Instrumentation scope attributes (optional, may be nullptr)
  30. */
  31. virtual nostd::shared_ptr<Tracer> GetTracer(
  32. nostd::string_view name,
  33. nostd::string_view version,
  34. nostd::string_view schema_url,
  35. const common::KeyValueIterable *attributes) noexcept = 0;
  36. /**
  37. * Gets or creates a named Tracer instance (API helper).
  38. *
  39. * @since ABI_VERSION 2
  40. *
  41. * @param[in] name Tracer instrumentation scope
  42. * @param[in] version Instrumentation scope version, optional
  43. * @param[in] schema_url Instrumentation scope schema URL, optional
  44. */
  45. nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
  46. nostd::string_view version = "",
  47. nostd::string_view schema_url = "")
  48. {
  49. return GetTracer(name, version, schema_url, nullptr);
  50. }
  51. /**
  52. * Gets or creates a named Tracer instance (API helper).
  53. *
  54. * @since ABI_VERSION 2
  55. *
  56. * @param[in] name Tracer instrumentation scope
  57. * @param[in] version Instrumentation scope version
  58. * @param[in] schema_url Instrumentation scope schema URL
  59. * @param[in] attributes Instrumentation scope attributes
  60. */
  61. nostd::shared_ptr<Tracer> GetTracer(
  62. nostd::string_view name,
  63. nostd::string_view version,
  64. nostd::string_view schema_url,
  65. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes)
  66. {
  67. /* Build a container from std::initializer_list. */
  68. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes_span{
  69. attributes.begin(), attributes.end()};
  70. /* Build a view on the container. */
  71. common::KeyValueIterableView<
  72. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
  73. iterable_attributes{attributes_span};
  74. /* Add attributes using the view. */
  75. return GetTracer(name, version, schema_url, &iterable_attributes);
  76. }
  77. /**
  78. * Gets or creates a named Tracer instance (API helper).
  79. *
  80. * @since ABI_VERSION 2
  81. *
  82. * @param[in] name Tracer instrumentation scope
  83. * @param[in] version Instrumentation scope version
  84. * @param[in] schema_url Instrumentation scope schema URL
  85. * @param[in] attributes Instrumentation scope attributes container
  86. */
  87. template <class T,
  88. nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
  89. nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
  90. nostd::string_view version,
  91. nostd::string_view schema_url,
  92. const T &attributes)
  93. {
  94. /* Build a view on the container. */
  95. common::KeyValueIterableView<T> iterable_attributes(attributes);
  96. /* Add attributes using the view. */
  97. return GetTracer(name, version, schema_url, &iterable_attributes);
  98. }
  99. #else
  100. /**
  101. * Gets or creates a named tracer instance.
  102. *
  103. * Optionally a version can be passed to create a named and versioned tracer
  104. * instance.
  105. */
  106. virtual nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
  107. nostd::string_view version = "",
  108. nostd::string_view schema_url = "") noexcept = 0;
  109. #endif
  110. };
  111. } // namespace trace
  112. OPENTELEMETRY_END_NAMESPACE