scope.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/context/context.h"
  5. #include "opentelemetry/context/runtime_context.h"
  6. #include "opentelemetry/nostd/shared_ptr.h"
  7. #include "opentelemetry/nostd/unique_ptr.h"
  8. #include "opentelemetry/trace/span.h"
  9. #include "opentelemetry/trace/span_metadata.h"
  10. #include "opentelemetry/version.h"
  11. OPENTELEMETRY_BEGIN_NAMESPACE
  12. namespace trace
  13. {
  14. /**
  15. * Controls how long a span is active.
  16. *
  17. * On creation of the Scope object, the given span is set to the currently
  18. * active span. On destruction, the given span is ended and the previously
  19. * active span will be the currently active span again.
  20. */
  21. class Scope final
  22. {
  23. public:
  24. /**
  25. * Initialize a new scope.
  26. * @param span the given span will be set as the currently active span.
  27. */
  28. Scope(const nostd::shared_ptr<Span> &span) noexcept
  29. : token_(context::RuntimeContext::Attach(
  30. context::RuntimeContext::GetCurrent().SetValue(kSpanKey, span)))
  31. {}
  32. private:
  33. nostd::unique_ptr<context::Token> token_;
  34. };
  35. } // namespace trace
  36. OPENTELEMETRY_END_NAMESPACE