Line data Source code
1 : #pragma once 2 : 3 : #include <functional> 4 : #include <string> 5 : 6 : #include "envoy/common/optref.h" 7 : #include "envoy/common/pure.h" 8 : #include "envoy/http/header_map.h" 9 : 10 : #include "absl/strings/string_view.h" 11 : #include "absl/types/optional.h" 12 : 13 : namespace Envoy { 14 : namespace Tracing { 15 : 16 : class TraceContextHandler; 17 : 18 : /** 19 : * Protocol-independent abstraction for traceable stream. It hides the differences between different 20 : * protocol and provides tracer driver with common methods for obtaining and setting the tracing 21 : * context. 22 : */ 23 : class TraceContext { 24 : public: 25 251 : virtual ~TraceContext() = default; 26 : 27 : using IterateCallback = std::function<bool(absl::string_view key, absl::string_view val)>; 28 : 29 : /** 30 : * Get context protocol. 31 : * 32 : * @return A string view representing the protocol of the traceable stream behind the context. 33 : */ 34 : virtual absl::string_view protocol() const PURE; 35 : 36 : /** 37 : * Get context host. 38 : * 39 : * @return The host of traceable stream. It typically should be domain, VIP, or service name that 40 : * used to represents target service instances. 41 : */ 42 : virtual absl::string_view host() const PURE; 43 : 44 : /** 45 : * Get context path. 46 : * 47 : * @return The path of traceable stream. The content and meaning of path are determined by 48 : * specific protocol itself. 49 : */ 50 : virtual absl::string_view path() const PURE; 51 : 52 : /** 53 : * Get context method. 54 : * 55 : * @return The method of traceable stream. The content and meaning of method are determined by 56 : * specific protocol itself. 57 : */ 58 : virtual absl::string_view method() const PURE; 59 : 60 : /** 61 : * Iterate over all context entry. 62 : * 63 : * @param callback supplies the iteration callback. 64 : */ 65 : virtual void forEach(IterateCallback callback) const PURE; 66 : 67 : /** 68 : * Get tracing context value by key. 69 : * 70 : * @param key The context key of string view type. 71 : * @return The optional context value of string_view type. 72 : */ 73 : virtual absl::optional<absl::string_view> get(absl::string_view key) const PURE; 74 : 75 : /** 76 : * Set new tracing context key/value pair. 77 : * 78 : * @param key The context key of string view type. 79 : * @param val The context value of string view type. 80 : */ 81 : virtual void set(absl::string_view key, absl::string_view val) PURE; 82 : 83 : /** 84 : * Removes the following key and its associated values from the tracing 85 : * context. 86 : * 87 : * @param key The key to remove if it exists. 88 : */ 89 : virtual void remove(absl::string_view key) PURE; 90 : 91 : private: 92 : friend class TraceContextHandler; 93 : 94 : /** 95 : * Optional HTTP request headers map. This is valid for HTTP protocol or any protocol that 96 : * that provides HTTP request headers. 97 : */ 98 0 : virtual OptRef<Http::RequestHeaderMap> requestHeaders() { return {}; }; 99 0 : virtual OptRef<const Http::RequestHeaderMap> requestHeaders() const { return {}; }; 100 : }; 101 : 102 : } // namespace Tracing 103 : } // namespace Envoy