context.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/context/context.h"
  5. #include "opentelemetry/nostd/shared_ptr.h"
  6. #include "opentelemetry/trace/default_span.h"
  7. #include "opentelemetry/version.h"
  8. OPENTELEMETRY_BEGIN_NAMESPACE
  9. namespace trace
  10. {
  11. // Get Span from explicit context
  12. inline nostd::shared_ptr<Span> GetSpan(const context::Context &context) noexcept
  13. {
  14. context::ContextValue span = context.GetValue(kSpanKey);
  15. if (nostd::holds_alternative<nostd::shared_ptr<Span>>(span))
  16. {
  17. return nostd::get<nostd::shared_ptr<Span>>(span);
  18. }
  19. return nostd::shared_ptr<Span>(new DefaultSpan(SpanContext::GetInvalid()));
  20. }
  21. inline bool IsRootSpan(const context::Context &context) noexcept
  22. {
  23. context::ContextValue is_root_span = context.GetValue(kIsRootSpanKey);
  24. if (nostd::holds_alternative<bool>(is_root_span))
  25. {
  26. return nostd::get<bool>(is_root_span);
  27. }
  28. return false;
  29. }
  30. // Set Span into explicit context
  31. inline context::Context SetSpan(context::Context &context,
  32. const nostd::shared_ptr<Span> &span) noexcept
  33. {
  34. return context.SetValue(kSpanKey, span);
  35. }
  36. } // namespace trace
  37. OPENTELEMETRY_END_NAMESPACE