Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import datadog.trace.bootstrap.instrumentation.api.BufferedHeaderFields;
import datadog.trace.util.AsciiSequence;
import java.nio.ByteBuffer;
import org.apache.kafka.common.header.Header;

Expand Down Expand Up @@ -79,6 +80,24 @@ static String utf8Value(Header header, BufferedHeaderFields fields) {
return value == null ? null : new String(value, UTF_8);
}

/**
* The header's value as a view of its bytes, without copying or decoding them, or null if it
* can't be viewed: no value, non-ASCII bytes, or a direct buffer. The view shares the record's
* buffer (the whole fetch buffer), so it must not outlive the extraction ({@link AsciiSequence}).
*/
static CharSequence asciiValueView(BufferedHeaderFields fields) {
byte[] value = fields.$$DD$$__value();
if (value != null) {
return AsciiSequence.ofAscii(value, 0, value.length);
}
ByteBuffer buffer = fields.$$DD$$__valueBuffer();
if (buffer == null || !buffer.hasArray()) {
return null;
}
return AsciiSequence.ofAscii(
buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
}

/** The known name whose bytes {@code buffer}'s remaining bytes are, or null. */
static String knownKey(ByteBuffer buffer) {
int length = buffer.remaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static datadog.trace.instrumentation.kafka_common.Utils.readProducedTime;

import datadog.context.propagation.CarrierKeys;
import datadog.context.propagation.HeaderSchema;
import datadog.context.propagation.TransientValueConsumer;
import datadog.trace.api.Config;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation.ContextVisitor;
Expand Down Expand Up @@ -40,7 +42,7 @@ public TextMapExtractAdapter(boolean decodeBase64Headers) {

@Override
public void forEachKey(Headers carrier, AgentPropagation.KeyClassifier classifier) {
visit(carrier, CarrierKeys.all(), classifier);
visit(carrier, CarrierKeys.all(), classifier, null);
}

/**
Expand All @@ -57,7 +59,11 @@ public void forEachKeyValue(
(key, value) -> {
visitor.accept(key, value);
return true;
});
},
// a visitor that takes transient views gets parse-only values without decoding them
decoder == null && visitor instanceof TransientValueConsumer
? (TransientValueConsumer) visitor
: null);
}

/**
Expand All @@ -66,14 +72,29 @@ public void forEachKeyValue(
* keys aren't decoded and UTF-8 values aren't copied before decoding; any other goes through
* {@code key()} and {@code value()}.
*/
private void visit(Headers carrier, CarrierKeys keys, AgentPropagation.KeyClassifier sink) {
private void visit(
Headers carrier,
CarrierKeys keys,
AgentPropagation.KeyClassifier sink,
TransientValueConsumer transientSink) {
for (Header header : carrier) {
BufferedHeaderFields fields =
header instanceof BufferedHeaderFields ? (BufferedHeaderFields) header : null;
String key = key(header, fields, keys);
if (key == null) {
continue;
}
if (transientSink != null && fields != null) {
// a matched key is the declared name itself, so this is an identity check
HeaderSchema schema = keys.header(key);
if (schema != null && schema.isParseOnly()) {
CharSequence view = RecordHeaderAccess.asciiValueView(fields);
if (view != null) {
transientSink.acceptTransient(schema, key, view);
continue;
}
}
}
String value = value(header, fields, key);
if (value == null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import datadog.context.propagation.CarrierKeys;
import datadog.context.propagation.HeaderSchema;
import datadog.context.propagation.TransientValueConsumer;
import datadog.trace.agent.tooling.bytebuddy.FieldAccessors;
import datadog.trace.bootstrap.instrumentation.api.BufferedHeaderFields;
import datadog.trace.util.AsciiSequence;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.function.BiConsumer;
import net.bytebuddy.description.type.TypeDescription.ForLoadedType;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.internals.RecordHeader;
Expand Down Expand Up @@ -217,6 +221,92 @@ void filteredVisitDecodesBase64ValuesOfKeptKeysOnly() {
assertEquals(Arrays.asList("traceparent=00-abc-def-01"), seen);
}

/** Records what it's handed: "key=value" for Strings, "key~value" for transient views. */
static final class Recording implements BiConsumer<String, String>, TransientValueConsumer {
final List<String> seen = new ArrayList<>();
final List<Object> views = new ArrayList<>();

@Override
public void accept(String key, String value) {
seen.add(key + "=" + value);
}

@Override
public void acceptTransient(HeaderSchema header, String key, CharSequence value) {
assertSame(header.headerName, key);
seen.add(key + "~" + value);
views.add(value);
}
}

private static final CarrierKeys DATADOG_AND_W3C =
CarrierKeys.of(
HeaderSchema.X_DATADOG_TRACE_ID,
HeaderSchema.X_DATADOG_PARENT_ID,
HeaderSchema.X_DATADOG_SAMPLING_PRIORITY,
HeaderSchema.TRACEPARENT);

@Test
void parseOnlyValuesAreHandedAsViews() {
RecordHeaders headers = new RecordHeaders();
headers.add(fetched("x-datadog-trace-id", "123"));
headers.add(fetched("traceparent", "00-abc-def-01"));
headers.add(fetched("x-datadog-sampling-priority", "1"));
headers.add(new OtherHeader("x-datadog-parent-id", "456"));
Recording recording = new Recording();
new TextMapExtractAdapter(false).forEachKeyValue(headers, DATADOG_AND_W3C, recording);

assertEquals(
Arrays.asList(
"x-datadog-trace-id~123",
"traceparent=00-abc-def-01",
"x-datadog-sampling-priority~1",
"x-datadog-parent-id=456"),
recording.seen);
for (Object view : recording.views) {
assertTrue(view instanceof AsciiSequence, view.getClass().getName());
}
}

@Test
void viewsAlsoCoverValuesKafkaAlreadyCopied() {
FieldsHeader header = fetched("x-datadog-parent-id", "456");
header.value(); // Kafka copies the value into its own array
RecordHeaders headers = new RecordHeaders();
headers.add(header);
Recording recording = new Recording();
new TextMapExtractAdapter(false).forEachKeyValue(headers, DATADOG_AND_W3C, recording);
assertEquals(Arrays.asList("x-datadog-parent-id~456"), recording.seen);
}

@Test
void nonAsciiOrMissingParseOnlyValuesAreNotViewed() {
RecordHeaders headers = new RecordHeaders();
headers.add(fetched("x-datadog-trace-id", "1\u00e92"));
headers.add(fetched("x-datadog-parent-id", null));
Recording recording = new Recording();
new TextMapExtractAdapter(false).forEachKeyValue(headers, DATADOG_AND_W3C, recording);
assertEquals(Arrays.asList("x-datadog-trace-id=1\u00e92"), recording.seen);
assertNull(RecordHeaderAccess.asciiValueView(fetched("x-datadog-parent-id", null)));
}

@Test
void noViewsInBase64ModeOrForPlainVisitors() {
RecordHeaders headers = new RecordHeaders();
headers.add(
new RecordHeader("x-datadog-trace-id", Base64.getEncoder().encode("123".getBytes(UTF_8))));
Recording recording = new Recording();
new TextMapExtractAdapter(true).forEachKeyValue(headers, DATADOG_AND_W3C, recording);
assertEquals(Arrays.asList("x-datadog-trace-id=123"), recording.seen);

RecordHeaders fetchedHeaders = new RecordHeaders();
fetchedHeaders.add(fetched("x-datadog-trace-id", "123"));
List<String> plain = new ArrayList<>();
new TextMapExtractAdapter(false)
.forEachKeyValue(fetchedHeaders, DATADOG_AND_W3C, (key, value) -> plain.add(value));
assertEquals(Arrays.asList("123"), plain);
}

private static List<String> visit(TextMapExtractAdapter adapter, RecordHeaders headers) {
List<String> seen = new ArrayList<>();
adapter.forEachKey(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package datadog.trace.util;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

import datadog.trace.api.function.NoEscape;

/**
* A {@code CharSequence} that is a view of ASCII bytes in an array, without copying or decoding
* them: the byte counterpart of {@link SubSequence}. Lets a value that arrived as bytes (a fetched
* Kafka header, for instance) be parsed in place, for example with {@code LongStringUtils}' range
* parsers, instead of being decoded into a {@code String} first.
*
* <p>{@link NoEscape}: a view shares its array, and for a fetched record that array is the whole
* fetch buffer, so keeping a view anywhere longer-lived than the call that received it (a field, a
* span, a context, a cache) pins all of it. A value that must be kept is copied with {@link
* #toString()}.
*
* <p>Only ASCII bytes can be viewed as chars one-for-one, so {@link #ofAscii} returns null when a
* byte isn't, and the caller decodes those bytes as before. Equality is identity, as for any {@code
* CharSequence} that doesn't promise otherwise; compare contents with {@code toString()} or a
* content comparison.
*/
@NoEscape
public final class AsciiSequence implements CharSequence {
private final byte[] bytes;
private final int offset;
private final int length;

private AsciiSequence(byte[] bytes, int offset, int length) {
this.bytes = bytes;
this.offset = offset;
this.length = length;
}

/**
* A view of {@code bytes[offset, offset + length)} if every one of those bytes is ASCII, else
* null.
*/
public static AsciiSequence ofAscii(byte[] bytes, int offset, int length) {
if (offset < 0 || length < 0 || offset > bytes.length - length) {
throw new IndexOutOfBoundsException(
"offset " + offset + ", length " + length + ", array length " + bytes.length);
}
for (int i = offset, end = offset + length; i < end; i++) {
if (bytes[i] < 0) {
return null; // not ASCII
}
}
return new AsciiSequence(bytes, offset, length);
}

@Override
public int length() {
return length;
}

@Override
public char charAt(int index) {
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("index " + index + ", length " + length);
}
return (char) bytes[offset + index];
}

/** A view of chars {@code [start, end)} of this one, sharing its array. */
@Override
public AsciiSequence subSequence(int start, int end) {
if (start < 0 || end > length || start > end) {
throw new IndexOutOfBoundsException("start " + start + ", end " + end + ", length " + length);
}
return new AsciiSequence(bytes, offset + start, end - start);
}

/** A copy, as a {@code String}: what to keep when the value must outlive the call. */
@Override
public String toString() {
// the bytes are ASCII, so ISO-8859-1 decodes them one-for-one in a single copy
return new String(bytes, offset, length, ISO_8859_1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package datadog.trace.util;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import datadog.trace.api.internal.util.LongStringUtils;
import org.junit.jupiter.api.Test;

class AsciiSequenceTest {

/** {@code value}'s UTF-8 bytes inside a larger array, at offset 3. */
private static byte[] embedded(String value) {
byte[] inner = value.getBytes(UTF_8);
byte[] outer = new byte[inner.length + 5];
outer[0] = 'x';
System.arraycopy(inner, 0, outer, 3, inner.length);
outer[outer.length - 1] = 'y';
return outer;
}

@Test
void viewsAsciiBytesAsChars() {
String value = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01";
byte[] bytes = embedded(value);
AsciiSequence view = AsciiSequence.ofAscii(bytes, 3, value.length());
assertNotNull(view);
assertEquals(value.length(), view.length());
for (int i = 0; i < value.length(); i++) {
assertEquals(value.charAt(i), view.charAt(i));
}
assertEquals(value, view.toString());
assertEquals("0af7651916cd43dd8448eb211c80319c", view.subSequence(3, 35).toString());
assertEquals("", view.subSequence(5, 5).toString());
}

@Test
void nonAsciiBytesAreNotViewed() {
byte[] bytes = embedded("café");
assertNull(AsciiSequence.ofAscii(bytes, 3, "café".getBytes(UTF_8).length));
// the ASCII part alone is fine
assertNotNull(AsciiSequence.ofAscii(bytes, 3, 3));
}

@Test
void toStringCopies() {
byte[] bytes = embedded("12345");
AsciiSequence view = AsciiSequence.ofAscii(bytes, 3, 5);
String copy = view.toString();
bytes[3] = '9';
assertEquals("12345", copy);
assertEquals('9', view.charAt(0)); // a view sees its array
}

@Test
void parsesInPlace() {
byte[] bytes = embedded("8264894952805660751");
AsciiSequence view = AsciiSequence.ofAscii(bytes, 3, 19);
assertEquals(
LongStringUtils.parseUnsignedLong("8264894952805660751"),
LongStringUtils.parseUnsignedLong(view, 0, view.length()));
}

@Test
void checksBounds() {
byte[] bytes = embedded("abc");
assertThrows(IndexOutOfBoundsException.class, () -> AsciiSequence.ofAscii(bytes, -1, 1));
assertThrows(IndexOutOfBoundsException.class, () -> AsciiSequence.ofAscii(bytes, 0, -1));
assertThrows(
IndexOutOfBoundsException.class, () -> AsciiSequence.ofAscii(bytes, 6, bytes.length));
AsciiSequence view = AsciiSequence.ofAscii(bytes, 3, 3);
assertThrows(IndexOutOfBoundsException.class, () -> view.charAt(3));
assertThrows(IndexOutOfBoundsException.class, () -> view.charAt(-1));
assertThrows(IndexOutOfBoundsException.class, () -> view.subSequence(2, 1));
assertThrows(IndexOutOfBoundsException.class, () -> view.subSequence(0, 4));
assertThrows(IndexOutOfBoundsException.class, () -> view.subSequence(-1, 2));
}
}
Loading