Skip to content

Commit

Permalink
feat(context): Introduce compositor extractor carrier cache
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectSlayer committed Jan 6, 2025
1 parent af5d4e6 commit 094c0d3
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package datadog.context.propagation;

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

class CompositePropagator implements Propagator {
private final Propagator[] propagators;
Expand All @@ -18,9 +21,35 @@ 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 carrier key/value pairs
CarrierCache carrierCache = new CarrierCache();
visitor.forEachKeyValue(carrier, carrierCache);
// Run the multiple extractions on cache
for (Propagator propagator : this.propagators) {
context = propagator.extract(context, carrier, visitor);
context = propagator.extract(context, carrierCache, carrierCache);
}
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;

public CarrierCache() {
this.keysAndValues = new ArrayList<>(32);
}

@Override
public void accept(String key, String value) {
this.keysAndValues.add(key);
this.keysAndValues.add(value);
}

@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));
}
}
}
}

0 comments on commit 094c0d3

Please sign in to comment.