Line data Source code
1 : #include "source/common/tracing/trace_context_impl.h" 2 : 3 : #include "source/common/http/header_map_impl.h" 4 : 5 : namespace Envoy { 6 : namespace Tracing { 7 : 8 0 : TraceContextHandler::TraceContextHandler(absl::string_view key) : key_(key) { 9 : // This will force the header map to be finalized in unit tests and do nothing in prod ( 10 : // where the header map is already finalized when the server is initializing). 11 0 : Http::TypedHeaderMapImpl<Http::RequestHeaderMap>::inlineHeadersSize(); 12 : 13 0 : handle_ = Http::CustomInlineHeaderRegistry::getInlineHeader< 14 0 : Http::CustomInlineHeaderRegistry::Type::RequestHeaders>(key_); 15 0 : } 16 : 17 0 : void TraceContextHandler::set(TraceContext& trace_context, absl::string_view value) const { 18 : // Will dynamic_cast be better? 19 0 : auto header_map = trace_context.requestHeaders(); 20 0 : if (!header_map.has_value()) { 21 0 : trace_context.set(key_, value); 22 0 : return; 23 0 : } 24 : 25 0 : if (handle_.has_value()) { 26 0 : header_map->setInline(handle_.value(), value); 27 0 : } else { 28 0 : header_map->setCopy(key_, value); 29 0 : } 30 0 : } 31 : 32 0 : void TraceContextHandler::setRefKey(TraceContext& trace_context, absl::string_view value) const { 33 0 : auto header_map = trace_context.requestHeaders(); 34 0 : if (!header_map.has_value()) { 35 0 : trace_context.set(key_, value); 36 0 : return; 37 0 : } 38 : 39 0 : if (handle_.has_value()) { 40 0 : header_map->setInline(handle_.value(), value); 41 0 : } else { 42 0 : header_map->setReferenceKey(key_, value); 43 0 : } 44 0 : } 45 : 46 0 : void TraceContextHandler::setRef(TraceContext& trace_context, absl::string_view value) const { 47 0 : auto header_map = trace_context.requestHeaders(); 48 0 : if (!header_map.has_value()) { 49 0 : trace_context.set(key_, value); 50 0 : return; 51 0 : } 52 : 53 0 : if (handle_.has_value()) { 54 0 : header_map->setReferenceInline(handle_.value(), value); 55 0 : } else { 56 0 : header_map->setReference(key_, value); 57 0 : } 58 0 : } 59 : 60 : absl::optional<absl::string_view> 61 0 : TraceContextHandler::get(const TraceContext& trace_context) const { 62 0 : auto header_map = trace_context.requestHeaders(); 63 0 : if (!header_map.has_value()) { 64 0 : return trace_context.get(key_); 65 0 : } 66 : 67 0 : if (handle_.has_value()) { 68 0 : auto* entry = header_map->getInline(handle_.value()); 69 0 : if (entry == nullptr) { 70 0 : return absl::nullopt; 71 0 : } 72 0 : return entry->value().getStringView(); 73 0 : } else { 74 0 : auto results = header_map->get(key_); 75 0 : if (results.empty()) { 76 0 : return absl::nullopt; 77 0 : } 78 0 : return results[0]->value().getStringView(); 79 0 : } 80 0 : } 81 : 82 0 : void TraceContextHandler::remove(TraceContext& trace_context) const { 83 0 : auto header_map = trace_context.requestHeaders(); 84 0 : if (!header_map.has_value()) { 85 0 : trace_context.remove(key_); 86 0 : return; 87 0 : } 88 : 89 0 : if (handle_.has_value()) { 90 0 : header_map->removeInline(handle_.value()); 91 0 : } else { 92 0 : header_map->remove(key_); 93 0 : } 94 0 : } 95 : 96 : } // namespace Tracing 97 : } // namespace Envoy