|
| 1 | +package datadog.trace.core.propagation; |
| 2 | + |
| 3 | +import static datadog.trace.api.TracePropagationStyle.BAGGAGE; |
| 4 | + |
| 5 | +import datadog.trace.api.Config; |
| 6 | +import datadog.trace.api.TraceConfig; |
| 7 | +import datadog.trace.api.TracePropagationStyle; |
| 8 | +import datadog.trace.bootstrap.instrumentation.api.AgentPropagation; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.TagContext; |
| 10 | +import datadog.trace.core.DDSpanContext; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.function.Supplier; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +/** A codec designed for HTTP transport via headers using Datadog headers */ |
| 20 | +class BaggageHttpCodec { |
| 21 | + private static final Logger log = LoggerFactory.getLogger(BaggageHttpCodec.class); |
| 22 | + |
| 23 | + static final String BAGGAGE_KEY = "baggage"; |
| 24 | + private static final int MAX_CHARACTER_SIZE = 4; |
| 25 | + |
| 26 | + private BaggageHttpCodec() { |
| 27 | + // This class should not be created. This also makes code coverage checks happy. |
| 28 | + } |
| 29 | + |
| 30 | + public static HttpCodec.Injector newInjector(Map<String, String> invertedBaggageMapping) { |
| 31 | + return new Injector(invertedBaggageMapping); |
| 32 | + } |
| 33 | + |
| 34 | + private static class Injector implements HttpCodec.Injector { |
| 35 | + |
| 36 | + private final Map<String, String> invertedBaggageMapping; |
| 37 | + |
| 38 | + public Injector(Map<String, String> invertedBaggageMapping) { |
| 39 | + assert invertedBaggageMapping != null; |
| 40 | + this.invertedBaggageMapping = invertedBaggageMapping; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public <C> void inject( |
| 45 | + final DDSpanContext context, final C carrier, final AgentPropagation.Setter<C> setter) { |
| 46 | + Config config = Config.get(); |
| 47 | + |
| 48 | + StringBuilder baggageText = new StringBuilder(); |
| 49 | + int processedBaggage = 0; |
| 50 | + int currentBytes = 0; |
| 51 | + int maxItems = config.getTraceBaggageMaxItems(); |
| 52 | + int maxBytes = config.getTraceBaggageMaxBytes(); |
| 53 | + int currentCharacters = 0; |
| 54 | + int maxSafeCharacters = maxBytes / MAX_CHARACTER_SIZE; |
| 55 | + for (final Map.Entry<String, String> entry : context.baggageItems()) { |
| 56 | + if (processedBaggage >= maxItems) { |
| 57 | + break; |
| 58 | + } |
| 59 | + StringBuilder currentText = new StringBuilder(); |
| 60 | + if (processedBaggage != 0) { |
| 61 | + currentText.append(','); |
| 62 | + } |
| 63 | + |
| 64 | + currentText.append(HttpCodec.encodeBaggage(entry.getKey())); |
| 65 | + currentText.append('='); |
| 66 | + currentText.append(HttpCodec.encodeBaggage(entry.getValue())); |
| 67 | + |
| 68 | + // worst case check |
| 69 | + if (currentCharacters + currentText.length() <= maxSafeCharacters) { |
| 70 | + currentCharacters += currentText.length(); |
| 71 | + } else { |
| 72 | + if (currentBytes |
| 73 | + == 0) { // special case to calculate byte size after surpassing worst-case number of |
| 74 | + // characters |
| 75 | + currentBytes = baggageText.toString().getBytes(StandardCharsets.UTF_8).length; |
| 76 | + } |
| 77 | + int byteSize = |
| 78 | + currentText |
| 79 | + .toString() |
| 80 | + .getBytes(StandardCharsets.UTF_8) |
| 81 | + .length; // find largest possible byte size for UTF encoded characters and only do |
| 82 | + // size checking after we hit this worst case scenario |
| 83 | + if (byteSize + currentBytes > maxBytes) { |
| 84 | + break; |
| 85 | + } |
| 86 | + currentBytes += byteSize; |
| 87 | + } |
| 88 | + baggageText.append(currentText); |
| 89 | + processedBaggage++; |
| 90 | + } |
| 91 | + |
| 92 | + setter.set(carrier, BAGGAGE_KEY, baggageText.toString()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public static HttpCodec.Extractor newExtractor( |
| 97 | + Config config, Supplier<TraceConfig> traceConfigSupplier) { |
| 98 | + return new TagContextExtractor( |
| 99 | + traceConfigSupplier, () -> new BaggageContextInterpreter(config)); |
| 100 | + } |
| 101 | + |
| 102 | + private static class BaggageContextInterpreter extends ContextInterpreter { |
| 103 | + |
| 104 | + private BaggageContextInterpreter(Config config) { |
| 105 | + super(config); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public TracePropagationStyle style() { |
| 110 | + return BAGGAGE; |
| 111 | + } |
| 112 | + |
| 113 | + private Map<String, String> parseBaggageHeaders(String input) { |
| 114 | + Map<String, String> baggage = new HashMap<>(); |
| 115 | + char keyValueSeparator = '='; |
| 116 | + char pairSeparator = ','; |
| 117 | + int start = 0; |
| 118 | + |
| 119 | + int pairSeparatorInd = input.indexOf(pairSeparator); |
| 120 | + pairSeparatorInd = pairSeparatorInd == -1 ? input.length() : pairSeparatorInd; |
| 121 | + int kvSeparatorInd = input.indexOf(keyValueSeparator); |
| 122 | + while (kvSeparatorInd != -1) { |
| 123 | + int end = pairSeparatorInd; |
| 124 | + if (kvSeparatorInd > end) { // value is missing |
| 125 | + return Collections.emptyMap(); |
| 126 | + } |
| 127 | + String key = HttpCodec.decode(input.substring(start, kvSeparatorInd).trim()); |
| 128 | + String value = HttpCodec.decode(input.substring(kvSeparatorInd + 1, end).trim()); |
| 129 | + if (key.isEmpty() || value.isEmpty()) { |
| 130 | + return Collections.emptyMap(); |
| 131 | + } |
| 132 | + baggage.put(key, value); |
| 133 | + |
| 134 | + kvSeparatorInd = input.indexOf(keyValueSeparator, pairSeparatorInd + 1); |
| 135 | + pairSeparatorInd = input.indexOf(pairSeparator, pairSeparatorInd + 1); |
| 136 | + pairSeparatorInd = pairSeparatorInd == -1 ? input.length() : pairSeparatorInd; |
| 137 | + start = end + 1; |
| 138 | + } |
| 139 | + return baggage; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean accept(String key, String value) { |
| 144 | + if (null == key || key.isEmpty()) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + if (LOG_EXTRACT_HEADER_NAMES) { |
| 148 | + log.debug("Header: {}", key); |
| 149 | + } |
| 150 | + |
| 151 | + if (key.equalsIgnoreCase(BAGGAGE_KEY)) { // Only process tags that are relevant to baggage |
| 152 | + baggage = parseBaggageHeaders(value); |
| 153 | + } |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + protected TagContext build() { |
| 159 | + return super.build(); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments