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 @@ -23,9 +23,10 @@
*/
public final class CarrierKeys {
private static final CarrierKeys ALL =
new CarrierKeys(true, new String[0][], new byte[0][][], new String[0]);
new CarrierKeys(true, new String[0][], new byte[0][][], new HeaderSchema[0][], new String[0]);
private static final CarrierKeys NONE =
new CarrierKeys(false, new String[0][], new byte[0][][], new String[0]);
new CarrierKeys(
false, new String[0][], new byte[0][][], new HeaderSchema[0][], new String[0]);

private final boolean all;

Expand All @@ -35,14 +36,22 @@ public final class CarrierKeys {
/** Each name's lower-case ASCII bytes (null if it isn't ASCII), parallel to the names. */
private final byte[][][] asciiByLength;

/** Each name's {@link HeaderSchema} constant, or null, parallel to {@link #namesByLength}. */
private final HeaderSchema[][] headersByLength;

/** Lower-cased prefixes. */
private final String[] prefixes;

private CarrierKeys(
boolean all, String[][] namesByLength, byte[][][] asciiByLength, String[] prefixes) {
boolean all,
String[][] namesByLength,
byte[][][] asciiByLength,
HeaderSchema[][] headersByLength,
String[] prefixes) {
this.all = all;
this.namesByLength = namesByLength;
this.asciiByLength = asciiByLength;
this.headersByLength = headersByLength;
this.prefixes = prefixes;
}

Expand All @@ -61,6 +70,15 @@ public static CarrierKeys of(String... names) {
return builder().names(Arrays.asList(names)).build();
}

/** Exactly these headers. */
public static CarrierKeys of(HeaderSchema... headers) {
Builder builder = builder();
for (HeaderSchema header : headers) {
builder.header(header);
}
return builder.build();
}

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -109,13 +127,49 @@ public boolean containsAscii(ByteBuffer key) {
* that matched a key's bytes use the name as the key without decoding it. Reads only.
*/
public String nameAscii(ByteBuffer key) {
int index = indexAscii(key);
return index < 0 ? null : namesByLength[key.remaining()][index];
}

/**
* The {@link HeaderSchema} constant of the declared name that the ASCII bytes between {@code
* key}'s position and limit spell, ignoring case; null if they spell no declared name, or one
* with no constant (such as a configured header tag). Reads only.
*/
public HeaderSchema headerAscii(ByteBuffer key) {
int index = indexAscii(key);
return index < 0 ? null : headersByLength[key.remaining()][index];
}

/**
* The {@link HeaderSchema} constant of the declared name {@code key} is, ignoring case; null if
* it's no declared name, or one with no constant.
*/
public HeaderSchema header(String key) {
int length = key.length();
if (length < namesByLength.length) {
String[] names = namesByLength[length];
if (names != null) {
for (int i = 0; i < names.length; i++) {
// usually lower case already: the declared name itself, or a decoded copy of it
if (names[i].equals(key) || names[i].regionMatches(true, 0, key, 0, length)) {
return headersByLength[length][i];
}
}
}
}
return null;
}

/** The index, among the names of its length, of the name {@code key}'s bytes spell, or -1. */
private int indexAscii(ByteBuffer key) {
int length = key.remaining();
if (length >= namesByLength.length) {
return null;
return -1;
}
String[] names = namesByLength[length];
if (names == null) {
return null;
return -1;
}
int start = key.position();
if (key.hasArray()) {
Expand All @@ -126,19 +180,19 @@ public String nameAscii(ByteBuffer key) {
byte[][] asciiNames = asciiByLength[length];
for (int i = 0; i < names.length; i++) {
if (bytesEqual(bytes, offset, asciiNames[i], length)) {
return names[i];
return i;
}
}
if (!hasUpperCase(bytes, offset, length)) {
return null;
return -1;
}
}
for (String name : names) {
if (asciiMatches(key, start, name, length)) {
return name;
for (int i = 0; i < names.length; i++) {
if (asciiMatches(key, start, names[i], length)) {
return i;
}
}
return null;
return -1;
}

/** Whether {@code length} bytes from {@code offset} are {@code expected}'s (null: never). */
Expand Down Expand Up @@ -227,6 +281,12 @@ public static final class Builder {

private Builder() {}

/** Adds {@code header}'s name, which then matches as that constant. */
public Builder header(HeaderSchema header) {
names.add(header.headerName);
return this;
}

public Builder name(String name) {
if (name != null && !name.isEmpty()) {
names.add(name.toLowerCase(Locale.ROOT));
Expand Down Expand Up @@ -287,10 +347,16 @@ public CarrierKeys build() {
sameLength.add(name);
}
String[][] namesByLength = new String[names.isEmpty() ? 0 : maxLength + 1][];
HeaderSchema[][] headersByLength = new HeaderSchema[namesByLength.length][];
for (int i = 0; i < namesByLength.length; i++) {
List<String> sameLength = grouped.get(i);
if (sameLength != null) {
namesByLength[i] = sameLength.toArray(new String[0]);
headersByLength[i] = new HeaderSchema[namesByLength[i].length];
for (int j = 0; j < namesByLength[i].length; j++) {
// any declared name that is a known header matches as its constant
headersByLength[i][j] = HeaderSchema.forName(namesByLength[i][j]);
}
}
}
byte[][][] asciiByLength = new byte[namesByLength.length][][];
Expand All @@ -302,7 +368,8 @@ public CarrierKeys build() {
}
}
}
return new CarrierKeys(false, namesByLength, asciiByLength, prefixes.toArray(new String[0]));
return new CarrierKeys(
false, namesByLength, asciiByLength, headersByLength, prefixes.toArray(new String[0]));
}

/** {@code name}'s ASCII bytes, or null if it has a char outside ASCII. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {
@Override
public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor) {
// Extract and cache the carrier key/value pairs the propagators read
CarrierCache carrierCache = new CarrierCache();
visitor.forEachKeyValue(carrier, keys(), carrierCache);
CarrierKeys keys = keys();
CarrierCache carrierCache = new CarrierCache(keys);
visitor.forEachKeyValue(carrier, keys, carrierCache);
// Run the multiple extractions on cache
for (Propagator propagator : this.propagators) {
context = propagator.extract(context, carrierCache, carrierCache);
Expand All @@ -85,16 +86,36 @@ static final class CarrierCache
/** Keys at even indexes, values at odd ones. */
private String[] keysAndValues = new String[2 * INITIAL_PAIRS];

/**
* Each pair's {@link HeaderSchema} constant as {@code ordinal() + 1}, or 0 for none: a byte per
* pair rather than a reference, since there are fewer than 127 constants.
*/
private byte[] headers = new byte[INITIAL_PAIRS];

private int size;

/** Classifies keys with {@code keys}, which knows each declared name's constant. */
private final CarrierKeys keys;

CarrierCache(CarrierKeys keys) {
this.keys = keys;
}

CarrierCache() {
this(CarrierKeys.none());
}

@Override
public void accept(String key, String value) {
int index = 2 * this.size;
if (index == this.keysAndValues.length) {
this.keysAndValues = Arrays.copyOf(this.keysAndValues, 2 * index);
this.headers = Arrays.copyOf(this.headers, this.size * 2);
}
this.keysAndValues[index] = key;
this.keysAndValues[index + 1] = value;
HeaderSchema header = key == null ? null : this.keys.header(key);
this.headers[this.size] = header == null ? 0 : (byte) (header.ordinal() + 1);
this.size++;
}

Expand All @@ -119,5 +140,11 @@ public String key(int index) {
public String value(int index) {
return this.keysAndValues[2 * index + 1];
}

@Override
public HeaderSchema header(int index) {
int header = this.headers[index];
return header == 0 ? null : HeaderSchema.fromOrdinal(header - 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package datadog.context.propagation;

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

/**
* The carrier keys (headers) the tracer knows by name, each with the encodings carriers commonly
* need, computed once. Propagators read a carrier's keys through {@link CarrierKeys}, which tells a
* key's constant from its characters or bytes, so code that handles several headers can switch on
* the constant instead of comparing strings.
*
* <p>A constant's {@link #ordinal()} is a dense index: per-header state, or carrier-specific forms
* of a header name (a Netty {@code AsciiString}, a gRPC {@code Metadata.Key}), belong in an array
* of length {@code HeaderSchema.values().length}, indexed by ordinal and filled once, which keeps
* this class free of carrier dependencies.
*
* <p>Names are compared ignoring ASCII case, as carriers such as HTTP do. Keys that aren't known
* ahead of time (configured header tags, baggage mappings, prefixed keys) have no constant.
*/
public enum HeaderSchema {
// Datadog
X_DATADOG_TRACE_ID("x-datadog-trace-id"),
X_DATADOG_PARENT_ID("x-datadog-parent-id"),
X_DATADOG_SAMPLING_PRIORITY("x-datadog-sampling-priority"),
X_DATADOG_ORIGIN("x-datadog-origin"),
X_DATADOG_TAGS("x-datadog-tags"),
// W3C trace context
TRACEPARENT("traceparent"),
TRACESTATE("tracestate"),
// W3C baggage
BAGGAGE("baggage"),
// B3
X_B3_TRACE_ID("x-b3-traceid"),
X_B3_SPAN_ID("x-b3-spanid"),
X_B3_SAMPLED("x-b3-sampled"),
B3("b3"),
// Haystack
HAYSTACK_TRACE_ID("trace-id"),
HAYSTACK_SPAN_ID("span-id"),
HAYSTACK_PARENT_ID("parent-id"),
// AWS X-Ray
X_AMZN_TRACE_ID("x-amzn-trace-id"),
// Data Streams Monitoring
DD_PATHWAY_CTX_BASE64("dd-pathway-ctx-base64"),
// forwarding, user agent and client IP, read by every propagation style
FORWARDED("forwarded"),
FORWARDED_FOR("forwarded-for"),
X_FORWARDED_PROTO("x-forwarded-proto"),
X_FORWARDED_HOST("x-forwarded-host"),
X_FORWARDED_FOR("x-forwarded-for"),
X_FORWARDED_PORT("x-forwarded-port"),
USER_AGENT("user-agent"),
X_CLIENT_IP("x-client-ip"),
TRUE_CLIENT_IP("true-client-ip"),
X_CLUSTER_CLIENT_IP("x-cluster-client-ip"),
X_REAL_IP("x-real-ip"),
FASTLY_CLIENT_IP("fastly-client-ip"),
CF_CONNECTING_IP("cf-connecting-ip"),
CF_CONNECTING_IP_V6("cf-connecting-ipv6");

private static final HeaderSchema[] VALUES = values();

/** The header name, lower case. */
public final String headerName;

/** {@link #headerName} in ASCII, for matching or writing a key without encoding it. */
private final byte[] asciiName;

/** {@code headerName} must be lower case, as {@code HeaderSchemaTest} checks. */
HeaderSchema(String headerName) {
this.headerName = headerName;
this.asciiName = headerName.getBytes(US_ASCII);
}

/** The length of {@link #headerName}, in chars and in ASCII bytes. */
public int length() {
return asciiName.length;
}

/** {@link #headerName}'s ASCII byte at {@code index}. */
public byte asciiByte(int index) {
return asciiName[index];
}

/** A copy of {@link #headerName} in ASCII. */
public byte[] asciiName() {
return asciiName.clone();
}

/** The number of constants: the length of an array indexed by {@link #ordinal()}. */
public static int count() {
return VALUES.length;
}

/** The constant whose {@link #headerName} is {@code lowerCaseName}, or null. */
public static HeaderSchema forName(String lowerCaseName) {
for (HeaderSchema header : VALUES) {
if (header.headerName.equals(lowerCaseName)) {
return header;
}
}
return null;
}

/** The constant with {@code ordinal}. */
public static HeaderSchema fromOrdinal(int ordinal) {
return VALUES[ordinal];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public interface IndexedCarrier {

/** The value of pair {@code index}, from 0 to {@link #size()} - 1. */
String value(int index);

/**
* The {@link HeaderSchema} constant of pair {@code index}'s key, or null if it has none (an
* unknown key, or a key the carrier didn't classify). Lets a propagator switch on the constant
* instead of comparing names; null keys must still be checked by name.
*/
HeaderSchema header(int index);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datadog.context.propagation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import datadog.context.propagation.CompositePropagator.CarrierCache;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +52,35 @@ void visitsEveryPairInOrder() {
assertEquals("key-19=value-19", seen.get(19));
}

@Test
void recordsEachKeysHeaderConstant() {
CarrierCache cache =
new CarrierCache(
CarrierKeys.builder().header(HeaderSchema.TRACEPARENT).name("x-custom").build());
cache.accept("traceparent", "tp");
cache.accept("x-custom", "c");
cache.accept(null, "no key");
assertSame(HeaderSchema.TRACEPARENT, cache.header(0));
assertNull(cache.header(1));
assertNull(cache.header(2));
assertNull(cache.key(2));
}

@Test
void keepsHeaderConstantsPastItsInitialCapacity() {
CarrierCache cache = new CarrierCache(CarrierKeys.of(HeaderSchema.BAGGAGE));
for (int i = 0; i < 20; i++) {
cache.accept(i % 2 == 0 ? "baggage" : "key-" + i, "value-" + i);
}
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
assertSame(HeaderSchema.BAGGAGE, cache.header(i));
} else {
assertNull(cache.header(i));
}
}
}

@Test
void emptyCache() {
CarrierCache cache = new CarrierCache();
Expand Down
Loading
Loading