|
| 1 | +// package datadog.trace.core.baggage; |
| 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.Arrays; |
| 13 | +// import java.util.Collections; |
| 14 | +// import java.util.HashMap; |
| 15 | +// import java.util.HashSet; |
| 16 | +// import java.util.Map; |
| 17 | +// import java.util.Set; |
| 18 | +// import java.util.function.Supplier; |
| 19 | +// |
| 20 | +// import datadog.trace.core.propagation.ContextInterpreter; |
| 21 | +// import datadog.trace.core.propagation.HttpCodec; |
| 22 | +// import datadog.trace.core.propagation.TagContextExtractor; |
| 23 | +// import org.slf4j.Logger; |
| 24 | +// import org.slf4j.LoggerFactory; |
| 25 | +// |
| 26 | +/// ** A codec designed for HTTP transport via headers using Datadog headers */ |
| 27 | +// class BaggageHttpCodec { |
| 28 | +// private static final Logger log = LoggerFactory.getLogger(BaggageHttpCodec.class); |
| 29 | +// |
| 30 | +// static final String BAGGAGE_KEY = "baggage"; |
| 31 | +// private static final int MAX_CHARACTER_SIZE = 4; |
| 32 | +// |
| 33 | +// private static final Set<Character> UNSAFE_CHARACTERS_KEY = |
| 34 | +// new HashSet<>( |
| 35 | +// Arrays.asList( |
| 36 | +// '\"', ',', ';', '\\', '(', ')', '/', ':', '<', '=', '>', '?', '@', '[', ']', '{', |
| 37 | +// '}')); |
| 38 | +// private static final Set<Character> UNSAFE_CHARACTERS_VALUE = |
| 39 | +// new HashSet<>(Arrays.asList('\"', ',', ';', '\\')); |
| 40 | +// |
| 41 | +// private BaggageHttpCodec() { |
| 42 | +// // This class should not be created. This also makes code coverage checks happy. |
| 43 | +// } |
| 44 | +// |
| 45 | +// public static HttpCodec.Injector newInjector(Map<String, String> invertedBaggageMapping) { |
| 46 | +// return new Injector(invertedBaggageMapping); |
| 47 | +// } |
| 48 | +// |
| 49 | +// private static class Injector implements HttpCodec.Injector { |
| 50 | +// |
| 51 | +// private final Map<String, String> invertedBaggageMapping; |
| 52 | +// |
| 53 | +// public Injector(Map<String, String> invertedBaggageMapping) { |
| 54 | +// assert invertedBaggageMapping != null; |
| 55 | +// this.invertedBaggageMapping = invertedBaggageMapping; |
| 56 | +// } |
| 57 | +// |
| 58 | +// private int encodeKey(String key, StringBuilder builder) { |
| 59 | +// return encode(key, builder, UNSAFE_CHARACTERS_KEY); |
| 60 | +// } |
| 61 | +// |
| 62 | +// private int encodeValue(String key, StringBuilder builder) { |
| 63 | +// return encode(key, builder, UNSAFE_CHARACTERS_VALUE); |
| 64 | +// } |
| 65 | +// |
| 66 | +// private int encode(String input, StringBuilder builder, Set<Character> UNSAFE_CHARACTERS) { |
| 67 | +// int size = 0; |
| 68 | +// for (int i = 0; i < input.length(); i++) { |
| 69 | +// char c = input.charAt(i); |
| 70 | +// if (UNSAFE_CHARACTERS.contains(c) || c > 126 || c <= 32) { // encode character |
| 71 | +// byte[] bytes = |
| 72 | +// Character.toString(c) |
| 73 | +// .getBytes(StandardCharsets.UTF_8); // not most efficient but what URLEncoder |
| 74 | +// does |
| 75 | +// for (byte b : bytes) { |
| 76 | +// builder.append(String.format("%%%02X", b & 0xFF)); |
| 77 | +// size += 1; |
| 78 | +// } |
| 79 | +// } else { |
| 80 | +// builder.append(c); |
| 81 | +// size += 1; |
| 82 | +// } |
| 83 | +// } |
| 84 | +// return size; |
| 85 | +// } |
| 86 | +// |
| 87 | +// @Override |
| 88 | +// public <C> void inject( |
| 89 | +// final DDSpanContext context, final C carrier, final AgentPropagation.Setter<C> setter) { |
| 90 | +// Config config = Config.get(); |
| 91 | +// |
| 92 | +// StringBuilder baggageText = new StringBuilder(); |
| 93 | +// int processedBaggage = 0; |
| 94 | +// int currentBytes = 0; |
| 95 | +// int maxItems = config.getTraceBaggageMaxItems(); |
| 96 | +// int maxBytes = config.getTraceBaggageMaxBytes(); |
| 97 | +// for (final Map.Entry<String, String> entry : context.baggageItems()) { |
| 98 | +// if (processedBaggage >= maxItems) { |
| 99 | +// break; |
| 100 | +// } |
| 101 | +// int additionalCharacters = 1; // accounting for potential comma and colon |
| 102 | +// if (processedBaggage != 0) { |
| 103 | +// additionalCharacters = 2; // allocating space for comma |
| 104 | +// } |
| 105 | +// |
| 106 | +// int byteSize = 1; // default include size of '=' |
| 107 | +// if (additionalCharacters == 2) { |
| 108 | +// baggageText.append(','); |
| 109 | +// byteSize += 1; |
| 110 | +// } |
| 111 | +// |
| 112 | +// byteSize += encodeKey(entry.getKey(), baggageText); |
| 113 | +// baggageText.append('='); |
| 114 | +// byteSize += encodeValue(entry.getValue(), baggageText); |
| 115 | +// |
| 116 | +// if (currentBytes + byteSize > maxBytes) { |
| 117 | +// baggageText.setLength(currentBytes); |
| 118 | +// break; |
| 119 | +// } |
| 120 | +// currentBytes += byteSize; |
| 121 | +// processedBaggage++; |
| 122 | +// } |
| 123 | +// |
| 124 | +// setter.set(carrier, BAGGAGE_KEY, baggageText.toString()); |
| 125 | +// } |
| 126 | +// } |
| 127 | +// |
| 128 | +// public static HttpCodec.Extractor newExtractor( |
| 129 | +// Config config, Supplier<TraceConfig> traceConfigSupplier) { |
| 130 | +// return new TagContextExtractor( |
| 131 | +// traceConfigSupplier, () -> new BaggageContextInterpreter(config)); |
| 132 | +// } |
| 133 | +// |
| 134 | +// private static class BaggageContextInterpreter extends ContextInterpreter { |
| 135 | +// |
| 136 | +// private BaggageContextInterpreter(Config config) { |
| 137 | +// super(config); |
| 138 | +// } |
| 139 | +// |
| 140 | +// @Override |
| 141 | +// public TracePropagationStyle style() { |
| 142 | +// return BAGGAGE; |
| 143 | +// } |
| 144 | +// |
| 145 | +// private Map<String, String> parseBaggageHeaders(String input) { |
| 146 | +// Map<String, String> baggage = new HashMap<>(); |
| 147 | +// char keyValueSeparator = '='; |
| 148 | +// char pairSeparator = ','; |
| 149 | +// int start = 0; |
| 150 | +// |
| 151 | +// int pairSeparatorInd = input.indexOf(pairSeparator); |
| 152 | +// pairSeparatorInd = pairSeparatorInd == -1 ? input.length() : pairSeparatorInd; |
| 153 | +// int kvSeparatorInd = input.indexOf(keyValueSeparator); |
| 154 | +// while (kvSeparatorInd != -1) { |
| 155 | +// int end = pairSeparatorInd; |
| 156 | +// if (kvSeparatorInd > end) { // value is missing |
| 157 | +// return Collections.emptyMap(); |
| 158 | +// } |
| 159 | +// String key = HttpCodec.decode(input.substring(start, kvSeparatorInd).trim()); |
| 160 | +// String value = HttpCodec.decode(input.substring(kvSeparatorInd + 1, end).trim()); |
| 161 | +// if (key.isEmpty() || value.isEmpty()) { |
| 162 | +// return Collections.emptyMap(); |
| 163 | +// } |
| 164 | +// baggage.put(key, value); |
| 165 | +// |
| 166 | +// kvSeparatorInd = input.indexOf(keyValueSeparator, pairSeparatorInd + 1); |
| 167 | +// pairSeparatorInd = input.indexOf(pairSeparator, pairSeparatorInd + 1); |
| 168 | +// pairSeparatorInd = pairSeparatorInd == -1 ? input.length() : pairSeparatorInd; |
| 169 | +// start = end + 1; |
| 170 | +// } |
| 171 | +// return baggage; |
| 172 | +// } |
| 173 | +// |
| 174 | +// @Override |
| 175 | +// public boolean accept(String key, String value) { |
| 176 | +// if (null == key || key.isEmpty()) { |
| 177 | +// return true; |
| 178 | +// } |
| 179 | +// if (LOG_EXTRACT_HEADER_NAMES) { |
| 180 | +// log.debug("Header: {}", key); |
| 181 | +// } |
| 182 | +// |
| 183 | +// if (key.equalsIgnoreCase(BAGGAGE_KEY)) { // Only process tags that are relevant to baggage |
| 184 | +// baggage = parseBaggageHeaders(value); |
| 185 | +// } |
| 186 | +// return true; |
| 187 | +// } |
| 188 | +// |
| 189 | +// @Override |
| 190 | +// protected TagContext build() { |
| 191 | +// return super.build(); |
| 192 | +// } |
| 193 | +// } |
| 194 | +// } |
0 commit comments