Improve injection/extraction styles configuration
MODIFY
dd-trace-api/src/main/java/datadog/trace/api/Config.java
dd-trace-api/src/main/java/datadog/trace/api/Config.java
@@ -3,7 +3,7 @@ package datadog.trace.api;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -32,8 +32,6 @@ public class Config {
private static final Pattern ENV_REPLACEMENT = Pattern.compile("[^a-zA-Z0-9_]");
- private static final Config INSTANCE = new Config();
-
public static final String SERVICE_NAME = "service.name";
public static final String SERVICE = "service";
public static final String WRITER_TYPE = "writer.type";
@@ -87,18 +85,21 @@ public class Config {
private static final boolean DEFAULT_TRACE_RESOLVER_ENABLED = true;
private static final boolean DEFAULT_HTTP_CLIENT_SPLIT_BY_DOMAIN = false;
private static final int DEFAULT_PARTIAL_FLUSH_MIN_SPANS = 0;
- private static final String DATADOG_HEADER_STYLE = "Datadog";
- private static final String B3_HEADER_STYLE = "B3";
- private static final String DEFAULT_PROPAGATION_STYLE_EXTRACT =
- DATADOG_HEADER_STYLE + " " + B3_HEADER_STYLE;
- private static final String DEFAULT_PROPAGATION_STYLE_INJECT =
- DATADOG_HEADER_STYLE + " " + B3_HEADER_STYLE;
+ private static final String DEFAULT_PROPAGATION_STYLE_EXTRACT = PropagationStyle.DATADOG.name();
+ private static final String DEFAULT_PROPAGATION_STYLE_INJECT = PropagationStyle.DATADOG.name();
private static final boolean DEFAULT_JMX_FETCH_ENABLED = false;
public static final int DEFAULT_JMX_FETCH_STATSD_PORT = 8125;
private static final boolean DEFAULT_APP_CUSTOM_LOG_MANAGER = false;
+ private static final String SPLIT_BY_SPACE_OR_COMMA_REGEX = "[,\\s]+";
+
+ public enum PropagationStyle {
+ DATADOG,
+ B3
+ }
+
/**
* this is a random UUID that gets generated on JVM start up and is attached to every root span
* and every JMX metric that is sent out.
@@ -120,10 +121,8 @@ public class Config {
@Getter private final boolean httpClientSplitByDomain;
@Getter private final Integer partialFlushMinSpans;
@Getter private final boolean runtimeContextFieldInjection;
- @Getter private final boolean extractDatadogHeaders;
- @Getter private final boolean extractB3Headers;
- @Getter private final boolean injectDatadogHeaders;
- @Getter private final boolean injectB3Headers;
+ @Getter private final Set<PropagationStyle> propagationStylesToExtract;
+ @Getter private final Set<PropagationStyle> propagationStylesToInject;
@Getter private final boolean jmxFetchEnabled;
@Getter private final List<String> jmxFetchMetricsConfigs;
@Getter private final Integer jmxFetchCheckPeriod;
@@ -169,17 +168,18 @@ public class Config {
getBooleanSettingFromEnvironment(
RUNTIME_CONTEXT_FIELD_INJECTION, DEFAULT_RUNTIME_CONTEXT_FIELD_INJECTION);
- final Set<String> propagationExtractStyles =
- getSpaceSeparatedStringSettingFromEnvironment(
- PROPAGATION_STYLE_EXTRACT, DEFAULT_PROPAGATION_STYLE_EXTRACT, true);
- extractDatadogHeaders = propagationExtractStyles.contains(DATADOG_HEADER_STYLE);
- extractB3Headers = propagationExtractStyles.contains(B3_HEADER_STYLE);
-
- final Set<String> propagationInjectStyles =
- getSpaceSeparatedStringSettingFromEnvironment(
- PROPAGATION_STYLE_INJECT, DEFAULT_PROPAGATION_STYLE_INJECT, true);
- injectDatadogHeaders = propagationInjectStyles.contains(DATADOG_HEADER_STYLE);
- injectB3Headers = propagationInjectStyles.contains(B3_HEADER_STYLE);
+ propagationStylesToExtract =
+ getEnumSetSettingFromEnvironment(
+ PROPAGATION_STYLE_EXTRACT,
+ DEFAULT_PROPAGATION_STYLE_EXTRACT,
+ PropagationStyle.class,
+ true);
+ propagationStylesToInject =
+ getEnumSetSettingFromEnvironment(
+ PROPAGATION_STYLE_INJECT,
+ DEFAULT_PROPAGATION_STYLE_INJECT,
+ PropagationStyle.class,
+ true);
jmxFetchEnabled =
getBooleanSettingFromEnvironment(JMX_FETCH_ENABLED, DEFAULT_JMX_FETCH_ENABLED);
@@ -234,25 +234,18 @@ public class Config {
getPropertyBooleanValue(
properties, RUNTIME_CONTEXT_FIELD_INJECTION, parent.runtimeContextFieldInjection);
- final Set<String> propagationExtractStyles =
- getPropertySpaceSeparatedString(properties, PROPAGATION_STYLE_EXTRACT);
- if (propagationExtractStyles == null) {
- extractDatadogHeaders = parent.extractDatadogHeaders;
- extractB3Headers = parent.extractB3Headers;
- } else {
- extractDatadogHeaders = propagationExtractStyles.contains(DATADOG_HEADER_STYLE);
- extractB3Headers = propagationExtractStyles.contains(B3_HEADER_STYLE);
- }
-
- final Set<String> propagationInjectStyles =
- getPropertySpaceSeparatedString(properties, PROPAGATION_STYLE_INJECT);
- if (propagationInjectStyles == null) {
- injectDatadogHeaders = parent.injectDatadogHeaders;
- injectB3Headers = parent.injectB3Headers;
- } else {
- injectDatadogHeaders = propagationInjectStyles.contains(DATADOG_HEADER_STYLE);
- injectB3Headers = propagationInjectStyles.contains(B3_HEADER_STYLE);
- }
+ final Set<PropagationStyle> parsedPropagationStylesToExtract =
+ getPropertySetValue(properties, PROPAGATION_STYLE_EXTRACT, PropagationStyle.class);
+ propagationStylesToExtract =
+ parsedPropagationStylesToExtract == null
+ ? parent.propagationStylesToExtract
+ : parsedPropagationStylesToExtract;
+ final Set<PropagationStyle> parsedPropagationStylesToInject =
+ getPropertySetValue(properties, PROPAGATION_STYLE_INJECT, PropagationStyle.class);
+ propagationStylesToInject =
+ parsedPropagationStylesToInject == null
+ ? parent.propagationStylesToInject
+ : parsedPropagationStylesToInject;
jmxFetchEnabled =
getPropertyBooleanValue(properties, JMX_FETCH_ENABLED, parent.jmxFetchEnabled);
@@ -412,34 +405,29 @@ public class Config {
/**
* Calls {@link #getSettingFromEnvironment(String, String)} and converts the result to a set of
- * strings splitting by space.
+ * strings splitting by space or comma.
*/
- private static Set<String> getSpaceSeparatedStringSettingFromEnvironment(
- final String name, final String defaultValue, final boolean emptyResultMeansUseDefault) {
+ private static <T extends Enum<T>> Set<T> getEnumSetSettingFromEnvironment(
+ final String name,
+ final String defaultValue,
+ final Class<T> clazz,
+ final boolean emptyResultMeansUseDefault) {
final String value = getSettingFromEnvironment(name, defaultValue);
- Set<String> result = splitStringIntoSetOfNonEmptyStrings(value, "\\s+");
+ Set<T> result =
+ convertStringSetToEnumSet(
+ parseStringIntoSetOfNonEmptyStrings(value, SPLIT_BY_SPACE_OR_COMMA_REGEX), clazz);
if (emptyResultMeansUseDefault && result.isEmpty()) {
// Treat empty parsing result as no value and use default instead
- result = splitStringIntoSetOfNonEmptyStrings(defaultValue, "\\s+");
+ result =
+ convertStringSetToEnumSet(
+ parseStringIntoSetOfNonEmptyStrings(defaultValue, SPLIT_BY_SPACE_OR_COMMA_REGEX),
+ clazz);
}
return result;
}
- private static Set<String> splitStringIntoSetOfNonEmptyStrings(
- final String str, final String regex) {
- final Set<String> result = new HashSet<>();
- // Java returns single value when splitting an empty string. We do not need that value, so
- // we need to throw it out.
- for (final String value : str.split(regex)) {
- if (!value.isEmpty()) {
- result.add(value);
- }
- }
- return Collections.unmodifiableSet(result);
- }
-
private static String propertyToEnvironmentName(final String name) {
return ENV_REPLACEMENT.matcher(name.toUpperCase()).replaceAll("_");
}
@@ -468,12 +456,19 @@ public class Config {
return value == null || value.trim().isEmpty() ? defaultValue : Integer.valueOf(value);
}
- private static Set<String> getPropertySpaceSeparatedString(
- final Properties properties, final String name) {
+ private static <T extends Enum<T>> Set<T> getPropertySetValue(
+ final Properties properties, final String name, final Class<T> clazz) {
final String value = properties.getProperty(name);
- return value == null
- ? null
- : Collections.unmodifiableSet(new HashSet<>(Arrays.asList(value.split("\\s+"))));
+ if (value != null) {
+ final Set<T> result =
+ convertStringSetToEnumSet(
+ parseStringIntoSetOfNonEmptyStrings(value, SPLIT_BY_SPACE_OR_COMMA_REGEX), clazz);
+ if (!result.isEmpty()) {
+ return result;
+ }
+ }
+ // null means parent value should be used
+ return null;
}
private static Map<String, String> parseMap(final String str, final String settingName) {
@@ -517,6 +512,37 @@ public class Config {
return Collections.unmodifiableList(Arrays.asList(tokens));
}
+ private static Set<String> parseStringIntoSetOfNonEmptyStrings(
+ final String str, final String regex) {
+ // Using LinkedHashSet to preserve original string order
+ final Set<String> result = new LinkedHashSet<>();
+ // Java returns single value when splitting an empty string. We do not need that value, so
+ // we need to throw it out.
+ for (final String value : str.split(regex)) {
+ if (!value.isEmpty()) {
+ result.add(value);
+ }
+ }
+ return Collections.unmodifiableSet(result);
+ }
+
+ private static <V extends Enum<V>> Set<V> convertStringSetToEnumSet(
+ final Set<String> input, final Class<V> clazz) {
+ // Using LinkedHashSet to preserve original string order
+ final Set<V> result = new LinkedHashSet<>();
+ for (final String value : input) {
+ try {
+ result.add(Enum.valueOf(clazz, value.toUpperCase()));
+ } catch (final IllegalArgumentException e) {
+ log.debug("Cannot recognize config string value: {}, {}", value, clazz);
+ }
+ }
+ return Collections.unmodifiableSet(result);
+ }
+
+ // This has to be placed after all other static fields to give them a chance to initialize
+ private static final Config INSTANCE = new Config();
+
public static Config get() {
return INSTANCE;
}

MODIFY
dd-trace-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy
dd-trace-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy
@@ -72,10 +72,8 @@ class ConfigTest extends Specification {
config.httpClientSplitByDomain == false
config.partialFlushMinSpans == 0
config.runtimeContextFieldInjection == true
- config.extractDatadogHeaders == true
- config.extractB3Headers == true
- config.injectDatadogHeaders == true
- config.injectB3Headers == true
+ config.propagationStylesToExtract.toList() == [Config.PropagationStyle.DATADOG]
+ config.propagationStylesToInject.toList() == [Config.PropagationStyle.DATADOG]
config.jmxFetchEnabled == false
config.jmxFetchMetricsConfigs == []
config.jmxFetchCheckPeriod == null
@@ -103,8 +101,8 @@ class ConfigTest extends Specification {
System.setProperty(PREFIX + HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "true")
System.setProperty(PREFIX + PARTIAL_FLUSH_MIN_SPANS, "15")
System.setProperty(PREFIX + RUNTIME_CONTEXT_FIELD_INJECTION, "false")
- System.setProperty(PREFIX + PROPAGATION_STYLE_EXTRACT, "Datadog")
- System.setProperty(PREFIX + PROPAGATION_STYLE_INJECT, "B3")
+ System.setProperty(PREFIX + PROPAGATION_STYLE_EXTRACT, "Datadog, B3")
+ System.setProperty(PREFIX + PROPAGATION_STYLE_INJECT, "B3, Datadog")
System.setProperty(PREFIX + JMX_FETCH_ENABLED, "true")
System.setProperty(PREFIX + JMX_FETCH_METRICS_CONFIGS, "/foo.yaml,/bar.yaml")
System.setProperty(PREFIX + JMX_FETCH_CHECK_PERIOD, "100")
@@ -130,10 +128,8 @@ class ConfigTest extends Specification {
config.httpClientSplitByDomain == true
config.partialFlushMinSpans == 15
config.runtimeContextFieldInjection == false
- config.extractDatadogHeaders == true
- config.extractB3Headers == false
- config.injectDatadogHeaders == false
- config.injectB3Headers == true
+ config.propagationStylesToExtract.toList() == [Config.PropagationStyle.DATADOG, Config.PropagationStyle.B3]
+ config.propagationStylesToInject.toList() == [Config.PropagationStyle.B3, Config.PropagationStyle.DATADOG]
config.jmxFetchEnabled == true
config.jmxFetchMetricsConfigs == ["/foo.yaml", "/bar.yaml"]
config.jmxFetchCheckPeriod == 100
@@ -146,8 +142,8 @@ class ConfigTest extends Specification {
setup:
environmentVariables.set(DD_SERVICE_NAME_ENV, "still something else")
environmentVariables.set(DD_WRITER_TYPE_ENV, "LoggingWriter")
- environmentVariables.set(DD_PROPAGATION_STYLE_EXTRACT, "B3")
- environmentVariables.set(DD_PROPAGATION_STYLE_INJECT, "Datadog")
+ environmentVariables.set(DD_PROPAGATION_STYLE_EXTRACT, "B3 Datadog")
+ environmentVariables.set(DD_PROPAGATION_STYLE_INJECT, "Datadog B3")
environmentVariables.set(DD_JMXFETCH_METRICS_CONFIGS_ENV, "some/file")
when:
@@ -156,10 +152,8 @@ class ConfigTest extends Specification {
then:
config.serviceName == "still something else"
config.writerType == "LoggingWriter"
- config.extractDatadogHeaders == false
- config.extractB3Headers == true
- config.injectDatadogHeaders == true
- config.injectB3Headers == false
+ config.propagationStylesToExtract.toList() == [Config.PropagationStyle.B3, Config.PropagationStyle.DATADOG]
+ config.propagationStylesToInject.toList() == [Config.PropagationStyle.DATADOG, Config.PropagationStyle.B3]
config.jmxFetchMetricsConfigs == ["some/file"]
}
@@ -197,7 +191,7 @@ class ConfigTest extends Specification {
System.setProperty(PREFIX + HEADER_TAGS, "1")
System.setProperty(PREFIX + SPAN_TAGS, "invalid")
System.setProperty(PREFIX + HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "invalid")
- System.setProperty(PREFIX + PROPAGATION_STYLE_EXTRACT, "")
+ System.setProperty(PREFIX + PROPAGATION_STYLE_EXTRACT, "some garbage")
System.setProperty(PREFIX + PROPAGATION_STYLE_INJECT, " ")
when:
@@ -214,10 +208,8 @@ class ConfigTest extends Specification {
config.mergedSpanTags == [:]
config.headerTags == [:]
config.httpClientSplitByDomain == false
- config.extractDatadogHeaders == true
- config.extractB3Headers == true
- config.injectDatadogHeaders == true
- config.injectB3Headers == true
+ config.propagationStylesToExtract.toList() == [Config.PropagationStyle.DATADOG]
+ config.propagationStylesToInject.toList() == [Config.PropagationStyle.DATADOG]
}
def "sys props and env vars overrides for trace_agent_port and agent_port_legacy as expected"() {
@@ -279,8 +271,8 @@ class ConfigTest extends Specification {
properties.setProperty(HEADER_TAGS, "e:5")
properties.setProperty(HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN, "true")
properties.setProperty(PARTIAL_FLUSH_MIN_SPANS, "15")
- properties.setProperty(PROPAGATION_STYLE_EXTRACT, "Datadog")
- properties.setProperty(PROPAGATION_STYLE_INJECT, "B3")
+ properties.setProperty(PROPAGATION_STYLE_EXTRACT, "B3 Datadog")
+ properties.setProperty(PROPAGATION_STYLE_INJECT, "Datadog B3")
properties.setProperty(JMX_FETCH_METRICS_CONFIGS, "/foo.yaml,/bar.yaml")
properties.setProperty(JMX_FETCH_CHECK_PERIOD, "100")
properties.setProperty(JMX_FETCH_REFRESH_BEANS_PERIOD, "200")
@@ -304,10 +296,8 @@ class ConfigTest extends Specification {
config.headerTags == [e: "5"]
config.httpClientSplitByDomain == true
config.partialFlushMinSpans == 15
- config.extractDatadogHeaders == true
- config.extractB3Headers == false
- config.injectDatadogHeaders == false
- config.injectB3Headers == true
+ config.propagationStylesToExtract.toList() == [Config.PropagationStyle.B3, Config.PropagationStyle.DATADOG]
+ config.propagationStylesToInject.toList() == [Config.PropagationStyle.DATADOG, Config.PropagationStyle.B3]
config.jmxFetchMetricsConfigs == ["/foo.yaml", "/bar.yaml"]
config.jmxFetchCheckPeriod == 100
config.jmxFetchRefreshBeansPeriod == 200
@@ -426,6 +416,7 @@ class ConfigTest extends Specification {
System.setProperty("dd.prop.zero.test", "0")
System.setProperty("dd.prop.float.test", "0.3")
System.setProperty("dd.float.test", "0.4")
+ System.setProperty("dd.garbage.test", "garbage")
System.setProperty("dd.negative.test", "-1")
expect:
@@ -439,6 +430,7 @@ class ConfigTest extends Specification {
"prop.float.test" | 0.3
"float.test" | 0.4
"negative.test" | -1.0
+ "garbage.test" | 10.0
"default.test" | 10.0
defaultValue = 10.0

MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/propagation/HttpCodec.java
dd-trace-ot/src/main/java/datadog/opentracing/propagation/HttpCodec.java
@@ -29,11 +29,16 @@ public class HttpCodec {
public static Injector createInjector(final Config config) {
final List<Injector> injectors = new ArrayList<>();
- if (config.isInjectDatadogHeaders()) {
+ for (final Config.PropagationStyle style : config.getPropagationStylesToInject()) {
+ if (style == Config.PropagationStyle.DATADOG) {
injectors.add(new DatadogHttpCodec.Injector());
+ continue;
}
- if (config.isInjectB3Headers()) {
+ if (style == Config.PropagationStyle.B3) {
injectors.add(new B3HttpCodec.Injector());
+ continue;
+ }
+ log.debug("No implementation found to inject propagation style: {}", style);
}
return new CompoundInjector(injectors);
}
@@ -41,11 +46,16 @@ public class HttpCodec {
public static Extractor createExtractor(
final Config config, final Map<String, String> taggedHeaders) {
final List<Extractor> extractors = new ArrayList<>();
- if (config.isExtractDatadogHeaders()) {
+ for (final Config.PropagationStyle style : config.getPropagationStylesToExtract()) {
+ if (style == Config.PropagationStyle.DATADOG) {
extractors.add(new DatadogHttpCodec.Extractor(taggedHeaders));
+ continue;
}
- if (config.isExtractB3Headers()) {
+ if (style == Config.PropagationStyle.B3) {
extractors.add(new B3HttpCodec.Extractor());
+ continue;
+ }
+ log.debug("No implementation found to extract propagation style: {}", style);
}
return new CompoundExtractor(extractors);
}

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/propagation/HttpExtractorTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/propagation/HttpExtractorTest.groovy
@@ -7,6 +7,8 @@ import spock.lang.Shared
import spock.lang.Specification
import static datadog.opentracing.propagation.HttpCodec.UINT64_MAX
+import static datadog.trace.api.Config.PropagationStyle.B3
+import static datadog.trace.api.Config.PropagationStyle.DATADOG
class HttpExtractorTest extends Specification {
@@ -16,8 +18,7 @@ class HttpExtractorTest extends Specification {
def "extract http headers"() {
setup:
Config config = Mock(Config) {
- isExtractDatadogHeaders() >> datadogEnabled
- isExtractB3Headers() >> b3Enabled
+ getPropagationStylesToExtract() >> styles
}
HttpCodec.Extractor extractor = HttpCodec.createExtractor(config, ["SOME_HEADER": "some-tag"])
@@ -59,22 +60,23 @@ class HttpExtractorTest extends Specification {
}
where:
- datadogEnabled | b3Enabled | datadogTraceId | datadogSpanId | b3TraceId | b3SpanId | expectedTraceId | expectedSpanId | putDatadogFields | expectDatadogFields | tagContext
- true | true | "1" | "2" | "a" | "b" | "1" | "2" | true | true | false
- true | true | null | null | "a" | "b" | "a" | "b" | false | false | true
- true | true | null | null | "a" | "b" | null | null | true | true | true
- true | false | "1" | "2" | "a" | "b" | "1" | "2" | true | true | false
- false | true | "1" | "2" | "a" | "b" | "10" | "11" | false | false | false
- false | false | "1" | "2" | "a" | "b" | null | null | false | false | false
- true | true | "abc" | "2" | "a" | "b" | "10" | "11" | false | false | false
- true | false | "abc" | "2" | "a" | "b" | null | null | false | false | false
+ styles | datadogTraceId | datadogSpanId | b3TraceId | b3SpanId | expectedTraceId | expectedSpanId | putDatadogFields | expectDatadogFields | tagContext
+ [DATADOG, B3] | "1" | "2" | "a" | "b" | "1" | "2" | true | true | false
+ [DATADOG, B3] | null | null | "a" | "b" | "a" | "b" | false | false | true
+ [DATADOG, B3] | null | null | "a" | "b" | null | null | true | true | true
+ [DATADOG] | "1" | "2" | "a" | "b" | "1" | "2" | true | true | false
+ [B3] | "1" | "2" | "a" | "b" | "10" | "11" | false | false | false
+ [B3, DATADOG] | "1" | "2" | "a" | "b" | "10" | "11" | false | false | false
+ [] | "1" | "2" | "a" | "b" | null | null | false | false | false
+ [DATADOG, B3] | "abc" | "2" | "a" | "b" | "10" | "11" | false | false | false
+ [DATADOG] | "abc" | "2" | "a" | "b" | null | null | false | false | false
- true | true | outOfRangeTraceId.toString() | "2" | "a" | "b" | "10" | "11" | false | false | false
- true | true | "1" | outOfRangeTraceId.toString() | "a" | "b" | "10" | "11" | false | false | false
- true | false | outOfRangeTraceId.toString() | "2" | "a" | "b" | null | null | false | false | false
- true | false | "1" | outOfRangeTraceId.toString() | "a" | "b" | null | null | false | false | false
- true | true | "1" | "2" | outOfRangeTraceId.toString() | "b" | "1" | "2" | true | false | false
- true | true | "1" | "2" | "a" | outOfRangeTraceId.toString() | "1" | "2" | true | false | false
+ [DATADOG, B3] | outOfRangeTraceId.toString() | "2" | "a" | "b" | "10" | "11" | false | false | false
+ [DATADOG, B3] | "1" | outOfRangeTraceId.toString() | "a" | "b" | "10" | "11" | false | false | false
+ [DATADOG] | outOfRangeTraceId.toString() | "2" | "a" | "b" | null | null | false | false | false
+ [DATADOG] | "1" | outOfRangeTraceId.toString() | "a" | "b" | null | null | false | false | false
+ [DATADOG, B3] | "1" | "2" | outOfRangeTraceId.toString() | "b" | "1" | "2" | true | false | false
+ [DATADOG, B3] | "1" | "2" | "a" | outOfRangeTraceId.toString() | "1" | "2" | true | false | false
}
}

MODIFY
dd-trace-ot/src/test/groovy/datadog/opentracing/propagation/HttpInjectorTest.groovy
dd-trace-ot/src/test/groovy/datadog/opentracing/propagation/HttpInjectorTest.groovy
@@ -9,13 +9,15 @@ import datadog.trace.common.writer.ListWriter
import io.opentracing.propagation.TextMapInjectAdapter
import spock.lang.Specification
+import static datadog.trace.api.Config.PropagationStyle.B3
+import static datadog.trace.api.Config.PropagationStyle.DATADOG
+
class HttpInjectorTest extends Specification {
def "inject http headers"() {
setup:
Config config = Mock(Config) {
- isInjectDatadogHeaders() >> datadogEnabled
- isInjectB3Headers() >> b3Enabled
+ getPropagationStylesToInject() >> styles
}
HttpCodec.Injector injector = HttpCodec.createInjector(config)
@@ -52,7 +54,7 @@ class HttpInjectorTest extends Specification {
injector.inject(mockedContext, new TextMapInjectAdapter(carrier))
then:
- if (datadogEnabled) {
+ if (styles.contains(DATADOG)) {
1 * carrier.put(DatadogHttpCodec.TRACE_ID_KEY, traceId)
1 * carrier.put(DatadogHttpCodec.SPAN_ID_KEY, spanId)
1 * carrier.put(DatadogHttpCodec.OT_BAGGAGE_PREFIX + "k1", "v1")
@@ -64,7 +66,7 @@ class HttpInjectorTest extends Specification {
1 * carrier.put(DatadogHttpCodec.ORIGIN_KEY, origin)
}
}
- if (b3Enabled) {
+ if (styles.contains(B3)) {
1 * carrier.put(B3HttpCodec.TRACE_ID_KEY, traceId)
1 * carrier.put(B3HttpCodec.SPAN_ID_KEY, spanId)
if (samplingPriority != PrioritySampling.UNSET) {
@@ -74,12 +76,13 @@ class HttpInjectorTest extends Specification {
0 * _
where:
- datadogEnabled | b3Enabled | samplingPriority | origin
- true | true | PrioritySampling.UNSET | null
- true | true | PrioritySampling.SAMPLER_KEEP | "saipan"
- true | false | PrioritySampling.UNSET | null
- true | false | PrioritySampling.SAMPLER_KEEP | "saipan"
- false | true | PrioritySampling.UNSET | null
- false | true | PrioritySampling.SAMPLER_KEEP | "saipan"
+ styles | samplingPriority | origin
+ [DATADOG, B3] | PrioritySampling.UNSET | null
+ [DATADOG, B3] | PrioritySampling.SAMPLER_KEEP | "saipan"
+ [DATADOG] | PrioritySampling.UNSET | null
+ [DATADOG] | PrioritySampling.SAMPLER_KEEP | "saipan"
+ [B3] | PrioritySampling.UNSET | null
+ [B3] | PrioritySampling.SAMPLER_KEEP | "saipan"
+ [B3, DATADOG] | PrioritySampling.SAMPLER_KEEP | "saipan"
}
}

Improve injection/extraction styles configuration