span_context.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <stdint.h>
  5. #include <utility>
  6. #include "opentelemetry/nostd/shared_ptr.h"
  7. #include "opentelemetry/trace/span_id.h"
  8. #include "opentelemetry/trace/trace_flags.h"
  9. #include "opentelemetry/trace/trace_id.h"
  10. #include "opentelemetry/trace/trace_state.h"
  11. #include "opentelemetry/version.h"
  12. OPENTELEMETRY_BEGIN_NAMESPACE
  13. namespace trace
  14. {
  15. /* SpanContext contains the state that must propagate to child Spans and across
  16. * process boundaries. It contains TraceId and SpanId, TraceFlags, TraceState
  17. * and whether it was propagated from a remote parent.
  18. */
  19. class SpanContext final
  20. {
  21. public:
  22. /* A temporary constructor for an invalid SpanContext.
  23. * Trace id and span id are set to invalid (all zeros).
  24. *
  25. * @param sampled_flag a required parameter specifying if child spans should be
  26. * sampled
  27. * @param is_remote true if this context was propagated from a remote parent.
  28. */
  29. SpanContext(bool sampled_flag, bool is_remote) noexcept
  30. : trace_id_(),
  31. span_id_(),
  32. trace_flags_(trace::TraceFlags(static_cast<uint8_t>(sampled_flag))),
  33. is_remote_(is_remote),
  34. trace_state_(TraceState::GetDefault())
  35. {}
  36. SpanContext(TraceId trace_id,
  37. SpanId span_id,
  38. TraceFlags trace_flags,
  39. bool is_remote,
  40. nostd::shared_ptr<TraceState> trace_state = TraceState::GetDefault()) noexcept
  41. : trace_id_(trace_id),
  42. span_id_(span_id),
  43. trace_flags_(trace_flags),
  44. is_remote_(is_remote),
  45. trace_state_(std::move(trace_state))
  46. {}
  47. SpanContext(const SpanContext &ctx) = default;
  48. // @returns whether this context is valid
  49. bool IsValid() const noexcept { return trace_id_.IsValid() && span_id_.IsValid(); }
  50. // @returns the trace_flags associated with this span_context
  51. const trace::TraceFlags &trace_flags() const noexcept { return trace_flags_; }
  52. // @returns the trace_id associated with this span_context
  53. const trace::TraceId &trace_id() const noexcept { return trace_id_; }
  54. // @returns the span_id associated with this span_context
  55. const trace::SpanId &span_id() const noexcept { return span_id_; }
  56. // @returns the trace_state associated with this span_context
  57. const nostd::shared_ptr<trace::TraceState> &trace_state() const noexcept { return trace_state_; }
  58. /*
  59. * @param that SpanContext for comparing.
  60. * @return true if `that` equals the current SpanContext.
  61. * N.B. trace_state is ignored for the comparison.
  62. */
  63. bool operator==(const SpanContext &that) const noexcept
  64. {
  65. return trace_id() == that.trace_id() && span_id() == that.span_id() &&
  66. trace_flags() == that.trace_flags();
  67. }
  68. SpanContext &operator=(const SpanContext &ctx) = default;
  69. bool IsRemote() const noexcept { return is_remote_; }
  70. static SpanContext GetInvalid() noexcept { return SpanContext(false, false); }
  71. bool IsSampled() const noexcept { return trace_flags_.IsSampled(); }
  72. private:
  73. trace::TraceId trace_id_;
  74. trace::SpanId span_id_;
  75. trace::TraceFlags trace_flags_;
  76. bool is_remote_;
  77. nostd::shared_ptr<trace::TraceState> trace_state_;
  78. };
  79. } // namespace trace
  80. OPENTELEMETRY_END_NAMESPACE