text_map_propagator.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/function_ref.h"
  6. #include "opentelemetry/nostd/string_view.h"
  7. #include "opentelemetry/version.h"
  8. OPENTELEMETRY_BEGIN_NAMESPACE
  9. namespace context
  10. {
  11. namespace propagation
  12. {
  13. // TextMapCarrier is the storage medium used by TextMapPropagator.
  14. class TextMapCarrier
  15. {
  16. public:
  17. // returns the value associated with the passed key.
  18. virtual nostd::string_view Get(nostd::string_view key) const noexcept = 0;
  19. // stores the key-value pair.
  20. virtual void Set(nostd::string_view key, nostd::string_view value) noexcept = 0;
  21. /* list of all the keys in the carrier.
  22. By default, it returns true without invoking callback */
  23. virtual bool Keys(nostd::function_ref<bool(nostd::string_view)> /* callback */) const noexcept
  24. {
  25. return true;
  26. }
  27. virtual ~TextMapCarrier() = default;
  28. };
  29. // The TextMapPropagator class provides an interface that enables extracting and injecting
  30. // context into carriers that travel in-band across process boundaries. HTTP frameworks and clients
  31. // can integrate with TextMapPropagator by providing the object containing the
  32. // headers, and a getter and setter function for the extraction and
  33. // injection of values, respectively.
  34. class TextMapPropagator
  35. {
  36. public:
  37. // Returns the context that is stored in the carrier with the TextMapCarrier as extractor.
  38. virtual context::Context Extract(const TextMapCarrier &carrier,
  39. context::Context &context) noexcept = 0;
  40. // Sets the context for carrier with self defined rules.
  41. virtual void Inject(TextMapCarrier &carrier, const context::Context &context) noexcept = 0;
  42. // Gets the fields set in the carrier by the `inject` method
  43. virtual bool Fields(nostd::function_ref<bool(nostd::string_view)> callback) const noexcept = 0;
  44. virtual ~TextMapPropagator() = default;
  45. };
  46. } // namespace propagation
  47. } // namespace context
  48. OPENTELEMETRY_END_NAMESPACE