scope_test.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <string>
  5. #include "opentelemetry/context/context_value.h"
  6. #include "opentelemetry/context/runtime_context.h"
  7. #include "opentelemetry/nostd/shared_ptr.h"
  8. #include "opentelemetry/nostd/variant.h"
  9. #include "opentelemetry/trace/noop.h"
  10. #include "opentelemetry/trace/scope.h"
  11. #include "opentelemetry/trace/span.h"
  12. #include "opentelemetry/trace/span_metadata.h"
  13. using opentelemetry::trace::kSpanKey;
  14. using opentelemetry::trace::NoopSpan;
  15. using opentelemetry::trace::Scope;
  16. using opentelemetry::trace::Span;
  17. namespace nostd = opentelemetry::nostd;
  18. namespace context = opentelemetry::context;
  19. TEST(ScopeTest, Construct)
  20. {
  21. nostd::shared_ptr<Span> span(new NoopSpan(nullptr));
  22. Scope scope(span);
  23. context::ContextValue active_span_value = context::RuntimeContext::GetValue(kSpanKey);
  24. ASSERT_TRUE(nostd::holds_alternative<nostd::shared_ptr<Span>>(active_span_value));
  25. auto active_span = nostd::get<nostd::shared_ptr<Span>>(active_span_value);
  26. ASSERT_EQ(active_span, span);
  27. }
  28. TEST(ScopeTest, Destruct)
  29. {
  30. nostd::shared_ptr<Span> span(new NoopSpan(nullptr));
  31. Scope scope(span);
  32. {
  33. nostd::shared_ptr<Span> span_nested(new NoopSpan(nullptr));
  34. Scope scope_nested(span_nested);
  35. context::ContextValue active_span_value = context::RuntimeContext::GetValue(kSpanKey);
  36. ASSERT_TRUE(nostd::holds_alternative<nostd::shared_ptr<Span>>(active_span_value));
  37. auto active_span = nostd::get<nostd::shared_ptr<Span>>(active_span_value);
  38. ASSERT_EQ(active_span, span_nested);
  39. }
  40. context::ContextValue active_span_value = context::RuntimeContext::GetValue(kSpanKey);
  41. ASSERT_TRUE(nostd::holds_alternative<nostd::shared_ptr<Span>>(active_span_value));
  42. auto active_span = nostd::get<nostd::shared_ptr<Span>>(active_span_value);
  43. ASSERT_EQ(active_span, span);
  44. }