Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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,12 +1,14 @@
package datadog.trace.core.baggage;

import static datadog.trace.api.TracePropagationBehaviorExtract.IGNORE;
import static java.util.Collections.emptyMap;

import datadog.context.Context;
import datadog.context.propagation.CarrierSetter;
import datadog.context.propagation.CarrierVisitor;
import datadog.context.propagation.Propagator;
import datadog.trace.api.Config;
import datadog.trace.api.TracePropagationBehaviorExtract;
import datadog.trace.bootstrap.instrumentation.api.Baggage;
import datadog.trace.core.util.PercentEscaper;
import datadog.trace.core.util.PercentEscaper.Escaped;
Expand All @@ -28,21 +30,29 @@ public class BaggagePropagator implements Propagator {
private final boolean extractBaggage;
private final int maxItems;
private final int maxBytes;
private final TracePropagationBehaviorExtract behaviorExtract;

public BaggagePropagator(Config config) {
this(
config.isBaggageInject(),
config.isBaggageInject(),
config.getTraceBaggageMaxItems(),
config.getTraceBaggageMaxBytes());
config.getTraceBaggageMaxBytes(),
config.getTracePropagationBehaviorExtract());
}

// use primarily for testing purposes
BaggagePropagator(boolean injectBaggage, boolean extractBaggage, int maxItems, int maxBytes) {
BaggagePropagator(
boolean injectBaggage,
boolean extractBaggage,
int maxItems,
int maxBytes,
TracePropagationBehaviorExtract behaviorExtract) {
this.injectBaggage = injectBaggage;
this.extractBaggage = extractBaggage;
this.maxItems = maxItems;
this.maxBytes = maxBytes;
this.behaviorExtract = behaviorExtract;
}

@Override
Expand Down Expand Up @@ -104,7 +114,11 @@ public <C> void inject(Context context, C carrier, CarrierSetter<C> setter) {

@Override
public <C> Context extract(Context context, C carrier, CarrierVisitor<C> visitor) {
if (!this.extractBaggage || context == null || carrier == null || visitor == null) {
if (!this.extractBaggage
|| this.behaviorExtract == IGNORE
|| context == null
|| carrier == null
|| visitor == null) {
return context;
}
BaggageExtractor baggageExtractor = new BaggageExtractor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.util.function.BiConsumer

import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_BAGGAGE_MAX_BYTES
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_BAGGAGE_MAX_ITEMS
import static datadog.trace.api.TracePropagationBehaviorExtract.CONTINUE
import static datadog.trace.core.baggage.BaggagePropagator.BAGGAGE_KEY

class BaggagePropagatorTest extends DDSpecification {
Expand All @@ -35,7 +36,7 @@ class BaggagePropagatorTest extends DDSpecification {
}

def setup() {
this.propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, DEFAULT_TRACE_BAGGAGE_MAX_BYTES)
this.propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, DEFAULT_TRACE_BAGGAGE_MAX_BYTES, CONTINUE)
this.setter = new MapCarrierAccessor()
this.carrier = [:]
this.context = Context.root()
Expand Down Expand Up @@ -65,7 +66,7 @@ class BaggagePropagatorTest extends DDSpecification {

def "test baggage inject item limit"() {
setup:
propagator = new BaggagePropagator(true, true, 2, DEFAULT_TRACE_BAGGAGE_MAX_BYTES) //creating a new instance after injecting config
propagator = new BaggagePropagator(true, true, 2, DEFAULT_TRACE_BAGGAGE_MAX_BYTES, CONTINUE) //creating a new instance after injecting config
context = Baggage.create(baggage).storeInto(context)

when:
Expand All @@ -82,7 +83,7 @@ class BaggagePropagatorTest extends DDSpecification {

def "test baggage inject bytes limit"() {
setup:
propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, 20) //creating a new instance after injecting config
propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, 20, CONTINUE) //creating a new instance after injecting config
context = Baggage.create(baggage).storeInto(context)

when:
Expand Down Expand Up @@ -184,7 +185,7 @@ class BaggagePropagatorTest extends DDSpecification {

def "test baggage cache items limit"(){
setup:
propagator = new BaggagePropagator(true, true, 2, DEFAULT_TRACE_BAGGAGE_MAX_BYTES) //creating a new instance after injecting config
propagator = new BaggagePropagator(true, true, 2, DEFAULT_TRACE_BAGGAGE_MAX_BYTES, CONTINUE) //creating a new instance after injecting config
def headers = [
(BAGGAGE_KEY) : baggageHeader,
]
Expand All @@ -205,7 +206,7 @@ class BaggagePropagatorTest extends DDSpecification {

def "test baggage cache bytes limit"(){
setup:
propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, 20) //creating a new instance after injecting config
propagator = new BaggagePropagator(true, true, DEFAULT_TRACE_BAGGAGE_MAX_ITEMS, 20, CONTINUE) //creating a new instance after injecting config
def headers = [
(BAGGAGE_KEY) : baggageHeader,
]
Expand Down