Skip to content
Merged
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 @@ -25,15 +25,15 @@ class B3HttpCodec {

private static final Logger log = LoggerFactory.getLogger(B3HttpCodec.class);

private static final String B3_TRACE_ID = "b3.traceid";
private static final String B3_SPAN_ID = "b3.spanid";
static final String B3_TRACE_ID = "b3.traceid";
static final String B3_SPAN_ID = "b3.spanid";
static final String TRACE_ID_KEY = "X-B3-TraceId";
static final String SPAN_ID_KEY = "X-B3-SpanId";
static final String SAMPLING_PRIORITY_KEY = "X-B3-Sampled";
// See https://github.com/openzipkin/b3-propagation#single-header for b3 header documentation
static final String B3_KEY = "b3";
private static final String SAMPLING_PRIORITY_ACCEPT = String.valueOf(1);
private static final String SAMPLING_PRIORITY_DROP = String.valueOf(0);
static final String SAMPLING_PRIORITY_ACCEPT = String.valueOf(1);
static final String SAMPLING_PRIORITY_DROP = String.valueOf(0);

private B3HttpCodec() {
// This class should not be created. This also makes code coverage checks happy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HaystackHttpCodec {
static final String OT_BAGGAGE_PREFIX = "Baggage-";
static final String TRACE_ID_KEY = "Trace-ID";
static final String SPAN_ID_KEY = "Span-ID";
private static final String PARENT_ID_KEY = "Parent-ID";
static final String PARENT_ID_KEY = "Parent-ID";

static final String DD_TRACE_ID_BAGGAGE_KEY = OT_BAGGAGE_PREFIX + "Datadog-Trace-Id";
static final String DD_SPAN_ID_BAGGAGE_KEY = OT_BAGGAGE_PREFIX + "Datadog-Span-Id";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package datadog.trace.core.propagation;

import static datadog.trace.bootstrap.ActiveSubsystems.APPSEC_ACTIVE;
import static datadog.trace.bootstrap.instrumentation.api.ContextVisitors.stringValuesMap;
import static datadog.trace.core.propagation.B3HttpCodec.B3_KEY;
import static datadog.trace.core.propagation.B3HttpCodec.B3_SPAN_ID;
import static datadog.trace.core.propagation.B3HttpCodec.B3_TRACE_ID;
import static datadog.trace.core.propagation.B3HttpCodec.SAMPLING_PRIORITY_ACCEPT;
import static datadog.trace.core.propagation.B3HttpCodec.SAMPLING_PRIORITY_KEY;
import static datadog.trace.core.propagation.B3HttpCodec.SPAN_ID_KEY;
import static datadog.trace.core.propagation.B3HttpCodec.TRACE_ID_KEY;
Expand All @@ -12,12 +16,10 @@
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import datadog.trace.api.Config;
import datadog.trace.api.DDSpanId;
import datadog.trace.api.DynamicConfig;
import datadog.trace.bootstrap.ActiveSubsystems;
import datadog.trace.bootstrap.instrumentation.api.TagContext;
import datadog.trace.junit.utils.config.WithConfig;
import datadog.trace.junit.utils.tabletest.PrioritySamplingConverter;
Expand All @@ -33,7 +35,6 @@

@WithConfig(key = "propagation.extract.log_header_names.enabled", value = "true")
class B3HttpExtractorTest extends DDJavaSpecification {

private static final String SOME_HEADER = "SOME_HEADER";
private static final String SOME_TAG = "some-tag";
private static final String SOME_VALUE = "my-interesting-info";
Expand All @@ -50,14 +51,14 @@ void setup() {
.setHeaderTags(singletonMap(SOME_HEADER, SOME_TAG))
.setBaggageMapping(emptyMap())
.apply();
extractor = B3HttpCodec.newExtractor(Config.get(), dynamicConfig::captureTraceConfig);
origAppSecActive = ActiveSubsystems.APPSEC_ACTIVE;
ActiveSubsystems.APPSEC_ACTIVE = true;
this.extractor = B3HttpCodec.newExtractor(Config.get(), dynamicConfig::captureTraceConfig);
this.origAppSecActive = APPSEC_ACTIVE;
APPSEC_ACTIVE = true;
}

@AfterEach
void teardown() {
ActiveSubsystems.APPSEC_ACTIVE = origAppSecActive;
APPSEC_ACTIVE = this.origAppSecActive;
}

@TableTest({
Expand All @@ -72,8 +73,8 @@ void extractHttpHeaders(
String traceIdHex,
String spanIdHex,
Integer samplingPriority,
@ConvertWith(PrioritySamplingConverter.class) int expectedSamplingPriority) {
Map<String, String> headers = new LinkedHashMap<>();
@ConvertWith(PrioritySamplingConverter.class) byte expectedSamplingPriority) {
Map<String, String> headers = new HashMap<>();
headers.put("", "empty key");
headers.put(TRACE_ID_KEY.toUpperCase(), traceIdHex);
headers.put(SPAN_ID_KEY.toUpperCase(), spanIdHex);
Expand Down Expand Up @@ -103,7 +104,7 @@ void extractHttpHeadersWithB3HeaderAtTheBeginning(
String b3,
String expectedTraceIdHex,
long expectedSpanId,
@ConvertWith(PrioritySamplingConverter.class) int expectedSamplingPriority) {
@ConvertWith(PrioritySamplingConverter.class) byte expectedSamplingPriority) {
String traceIdHex = "1";
String spanIdHex = "2";
Map<String, String> headers = new LinkedHashMap<>();
Expand All @@ -112,9 +113,10 @@ void extractHttpHeadersWithB3HeaderAtTheBeginning(
headers.put(TRACE_ID_KEY.toUpperCase(), traceIdHex);
headers.put(SPAN_ID_KEY.toUpperCase(), spanIdHex);
headers.put(SOME_HEADER, SOME_VALUE);
headers.put(SAMPLING_PRIORITY_KEY, "1");
headers.put(SAMPLING_PRIORITY_KEY, SAMPLING_PRIORITY_ACCEPT);

ExtractedContext context = (ExtractedContext) extractor.extract(headers, stringValuesMap());
ExtractedContext context =
(ExtractedContext) this.extractor.extract(headers, stringValuesMap());

assertB3MultiOrSingleContext(
context, expectedTraceIdHex, expectedSpanId, expectedSamplingPriority);
Expand All @@ -131,7 +133,7 @@ void extractHttpHeadersWithB3HeaderAtTheEnd(
String b3,
String expectedTraceIdHex,
long expectedSpanId,
@ConvertWith(PrioritySamplingConverter.class) int expectedSamplingPriority) {
@ConvertWith(PrioritySamplingConverter.class) byte expectedSamplingPriority) {
String traceIdHex = "1";
String spanIdHex = "2";
Map<String, String> headers = new LinkedHashMap<>();
Expand All @@ -140,9 +142,9 @@ void extractHttpHeadersWithB3HeaderAtTheEnd(
headers.put(SPAN_ID_KEY.toUpperCase(), spanIdHex);
headers.put(B3_KEY, b3);
headers.put(SOME_HEADER, SOME_VALUE);
headers.put(SAMPLING_PRIORITY_KEY, "1");
headers.put(SAMPLING_PRIORITY_KEY, SAMPLING_PRIORITY_ACCEPT);

TagContext context = extractor.extract(headers, stringValuesMap());
TagContext context = this.extractor.extract(headers, stringValuesMap());

assertB3MultiOrSingleContext(
context, expectedTraceIdHex, expectedSpanId, expectedSamplingPriority);
Expand All @@ -163,8 +165,8 @@ private void assertB3MultiOrSingleContext(

private Map<String, Object> expectedB3Tags(TagContext context) {
Map<String, Object> expected = new HashMap<>();
expected.put("b3.traceid", ((B3TraceId) context.getTraceId()).getOriginal());
expected.put("b3.spanid", DDSpanId.toHexString(context.getSpanId()));
expected.put(B3_TRACE_ID, ((B3TraceId) context.getTraceId()).getOriginal());
expected.put(B3_SPAN_ID, DDSpanId.toHexString(context.getSpanId()));
expected.put(SOME_TAG, SOME_VALUE);
return expected;
}
Expand All @@ -186,31 +188,34 @@ private Map<String, Object> expectedB3Tags(TagContext context) {
})
void extract128BitIdTruncatesIdTo64Bit(
String traceId, String spanId, String expectedTraceIdHex, Long expectedSpanId) {
Map<String, String> headers = new LinkedHashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put(TRACE_ID_KEY.toUpperCase(), traceId);
headers.put(SPAN_ID_KEY.toUpperCase(), spanId);

TagContext context = extractor.extract(headers, stringValuesMap());
TagContext context = this.extractor.extract(headers, stringValuesMap());

if (expectedTraceIdHex != null) {
assertInstanceOf(ExtractedContext.class, context);
B3TraceId expectedTraceId = B3TraceId.fromHex(expectedTraceIdHex);
assertEquals(expectedTraceId, context.getTraceId());
long spanIdValue = expectedSpanId == null ? 0L : expectedSpanId;
assertEquals(spanIdValue, context.getSpanId());
assertEquals(expectedTraceId.getOriginal(), context.getTags().get("b3.traceid"));
Object expectedSpanIdTag =
expectedSpanId == null ? null : DDSpanId.toHexString(expectedSpanId);
assertEquals(expectedSpanIdTag, context.getTags().get("b3.spanid"));
} else {
assertTrue(context == null || !(context instanceof ExtractedContext));
assertEquals(expectedTraceId.getOriginal(), context.getTags().getString(B3_TRACE_ID));
if (expectedSpanId == null) {
assertNull(context.getTags().getString(B3_SPAN_ID));
} else {
assertEquals(DDSpanId.toHexString(expectedSpanId), context.getTags().getString(B3_SPAN_ID));
}
} else if (context != null) {
assertInstanceOf(TagContext.class, context);
assertFalse(context instanceof ExtractedContext);
}
}

@Test
void extractHeaderTagsWithNoPropagation() {
TagContext context =
extractor.extract(singletonMap(SOME_HEADER, SOME_VALUE), stringValuesMap());
this.extractor.extract(singletonMap(SOME_HEADER, SOME_VALUE), stringValuesMap());

assertFalse(context instanceof ExtractedContext);
assertEquals(singletonMap(SOME_TAG, SOME_VALUE), context.getTags());
Expand All @@ -220,7 +225,7 @@ void extractHeaderTagsWithNoPropagation() {
void extractHeadersWithForwarding() {
String forwarded = "for=" + FORWARDED_IP + ":" + FORWARDED_PORT;
Map<String, String> tagOnlyCtx = singletonMap("Forwarded", forwarded);
Map<String, String> fullCtx = new LinkedHashMap<>();
Map<String, String> fullCtx = new HashMap<>();
fullCtx.put(TRACE_ID_KEY.toUpperCase(), "1");
fullCtx.put(SPAN_ID_KEY.toUpperCase(), "2");
fullCtx.put("Forwarded", forwarded);
Expand All @@ -231,7 +236,7 @@ void extractHeadersWithForwarding() {
assertFalse(context instanceof ExtractedContext);
assertEquals(forwarded, context.getForwarded());

context = extractor.extract(fullCtx, stringValuesMap());
context = this.extractor.extract(fullCtx, stringValuesMap());

assertInstanceOf(ExtractedContext.class, context);
assertEquals(1L, context.getTraceId().toLong());
Expand All @@ -241,10 +246,10 @@ void extractHeadersWithForwarding() {

@Test
void extractHeadersWithXForwarding() {
Map<String, String> tagOnlyCtx = new LinkedHashMap<>();
Map<String, String> tagOnlyCtx = new HashMap<>();
tagOnlyCtx.put("X-Forwarded-For", FORWARDED_IP);
tagOnlyCtx.put("X-Forwarded-Port", FORWARDED_PORT);
Map<String, String> fullCtx = new LinkedHashMap<>();
Map<String, String> fullCtx = new HashMap<>();
fullCtx.put(TRACE_ID_KEY.toUpperCase(), "1");
fullCtx.put(SPAN_ID_KEY.toUpperCase(), "2");
fullCtx.put("x-forwarded-for", FORWARDED_IP);
Expand Down Expand Up @@ -273,7 +278,7 @@ void extractEmptyHeadersReturnsNull() {

@Test
void extractHttpHeadersWithInvalidNonNumericId() {
Map<String, String> headers = new LinkedHashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put(TRACE_ID_KEY.toUpperCase(), "traceId");
headers.put(SPAN_ID_KEY.toUpperCase(), "spanId");
headers.put(SOME_HEADER, SOME_VALUE);
Expand All @@ -286,7 +291,7 @@ void extractHttpHeadersWithInvalidNonNumericId() {

@Test
void extractHttpHeadersWithOutOfRangeSpanId() {
Map<String, String> headers = new LinkedHashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put(TRACE_ID_KEY.toUpperCase(), "0");
headers.put(SPAN_ID_KEY.toUpperCase(), "-1");
headers.put(SOME_HEADER, SOME_VALUE);
Expand All @@ -309,7 +314,7 @@ void extractHttpHeadersWithOutOfRangeSpanId() {
})
void extractIdsWhileRetainingTheOriginalString(
String traceId, String spanId, long expectedSpanId) {
Map<String, String> headers = new LinkedHashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put(TRACE_ID_KEY.toUpperCase(), traceId);
headers.put(SPAN_ID_KEY.toUpperCase(), spanId);
B3TraceId expectedTraceId = B3TraceId.fromHex(traceId);
Expand All @@ -336,7 +341,7 @@ private static String trimmed(String hex) {

@Test
void extractCommonHttpHeaders() {
Map<String, String> headers = new LinkedHashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put(HttpCodec.USER_AGENT_KEY, "some-user-agent");
headers.put(HttpCodec.X_CLUSTER_CLIENT_IP_KEY, "1.1.1.1");
headers.put(HttpCodec.X_REAL_IP_KEY, "2.2.2.2");
Expand Down

This file was deleted.

Loading