baggage_context.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/baggage/baggage.h"
  5. #include "opentelemetry/context/context.h"
  6. #include "opentelemetry/nostd/shared_ptr.h"
  7. #include "opentelemetry/version.h"
  8. OPENTELEMETRY_BEGIN_NAMESPACE
  9. namespace baggage
  10. {
  11. static const std::string kBaggageHeader = "baggage";
  12. inline nostd::shared_ptr<Baggage> GetBaggage(const context::Context &context) noexcept
  13. {
  14. context::ContextValue context_value = context.GetValue(kBaggageHeader);
  15. if (nostd::holds_alternative<nostd::shared_ptr<Baggage>>(context_value))
  16. {
  17. return nostd::get<nostd::shared_ptr<Baggage>>(context_value);
  18. }
  19. static nostd::shared_ptr<Baggage> empty_baggage{new Baggage()};
  20. return empty_baggage;
  21. }
  22. inline context::Context SetBaggage(context::Context &context,
  23. const nostd::shared_ptr<Baggage> &baggage) noexcept
  24. {
  25. return context.SetValue(kBaggageHeader, baggage);
  26. }
  27. } // namespace baggage
  28. OPENTELEMETRY_END_NAMESPACE