Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/http/header_map.h" 4 : #include "envoy/tracing/trace_context.h" 5 : 6 : namespace Envoy { 7 : namespace Tracing { 8 : 9 : using InlineHandle = Http::CustomInlineHeaderRegistry::Handle< 10 : Http::CustomInlineHeaderRegistry::Type::RequestHeaders>; 11 : 12 : /** 13 : * A handler class for trace context. This class could be used to set, get and erase the key/value 14 : * pair in the trace context. This handler is optimized for HTTP to support reference semantics and 15 : * inline header. If HTTP header is used as the trace context and the key is registered in the 16 : * custom inline header registry, then inline handle will be used to improve the performance. 17 : */ 18 : class TraceContextHandler { 19 : public: 20 : /** 21 : * Construct a handler with the given key. Note that the key will be lowercase and copied. 22 : * @param key the key of the handler. 23 : */ 24 : TraceContextHandler(absl::string_view key); 25 : 26 : /** 27 : * Get the key of the handler. 28 : * @return absl::string_view the key of the handler. Note that the key is lowercase. 29 : */ 30 0 : const Http::LowerCaseString& key() const { return key_; } 31 : 32 : /** 33 : * Erase the key/value pair from the trace context. 34 : * @param trace_context the trace context to erase the key/value pair. 35 : */ 36 : void remove(TraceContext& trace_context) const; 37 : 38 : /** 39 : * Get the value from the trace context by the key. 40 : * @param trace_context the trace context to get the value. 41 : * @return absl::optional<absl::string_view> the value of the key. If the key is not found, then 42 : * absl::nullopt will be returned. 43 : */ 44 : absl::optional<absl::string_view> get(const TraceContext& trace_context) const; 45 : 46 : /* 47 : * Set the key/value pair in the trace context. 48 : * @param trace_context the trace context to set the key/value pair. 49 : * @param value the value to set. 50 : */ 51 : void set(TraceContext& trace_context, absl::string_view value) const; 52 : 53 : /** 54 : * Set the key/value pair in the trace context. This method should only be used when the handler 55 : * has longer lifetime than current stream. 56 : * @param trace_context the trace context to set the key/value pair. 57 : * @param value the value to set. 58 : */ 59 : void setRefKey(TraceContext& trace_context, absl::string_view value) const; 60 : 61 : /** 62 : * Set the key/value pair in the trace context. This method should only be used when both the 63 : * handler and the value have longer lifetime than current stream. 64 : * @param trace_context the trace context to set the key/value pair. 65 : * @param value the value to set. 66 : */ 67 : void setRef(TraceContext& trace_context, absl::string_view value) const; 68 : 69 : private: 70 : const Http::LowerCaseString key_; 71 : absl::optional<InlineHandle> handle_; 72 : }; 73 : 74 : } // namespace Tracing 75 : } // namespace Envoy