diff --git a/dd-trace-api/src/main/java/datadog/trace/api/internal/util/LongStringUtils.java b/dd-trace-api/src/main/java/datadog/trace/api/internal/util/LongStringUtils.java index 99fd19709d1..8b202c4bf2f 100644 --- a/dd-trace-api/src/main/java/datadog/trace/api/internal/util/LongStringUtils.java +++ b/dd-trace-api/src/main/java/datadog/trace/api/internal/util/LongStringUtils.java @@ -133,38 +133,170 @@ public static long parseUnsignedLong(String s) throws NumberFormatException { } else { if (len <= 18) { // Signed 64 bits max is 19 digits, so this always fits return Long.parseLong(s); - } else if (len > 20) { // Unsigned 64 bits max is 20 digits, so this always overflows - throw numberFormatOutOfLongRange(s); } - // Now do the first part and the last character - long first = 0; - int ok = 0; - for (int i = 0; i < len - 1; i++) { - char c = s.charAt(i); - int d = Character.digit(c, 10); - ok |= d; - first = first * 10 + d; - } - int last = Character.digit(s.charAt(len - 1), 10); - ok |= last; - if (ok < 0) { - throw new NumberFormatException("Illegal character in " + s); - } - if (Long.compareUnsigned(first, MAX_FIRST_PART) > 0) { - throw numberFormatOutOfLongRange(s); - } - long guard = first * 10; - long result = guard + last; - if (guard < 0 && result >= 0) { - throw numberFormatOutOfLongRange(s); + return parseLongUnsignedDigits(s, 0, len); + } + } else { + throw new NumberFormatException("Empty input string"); + } + } + + /** + * {@link #parseUnsignedLong(String)} for the chars {@code [start, end)} of {@code s}: the same + * result, or a {@code NumberFormatException}, as for the same chars in a {@code String}, without + * making one. + */ + public static long parseUnsignedLong(CharSequence s, int start, int end) + throws NumberFormatException { + if (s == null) { + throw new NumberFormatException("s can't be null"); + } + + int len = end - start; + if (len > 0) { + char firstChar = s.charAt(start); + if (firstChar == '-') { + throw new NumberFormatException( + String.format( + Locale.ROOT, + "Illegal leading minus sign on unsigned string %s.", + s.subSequence(start, end))); + } else { + if (len <= 18) { // Signed 64 bits max is 19 digits, so this always fits + return parseLong(s, start, end); } - return result; + return parseLongUnsignedDigits(s, start, end); } } else { throw new NumberFormatException("Empty input string"); } } + /** The 19 or more digits {@code [start, end)} of {@code s} as an unsigned long. */ + private static long parseLongUnsignedDigits(CharSequence s, int start, int end) { + int len = end - start; + if (len > 20) { // Unsigned 64 bits max is 20 digits, so this always overflows + throw numberFormatOutOfLongRange(s.subSequence(start, end)); + } + // Now do the first part and the last character + long first = 0; + int ok = 0; + for (int i = start; i < end - 1; i++) { + char c = s.charAt(i); + int d = Character.digit(c, 10); + ok |= d; + first = first * 10 + d; + } + int last = Character.digit(s.charAt(end - 1), 10); + ok |= last; + if (ok < 0) { + throw new NumberFormatException("Illegal character in " + s.subSequence(start, end)); + } + if (Long.compareUnsigned(first, MAX_FIRST_PART) > 0) { + throw numberFormatOutOfLongRange(s.subSequence(start, end)); + } + long guard = first * 10; + long result = guard + last; + if (guard < 0 && result >= 0) { + throw numberFormatOutOfLongRange(s.subSequence(start, end)); + } + return result; + } + + /** + * {@link Long#parseLong(String)} for the chars {@code [start, end)} of {@code s}, in base 10: the + * same result, or a {@code NumberFormatException}, as for the same chars in a {@code String}. + * Java 8 has no range-based {@code CharSequence} overload. + */ + public static long parseLong(CharSequence s, int start, int end) throws NumberFormatException { + if (s == null) { + throw new NumberFormatException("s can't be null"); + } + if (start >= end) { + throw new NumberFormatException("Empty input string"); + } + boolean negative = false; + long limit = -Long.MAX_VALUE; + int i = start; + char firstChar = s.charAt(i); + if (firstChar < '0') { // possible leading "+" or "-" + if (firstChar == '-') { + negative = true; + limit = Long.MIN_VALUE; + } else if (firstChar != '+') { + throw forInput(s, start, end); + } + if (end - start == 1) { // a lone "+" or "-" + throw forInput(s, start, end); + } + i++; + } + long multmin = limit / 10; + long result = 0; + while (i < end) { + // Accumulating negatively avoids surprises near MAX_VALUE + int digit = Character.digit(s.charAt(i++), 10); + if (digit < 0 || result < multmin) { + throw forInput(s, start, end); + } + result *= 10; + if (result < limit + digit) { + throw forInput(s, start, end); + } + result -= digit; + } + return negative ? result : -result; + } + + /** + * {@link Integer#parseInt(String)} for the chars {@code [start, end)} of {@code s}: the same + * result, or a {@code NumberFormatException}, as for the same chars in a {@code String}. Java 8 + * has no range-based {@code CharSequence} overload. + */ + public static int parseInt(CharSequence s, int start, int end) throws NumberFormatException { + if (s == null) { + throw new NumberFormatException("s can't be null"); + } + if (start >= end) { + throw new NumberFormatException("Empty input string"); + } + boolean negative = false; + int limit = -Integer.MAX_VALUE; + int i = start; + char firstChar = s.charAt(i); + if (firstChar < '0') { // possible leading "+" or "-" + if (firstChar == '-') { + negative = true; + limit = Integer.MIN_VALUE; + } else if (firstChar != '+') { + throw forInput(s, start, end); + } + if (end - start == 1) { // a lone "+" or "-" + throw forInput(s, start, end); + } + i++; + } + int multmin = limit / 10; + int result = 0; + while (i < end) { + // Accumulating negatively avoids surprises near MAX_VALUE + int digit = Character.digit(s.charAt(i++), 10); + if (digit < 0 || result < multmin) { + throw forInput(s, start, end); + } + result *= 10; + if (result < limit + digit) { + throw forInput(s, start, end); + } + result -= digit; + } + return negative ? result : -result; + } + + private static NumberFormatException forInput(CharSequence s, int start, int end) { + return new NumberFormatException("For input string: \"" + s.subSequence(start, end) + "\""); + } + /** * Creates a {@code NumberFormatException} with a consistent error message. * diff --git a/dd-trace-api/src/test/java/datadog/trace/api/internal/util/LongStringUtilsRangeTest.java b/dd-trace-api/src/test/java/datadog/trace/api/internal/util/LongStringUtilsRangeTest.java new file mode 100644 index 00000000000..fa2e26f2b9a --- /dev/null +++ b/dd-trace-api/src/test/java/datadog/trace/api/internal/util/LongStringUtilsRangeTest.java @@ -0,0 +1,132 @@ +package datadog.trace.api.internal.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Random; +import java.util.concurrent.Callable; +import org.junit.jupiter.api.Test; + +/** + * The range-based {@code CharSequence} parsers give the same result, or a {@code + * NumberFormatException}, as their {@code String} counterparts on the same chars. + */ +class LongStringUtilsRangeTest { + + private static final String[] EDGES = { + "", + "0", + "+", + "-", + "+0", + "-0", + "00", + "007", + "-1", + "+1", + "++1", + "1+", + " 1", + "1 ", + "a", + "12a4", + "٣", // ARABIC-INDIC DIGIT THREE, a digit to Character.digit + "1٠2", + "2147483647", + "2147483648", + "-2147483648", + "-2147483649", + "+2147483647", + "9223372036854775807", + "9223372036854775808", + "-9223372036854775808", + "-9223372036854775809", + "+9223372036854775807", + "999999999999999999", + "1000000000000000000", + "18446744073709551615", + "18446744073709551616", + "28446744073709551615", + "00000000000000000001", + "000000000000000000001", + "+8446744073709551615", + "1844674407370955161a", + "184467440737095516150", + }; + + /** The outcome of a parse: its value, or "NFE". */ + private static String outcome(Callable parse) { + try { + return String.valueOf(parse.call()); + } catch (NumberFormatException e) { + return "NFE"; + } catch (Exception e) { + throw new AssertionError(e); + } + } + + private static void assertSameOutcomes(String s) { + // embedded in a non-String CharSequence, with chars around it + StringBuilder padded = new StringBuilder("x9").append(s).append("7y"); + int start = 2; + int end = 2 + s.length(); + assertEquals( + outcome(() -> Long.parseLong(s)), + outcome(() -> LongStringUtils.parseLong(padded, start, end)), + () -> "parseLong '" + s + "'"); + assertEquals( + outcome(() -> Integer.parseInt(s)), + outcome(() -> LongStringUtils.parseInt(padded, start, end)), + () -> "parseInt '" + s + "'"); + assertEquals( + outcome(() -> LongStringUtils.parseUnsignedLong(s)), + outcome(() -> LongStringUtils.parseUnsignedLong(padded, start, end)), + () -> "parseUnsignedLong '" + s + "'"); + } + + @Test + void matchesTheStringParsersOnEdgeCases() { + for (String edge : EDGES) { + assertSameOutcomes(edge); + } + } + + @Test + void matchesTheStringParsersOnRandomInput() { + char[] alphabet = {'0', '1', '5', '9', '+', '-', 'a', ' ', '٣'}; + Random random = new Random(42); + for (int run = 0; run < 50_000; run++) { + int length = random.nextInt(22); + StringBuilder s = new StringBuilder(); + for (int i = 0; i < length; i++) { + // mostly digits, so values near every length boundary come up + s.append( + random.nextInt(6) == 0 + ? alphabet[random.nextInt(alphabet.length)] + : (char) ('0' + random.nextInt(10))); + } + assertSameOutcomes(s.toString()); + } + } + + @Test + void matchesOnRandomNumbersOfEveryMagnitude() { + Random random = new Random(7); + for (int run = 0; run < 20_000; run++) { + long value = random.nextLong() >>> random.nextInt(64); + assertSameOutcomes(Long.toUnsignedString(value)); + assertSameOutcomes(Long.toString(value)); + assertSameOutcomes(Long.toString(-value)); + assertSameOutcomes(Integer.toString((int) value)); + } + } + + @Test + void rejectsNullAndEmptyRanges() { + assertThrows(NumberFormatException.class, () -> LongStringUtils.parseLong(null, 0, 0)); + assertThrows(NumberFormatException.class, () -> LongStringUtils.parseInt(null, 0, 0)); + assertThrows(NumberFormatException.class, () -> LongStringUtils.parseUnsignedLong(null, 0, 0)); + assertThrows(NumberFormatException.class, () -> LongStringUtils.parseLong("123", 1, 1)); + assertThrows(NumberFormatException.class, () -> LongStringUtils.parseInt("123", 2, 1)); + } +} diff --git a/dd-trace-core/src/main/java/datadog/trace/core/propagation/ContextInterpreter.java b/dd-trace-core/src/main/java/datadog/trace/core/propagation/ContextInterpreter.java index 9094706e1d1..9478b239d3b 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/propagation/ContextInterpreter.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/propagation/ContextInterpreter.java @@ -206,6 +206,34 @@ public boolean accept(HeaderSchema header, String key, String value) { return acceptUnclassified(key, value); } + /** + * {@link #acceptTransient} for a style that doesn't parse views itself: a parse-only header that + * isn't the style's own (another style's id header) is skipped, without copying the view, unless + * the configuration names it (a request header tag, a baggage mapping, the client IP header), + * since the shared handling reads nothing else of it. Anything else is copied and accepted. + */ + @Override + public boolean acceptTransient(HeaderSchema header, String key, CharSequence value) { + if (header != null + && !LOG_EXTRACT_HEADER_NAMES + && header.isParseOnly() + && classify(header) == NOT_OWN_HEADER + && !isConfiguredFor(key)) { + return true; + } + return accept(header, key, value == null ? null : value.toString()); + } + + /** + * Whether the configuration names {@code key}: a request header tag, mapped baggage, client IP. + */ + private boolean isConfiguredFor(String key) { + String lowerCaseKey = toLowerCase(key); + return headerTags.containsKey(lowerCaseKey) + || baggageMapping.containsKey(lowerCaseKey) + || (customIpHeaderName != null && customIpHeaderName.equalsIgnoreCase(key)); + } + /** * How this style handles {@code header}: one of its own classifications (non-negative, for {@link * #acceptClassified}), {@link #NOT_OWN_HEADER} for a header it doesn't read itself, or {@link diff --git a/dd-trace-core/src/main/java/datadog/trace/core/propagation/DatadogHttpCodec.java b/dd-trace-core/src/main/java/datadog/trace/core/propagation/DatadogHttpCodec.java index 38dc2b0298b..07c1b31cc8b 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/propagation/DatadogHttpCodec.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/propagation/DatadogHttpCodec.java @@ -18,6 +18,7 @@ import datadog.trace.api.DDTraceId; import datadog.trace.api.TraceConfig; import datadog.trace.api.TracePropagationStyle; +import datadog.trace.api.internal.util.LongStringUtils; import datadog.trace.api.llmobs.LLMObsInternal; import datadog.trace.bootstrap.instrumentation.api.TagContext; import datadog.trace.core.DDSpanContext; @@ -228,6 +229,66 @@ protected boolean acceptClassified(int classification, String key, String value) return handleClassified(classification, key, value, null); } + /** + * Parses the trace id, parent id and sampling priority straight from a transient view, as + * {@link #handleClassified} would from the same chars, without making a {@code String}. Values + * that path would treat differently (several comma-separated values, or a trace id whose text + * isn't canonical, since {@code DDTraceId} keeps the text it parsed) are copied and take it. + * The view is parsed here and not kept. + */ + @Override + public boolean acceptTransient(HeaderSchema header, String key, CharSequence value) { + int classification = + header == null || value == null || LOG_EXTRACT_HEADER_NAMES ? BY_NAME : classify(header); + if ((classification != TRACE_ID + && classification != SPAN_ID + && classification != SAMPLING_PRIORITY) + || !isParsedAsIs(classification, value)) { + return accept(header, key, value == null ? null : value.toString()); + } + int end = value.length(); + try { + switch (classification) { + case TRACE_ID: + traceId = DDTraceId.from(LongStringUtils.parseUnsignedLong(value, 0, end)); + break; + case SPAN_ID: + spanId = LongStringUtils.parseUnsignedLong(value, 0, end); + break; + default: + samplingPriority = LongStringUtils.parseInt(value, 0, end); + } + } catch (NumberFormatException e) { + invalidateContext(); + log.debug(EXCLUDE_TELEMETRY, "Ignoring context with invalid {}: {}", key, value.toString()); + return false; + } catch (RuntimeException e) { + invalidateContext(); + log.debug("Exception when extracting context", e); + return false; + } + return true; + } + + /** + * Whether {@link #handleClassified} would parse {@code value} as is: no comma (so {@code + * firstHeaderValue} returns it unchanged), and for a trace id, canonical decimal text (no + * leading {@code +} or zero), which {@code DDTraceId} would print back the same. + */ + private static boolean isParsedAsIs(int classification, CharSequence value) { + int length = value.length(); + for (int i = 0; i < length; i++) { + if (value.charAt(i) == ',') { + return false; + } + } + if (classification == TRACE_ID && length > 1) { + char first = value.charAt(0); + return first != '0' && first != '+'; + } + return true; + } + /** Stores the value of a key classified as one of this style's own. */ private boolean handleClassified( int classification, String key, String value, String lowerCaseKey) { diff --git a/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java b/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java index a05509ec55d..7e55dd7ad11 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/propagation/HttpCodec.java @@ -15,6 +15,7 @@ import datadog.trace.api.DDTraceId; import datadog.trace.api.TraceConfig; import datadog.trace.api.TracePropagationStyle; +import datadog.trace.api.internal.util.LongStringUtils; import datadog.trace.bootstrap.instrumentation.api.AgentPropagation; import datadog.trace.bootstrap.instrumentation.api.SpanAttributes; import datadog.trace.bootstrap.instrumentation.api.TagContext; @@ -411,7 +412,8 @@ public ExtractionCache(C carrier, AgentPropagation.ContextVisitor getter) { // a known header is matched by its constant; a key without one, by name if (header == HeaderSchema.X_DATADOG_PARENT_ID || header == null && SPAN_ID_KEY.equalsIgnoreCase(this.indexed.key(i))) { - cacheDatadogSpanIdValue(this.indexed.value(i)); + // parsed from the value as handed, so a transient view isn't copied + cacheDatadogSpanIdValue(this.indexed.valueView(i)); } } } else { @@ -435,10 +437,15 @@ private void cacheDatadogSpanId(String key, String value) { } } - private void cacheDatadogSpanIdValue(String value) { + private void cacheDatadogSpanIdValue(CharSequence value) { + if (value == null) { + return; + } try { - // Parse numeric header value to format it as 16 hexadecimal character format - this.datadogSpanIdHex = DDSpanId.toHexStringPadded(DDSpanId.from(value)); + // Parse numeric header value to format it as 16 hexadecimal character format; the + // range parser gives what DDSpanId.from would for the same chars + this.datadogSpanIdHex = + DDSpanId.toHexStringPadded(LongStringUtils.parseUnsignedLong(value, 0, value.length())); } catch (NumberFormatException ignored) { } } diff --git a/dd-trace-core/src/main/java/datadog/trace/core/propagation/InferredProxyPropagator.java b/dd-trace-core/src/main/java/datadog/trace/core/propagation/InferredProxyPropagator.java index 2206bda2d89..4dfc6ef121a 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/propagation/InferredProxyPropagator.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/propagation/InferredProxyPropagator.java @@ -6,6 +6,7 @@ import datadog.context.propagation.CarrierKeys; import datadog.context.propagation.CarrierSetter; import datadog.context.propagation.CarrierVisitor; +import datadog.context.propagation.IndexedCarrier; import datadog.context.propagation.Propagator; import datadog.trace.api.gateway.InferredProxySpan; import java.util.HashMap; @@ -35,7 +36,17 @@ public Context extract(Context context, C carrier, CarrierVisitor visitor return context; } InferredProxyContextExtractor extractor = new InferredProxyContextExtractor(); - visitor.forEachKeyValue(carrier, extractor); + if (carrier instanceof IndexedCarrier) { + // a known header never has the proxy prefix, so only keys without a constant are read + IndexedCarrier indexed = (IndexedCarrier) carrier; + for (int i = 0, n = indexed.size(); i < n; i++) { + if (indexed.header(i) == null) { + extractor.accept(indexed.key(i), indexed.value(i)); + } + } + } else { + visitor.forEachKeyValue(carrier, extractor); + } InferredProxySpan inferredProxySpan = extractor.inferredProxySpan(); if (inferredProxySpan != null) { context = context.with(inferredProxySpan); diff --git a/dd-trace-core/src/test/java/datadog/trace/core/propagation/ExtractorKeysForkedTest.java b/dd-trace-core/src/test/java/datadog/trace/core/propagation/ExtractorKeysForkedTest.java index 99e77f0b6b5..ce0ab7826ac 100644 --- a/dd-trace-core/src/test/java/datadog/trace/core/propagation/ExtractorKeysForkedTest.java +++ b/dd-trace-core/src/test/java/datadog/trace/core/propagation/ExtractorKeysForkedTest.java @@ -224,15 +224,31 @@ static final class Indexed implements IndexedCarrier { final List keys = new ArrayList<>(); final List values = new ArrayList<>(); final List headers = new ArrayList<>(); + final List views = new ArrayList<>(); Indexed(Map carrier, CarrierKeys classifier) { carrier.forEach((key, value) -> { + HeaderSchema header = classifier.header(key); keys.add(key); values.add(value); - headers.add(classifier.header(key)); + headers.add(header); + views.add( + header != null && header.isParseOnly() && value != null + ? new StringBuilder(value) + : null); }); } + /** Overwrites every view, as a reused fetch buffer would. */ + void overwriteViews() { + for (StringBuilder view : views) { + if (view != null) { + view.setLength(0); + view.append("999"); + } + } + } + @Override public int size() { return keys.size(); @@ -256,11 +272,8 @@ public HeaderSchema header(int index) { /** Parse-only values as views that aren't {@code String}s, as a byte carrier hands them. */ @Override public CharSequence valueView(int index) { - HeaderSchema header = headers.get(index); - String value = values.get(index); - return header != null && header.isParseOnly() && value != null - ? new StringBuilder(value) - : value; + StringBuilder view = views.get(index); + return view != null ? view : values.get(index); } } @@ -322,4 +335,64 @@ void compoundExtractionReadsHeaderConstantsInPlace() { describe(compound.extract(headers, stringValuesMap())), describe(compound.extract(new Indexed(headers, compound.keys()), WITH_HEADERS))); } + + static String[][] datadogValues() { + return new String[][] { + {"123", "456", "1"}, + {"00123", "0456", "+1"}, + {"+123", "+456", "-1"}, + {"0", "0", "0"}, + {"123,456", "456, 789", "2,1"}, + {"abc", "456", "1"}, + {"123", "4x6", "1"}, + {"123", "456", "one"}, + {"18446744073709551615", "18446744073709551615", "2147483647"}, + {"18446744073709551616", "456", "2147483648"}, + {"9223372036854775807", "9223372036854775808", "-2147483648"}, + }; + } + + private static Map datadogHeaders( + String traceId, String parentId, String priority) { + Map headers = new LinkedHashMap<>(); + headers.put("x-datadog-trace-id", traceId); + headers.put("x-datadog-parent-id", parentId); + headers.put("x-datadog-sampling-priority", priority); + headers.put("x-datadog-origin", "synthetics"); + headers.put("traceparent", "00-0000000000000000000000000000007b-00000000000001c8-01"); + return headers; + } + + private static HttpCodec.Extractor datadogAndW3C() { + DynamicConfig dynamicConfig = DynamicConfig.create().apply(); + List parts = new ArrayList<>(); + parts.add(DatadogHttpCodec.newExtractor(Config.get(), dynamicConfig::captureTraceConfig)); + parts.add(W3CHttpCodec.newExtractor(Config.get(), dynamicConfig::captureTraceConfig)); + return new HttpCodec.CompoundExtractor(parts, false); + } + + @ParameterizedTest + @MethodSource("datadogValues") + void parsingIdsFromViewsMatchesParsingStrings(String traceId, String parentId, String priority) { + Map headers = datadogHeaders(traceId, parentId, priority); + DynamicConfig dynamicConfig = DynamicConfig.create().apply(); + HttpCodec.Extractor datadog = + DatadogHttpCodec.newExtractor(Config.get(), dynamicConfig::captureTraceConfig); + for (HttpCodec.Extractor extractor : new HttpCodec.Extractor[] {datadog, datadogAndW3C()}) { + assertEquals( + describe(extractor.extract(headers, stringValuesMap())), + describe(extractor.extract(new Indexed(headers, extractor.keys()), WITH_HEADERS))); + } + } + + @ParameterizedTest + @MethodSource("datadogValues") + void viewsAreNotKeptPastExtraction(String traceId, String parentId, String priority) { + HttpCodec.Extractor extractor = datadogAndW3C(); + Indexed carrier = new Indexed(datadogHeaders(traceId, parentId, priority), extractor.keys()); + TagContext context = extractor.extract(carrier, WITH_HEADERS); + String before = describe(context); + carrier.overwriteViews(); + assertEquals(before, describe(context), "the context kept a view"); + } }