global_propagator.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <mutex>
  5. #include "opentelemetry/context/propagation/noop_propagator.h"
  6. #include "opentelemetry/common/macros.h"
  7. #include "opentelemetry/common/spin_lock_mutex.h"
  8. #include "opentelemetry/nostd/shared_ptr.h"
  9. #include "opentelemetry/version.h"
  10. OPENTELEMETRY_BEGIN_NAMESPACE
  11. namespace context
  12. {
  13. namespace propagation
  14. {
  15. class TextMapPropagator;
  16. /* Stores the singleton TextMapPropagator */
  17. class OPENTELEMETRY_EXPORT GlobalTextMapPropagator
  18. {
  19. public:
  20. static nostd::shared_ptr<TextMapPropagator> GetGlobalPropagator() noexcept
  21. {
  22. std::lock_guard<common::SpinLockMutex> guard(GetLock());
  23. return nostd::shared_ptr<TextMapPropagator>(GetPropagator());
  24. }
  25. static void SetGlobalPropagator(const nostd::shared_ptr<TextMapPropagator> &prop) noexcept
  26. {
  27. std::lock_guard<common::SpinLockMutex> guard(GetLock());
  28. GetPropagator() = prop;
  29. }
  30. private:
  31. OPENTELEMETRY_API_SINGLETON static nostd::shared_ptr<TextMapPropagator> &GetPropagator() noexcept
  32. {
  33. static nostd::shared_ptr<TextMapPropagator> propagator(new NoOpPropagator());
  34. return propagator;
  35. }
  36. OPENTELEMETRY_API_SINGLETON static common::SpinLockMutex &GetLock() noexcept
  37. {
  38. static common::SpinLockMutex lock;
  39. return lock;
  40. }
  41. };
  42. } // namespace propagation
  43. } // namespace context
  44. OPENTELEMETRY_END_NAMESPACE