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
@@ -1,8 +1,7 @@
package datadog.context.propagation;

import datadog.context.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.function.BiConsumer;

class CompositePropagator implements Propagator {
Expand Down Expand Up @@ -74,25 +73,51 @@ public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor
return context;
}

static class CarrierCache implements BiConsumer<String, String>, CarrierVisitor<CarrierCache> {
/** Cached key/values from carrier (even indexes are keys, odd indexes are values). */
private final List<String> keysAndValues;
/**
* The carrier key/value pairs the propagators read, cached so each propagator reads them without
* walking the carrier again, and readable in place as an {@link IndexedCarrier}.
*/
static final class CarrierCache
implements BiConsumer<String, String>, CarrierVisitor<CarrierCache>, IndexedCarrier {
/** Enough for the trace headers a carrier usually holds once unread keys are skipped. */
private static final int INITIAL_PAIRS = 8;

public CarrierCache() {
this.keysAndValues = new ArrayList<>(32);
}
/** Keys at even indexes, values at odd ones. */
private String[] keysAndValues = new String[2 * INITIAL_PAIRS];

private int size;

@Override
public void accept(String key, String value) {
this.keysAndValues.add(key);
this.keysAndValues.add(value);
int index = 2 * this.size;
if (index == this.keysAndValues.length) {
this.keysAndValues = Arrays.copyOf(this.keysAndValues, 2 * index);
}
this.keysAndValues[index] = key;
this.keysAndValues[index + 1] = value;
this.size++;
}

@Override
public void forEachKeyValue(CarrierCache carrier, BiConsumer<String, String> visitor) {
for (int i = 0; i < carrier.keysAndValues.size() - 1; i += 2) {
visitor.accept(carrier.keysAndValues.get(i), carrier.keysAndValues.get(i + 1));
for (int i = 0; i < carrier.size; i++) {
visitor.accept(carrier.keysAndValues[2 * i], carrier.keysAndValues[2 * i + 1]);
}
}

@Override
public int size() {
return this.size;
}

@Override
public String key(int index) {
return this.keysAndValues[2 * index];
}

@Override
public String value(int index) {
return this.keysAndValues[2 * index + 1];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package datadog.context.propagation;

/**
* A carrier whose key/value pairs are already in memory and can be read by index, so a propagator
* that reads them more than once can do so in place instead of copying them first. The composite
* propagator hands its propagators one, holding the keys any of them reads.
*
* <p>Read-only, and valid only for the extraction it was handed to: don't keep a reference.
*/
public interface IndexedCarrier {
/** The number of key/value pairs. */
int size();

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

/** The value of pair {@code index}, from 0 to {@link #size()} - 1. */
String value(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package datadog.context.propagation;

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

import datadog.context.propagation.CompositePropagator.CarrierCache;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;

class CarrierCacheTest {

@Test
void readsPairsByIndexInOrder() {
CarrierCache cache = new CarrierCache();
cache.accept("traceparent", "tp");
cache.accept("baggage", "b");

assertEquals(2, cache.size());
assertEquals("traceparent", cache.key(0));
assertEquals("tp", cache.value(0));
assertEquals("baggage", cache.key(1));
assertEquals("b", cache.value(1));
}

@Test
void growsPastItsInitialCapacity() {
CarrierCache cache = new CarrierCache();
for (int i = 0; i < 40; i++) {
cache.accept("key-" + i, "value-" + i);
}

assertEquals(40, cache.size());
for (int i = 0; i < 40; i++) {
assertEquals("key-" + i, cache.key(i));
assertEquals("value-" + i, cache.value(i));
}
}

@Test
void visitsEveryPairInOrder() {
CarrierCache cache = new CarrierCache();
for (int i = 0; i < 20; i++) {
cache.accept("key-" + i, "value-" + i);
}
List<String> seen = new ArrayList<>();
cache.forEachKeyValue(cache, (key, value) -> seen.add(key + "=" + value));

assertEquals(20, seen.size());
assertEquals("key-0=value-0", seen.get(0));
assertEquals("key-19=value-19", seen.get(19));
}

@Test
void emptyCache() {
CarrierCache cache = new CarrierCache();
List<String> seen = new ArrayList<>();
cache.forEachKeyValue(cache, (key, value) -> seen.add(key));
assertEquals(0, cache.size());
assertEquals(0, seen.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import datadog.context.propagation.CarrierKeys;
import datadog.context.propagation.CarrierSetter;
import datadog.context.propagation.IndexedCarrier;
import datadog.trace.api.Config;
import datadog.trace.api.DD128bTraceId;
import datadog.trace.api.DD64bTraceId;
Expand Down Expand Up @@ -371,21 +372,34 @@ private <C> void applyTraceContextToFirstContext(
}
}

/**
* The carrier's key/value pairs, read once so each extractor visits them without walking the
* carrier again. A carrier that is already an {@link IndexedCarrier} (the composite propagator's
* cache) is read in place; any other is copied.
*/
private static class ExtractionCache<C>
implements AgentPropagation.KeyClassifier,
AgentPropagation.ContextVisitor<ExtractionCache<?>> {
/** Cached context key-values (even indexes are header names, odd indexes are header values). */
/** The pairs, read in place; null when they were copied into {@link #keysAndValues}. */
private final IndexedCarrier indexed;

/** The copied pairs, keys at even indexes; null when {@link #indexed} is used. */
private final List<String> keysAndValues;

/**
* The parent span identifier from {@link DatadogHttpCodec#SPAN_ID_KEY} header formatted as 16
* hexadecimal characters, {@code null} if absent or invalid.
*/
private String datadogSpanIdHex;

public ExtractionCache(C carrier, AgentPropagation.ContextVisitor<C> getter) {
this.keysAndValues = new ArrayList<>(32);
getter.forEachKey(carrier, this);
if (carrier instanceof IndexedCarrier) {
this.indexed = (IndexedCarrier) carrier;
this.keysAndValues = null;
for (int i = 0, n = this.indexed.size(); i < n; i++) {
cacheDatadogSpanId(this.indexed.key(i), this.indexed.value(i));
}
} else {
this.indexed = null;
this.keysAndValues = new ArrayList<>(32);
getter.forEachKey(carrier, this);
}
}

@Override
Expand All @@ -412,6 +426,13 @@ private String getDatadogSpanIdHex() {

@Override
public void forEachKey(ExtractionCache<?> carrier, AgentPropagation.KeyClassifier classifier) {
IndexedCarrier indexed = carrier.indexed;
if (indexed != null) {
for (int i = 0, n = indexed.size(); i < n; i++) {
classifier.accept(indexed.key(i), indexed.value(i));
}
return;
}
List<String> keysAndValues = carrier.keysAndValues;
for (int i = 0; i < keysAndValues.size(); i += 2) {
classifier.accept(keysAndValues.get(i), keysAndValues.get(i + 1));
Expand Down