span.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <cstdint>
  5. #include "opentelemetry/common/attribute_value.h"
  6. #include "opentelemetry/common/key_value_iterable_view.h"
  7. #include "opentelemetry/nostd/span.h"
  8. #include "opentelemetry/nostd/string_view.h"
  9. #include "opentelemetry/nostd/type_traits.h"
  10. #include "opentelemetry/trace/span_context.h"
  11. #include "opentelemetry/trace/span_context_kv_iterable.h"
  12. #include "opentelemetry/trace/span_context_kv_iterable_view.h"
  13. #include "opentelemetry/trace/span_metadata.h"
  14. #include "opentelemetry/version.h"
  15. OPENTELEMETRY_BEGIN_NAMESPACE
  16. namespace trace
  17. {
  18. class Tracer;
  19. /**
  20. * A Span represents a single operation within a Trace.
  21. *
  22. * Span attributes can be provided:
  23. * - at span creation time, using Tracer::StartSpan(),
  24. * - during the span lifetime, using Span::SetAttribute()
  25. *
  26. * Please note that head samplers,
  27. * in the SDK (@ref opentelemetry::sdk::trace::Sampler),
  28. * can only make sampling decisions based on data known
  29. * at span creation time.
  30. *
  31. * When attributes are known early, adding attributes
  32. * with @ref opentelemetry::trace::Tracer::StartSpan() is preferable.
  33. *
  34. * Attributes added or changed with Span::SetAttribute()
  35. * can not change a sampler decision.
  36. *
  37. * Likewise, links can be provided:
  38. * - at span creation time, using Tracer::StartSpan(),
  39. * - during the span lifetime, using Span::AddLink() or Span::AddLinks().
  40. *
  41. * When links are known early, adding links
  42. * with @ref opentelemetry::trace::Tracer::StartSpan() is preferable.
  43. *
  44. * Links added with Span::AddLink() or Span::AddLinks()
  45. * can not change a sampler decision.
  46. */
  47. class Span
  48. {
  49. public:
  50. // Note that Spans should be created using the Tracer class. Please refer to
  51. // tracer.h for documentation.
  52. Span() = default;
  53. // The Span destructor End()s the Span, if it hasn't been ended already.
  54. virtual ~Span() = default;
  55. // Not copiable or movable.
  56. Span(const Span &) = delete;
  57. Span(Span &&) = delete;
  58. Span &operator=(const Span &) = delete;
  59. Span &operator=(Span &&) = delete;
  60. /**
  61. * Sets an attribute on the Span (ABI).
  62. *
  63. * If the Span previously contained a mapping for the key,
  64. * the old value is replaced.
  65. *
  66. * See comments about sampling in @ref opentelemetry::trace::Span
  67. */
  68. virtual void SetAttribute(nostd::string_view key,
  69. const common::AttributeValue &value) noexcept = 0;
  70. // Adds an event to the Span.
  71. virtual void AddEvent(nostd::string_view name) noexcept = 0;
  72. // Adds an event to the Span, with a custom timestamp.
  73. virtual void AddEvent(nostd::string_view name, common::SystemTimestamp timestamp) noexcept = 0;
  74. // Adds an event to the Span, with a custom timestamp, and attributes.
  75. virtual void AddEvent(nostd::string_view name,
  76. common::SystemTimestamp timestamp,
  77. const common::KeyValueIterable &attributes) noexcept = 0;
  78. virtual void AddEvent(nostd::string_view name,
  79. const common::KeyValueIterable &attributes) noexcept
  80. {
  81. this->AddEvent(name, std::chrono::system_clock::now(), attributes);
  82. }
  83. template <class T,
  84. nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
  85. void AddEvent(nostd::string_view name,
  86. common::SystemTimestamp timestamp,
  87. const T &attributes) noexcept
  88. {
  89. this->AddEvent(name, timestamp, common::KeyValueIterableView<T>{attributes});
  90. }
  91. template <class T,
  92. nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
  93. void AddEvent(nostd::string_view name, const T &attributes) noexcept
  94. {
  95. this->AddEvent(name, common::KeyValueIterableView<T>{attributes});
  96. }
  97. void AddEvent(nostd::string_view name,
  98. common::SystemTimestamp timestamp,
  99. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  100. attributes) noexcept
  101. {
  102. this->AddEvent(name, timestamp,
  103. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  104. attributes.begin(), attributes.end()});
  105. }
  106. void AddEvent(nostd::string_view name,
  107. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  108. attributes) noexcept
  109. {
  110. this->AddEvent(name, std::chrono::system_clock::now(),
  111. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  112. attributes.begin(), attributes.end()});
  113. }
  114. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  115. /**
  116. * Add link (ABI).
  117. *
  118. * See comments about sampling in @ref opentelemetry::trace::Span
  119. *
  120. * @since ABI_VERSION 2
  121. */
  122. virtual void AddLink(const SpanContext &target,
  123. const common::KeyValueIterable &attrs) noexcept = 0;
  124. /**
  125. * Add links (ABI).
  126. *
  127. * See comments about sampling in @ref opentelemetry::trace::Span
  128. *
  129. * @since ABI_VERSION 2
  130. */
  131. virtual void AddLinks(const SpanContextKeyValueIterable &links) noexcept = 0;
  132. /**
  133. * Add link (API helper).
  134. *
  135. * See comments about sampling in @ref opentelemetry::trace::Span
  136. *
  137. * @since ABI_VERSION 2
  138. */
  139. template <class U,
  140. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  141. void AddLink(const SpanContext &target, const U &attrs)
  142. {
  143. common::KeyValueIterableView<U> view(attrs);
  144. this->AddLink(target, view);
  145. }
  146. /**
  147. * Add link (API helper).
  148. *
  149. * See comments about sampling in @ref opentelemetry::trace::Span
  150. *
  151. * @since ABI_VERSION 2
  152. */
  153. void AddLink(const SpanContext &target,
  154. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attrs)
  155. {
  156. /* Build a container from std::initializer_list. */
  157. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> container{
  158. attrs.begin(), attrs.end()};
  159. /* Build a view on the container. */
  160. common::KeyValueIterableView<
  161. nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
  162. view(container);
  163. return this->AddLink(target, view);
  164. }
  165. /**
  166. * Add links (API helper).
  167. *
  168. * See comments about sampling in @ref opentelemetry::trace::Span
  169. *
  170. * @since ABI_VERSION 2
  171. */
  172. template <class U, nostd::enable_if_t<detail::is_span_context_kv_iterable<U>::value> * = nullptr>
  173. void AddLinks(const U &links)
  174. {
  175. SpanContextKeyValueIterableView<U> view(links);
  176. this->AddLinks(view);
  177. }
  178. /**
  179. * Add links (API helper).
  180. *
  181. * See comments about sampling in @ref opentelemetry::trace::Span
  182. *
  183. * @since ABI_VERSION 2
  184. */
  185. void AddLinks(
  186. std::initializer_list<
  187. std::pair<SpanContext,
  188. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>
  189. links)
  190. {
  191. /* Build a container from std::initializer_list. */
  192. nostd::span<const std::pair<
  193. SpanContext, std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>
  194. container{links.begin(), links.end()};
  195. /* Build a view on the container. */
  196. SpanContextKeyValueIterableView<nostd::span<const std::pair<
  197. SpanContext, std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>>
  198. view(container);
  199. return this->AddLinks(view);
  200. }
  201. #endif /* OPENTELEMETRY_ABI_VERSION_NO */
  202. // Sets the status of the span. The default status is Unset. Only the value of
  203. // the last call will be
  204. // recorded, and implementations are free to ignore previous calls.
  205. virtual void SetStatus(StatusCode code, nostd::string_view description = "") noexcept = 0;
  206. // Updates the name of the Span. If used, this will override the name provided
  207. // during creation.
  208. virtual void UpdateName(nostd::string_view name) noexcept = 0;
  209. /**
  210. * Mark the end of the Span.
  211. * Only the timing of the first End call for a given Span will be recorded,
  212. * and implementations are free to ignore all further calls.
  213. * @param options can be used to manually define span properties like the end
  214. * timestamp
  215. */
  216. virtual void End(const trace::EndSpanOptions &options = {}) noexcept = 0;
  217. virtual trace::SpanContext GetContext() const noexcept = 0;
  218. // Returns true if this Span is recording tracing events (e.g. SetAttribute,
  219. // AddEvent).
  220. virtual bool IsRecording() const noexcept = 0;
  221. };
  222. } // namespace trace
  223. OPENTELEMETRY_END_NAMESPACE