Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read tracing info from system properties #8368

Merged
merged 3 commits into from
May 19, 2023
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
Expand Up @@ -26,7 +26,7 @@
final class AwsXrayEnvSpanLinksExtractor implements SpanLinksExtractor<AwsLambdaRequest> {

private static final String AWS_TRACE_HEADER_ENV_KEY = "_X_AMZN_TRACE_ID";

private static final String AWS_TRACE_HEADER_PROP = "com.amazonaws.xray.traceHeader";
// lower-case map getter used for extraction
private static final String AWS_TRACE_HEADER_PROPAGATOR_KEY = "x-amzn-trace-id";

Expand All @@ -42,22 +42,32 @@ public void extract(
}

public static void extract(SpanLinksBuilder spanLinks) {
String parentTraceHeader = System.getenv(AWS_TRACE_HEADER_ENV_KEY);
if (parentTraceHeader == null || parentTraceHeader.isEmpty()) {
Map<String, String> contextMap = getSystemPropertyOrEnvironmentVariable();
if (contextMap.isEmpty()) {
return;
}
Context xrayContext =
AwsXrayPropagator.getInstance()
.extract(
Context.root(),
Collections.singletonMap(AWS_TRACE_HEADER_PROPAGATOR_KEY, parentTraceHeader),
MapGetter.INSTANCE);
AwsXrayPropagator.getInstance().extract(Context.root(), contextMap, MapGetter.INSTANCE);
SpanContext envVarSpanCtx = Span.fromContext(xrayContext).getSpanContext();
if (envVarSpanCtx.isValid()) {
spanLinks.addLink(envVarSpanCtx, LINK_ATTRIBUTES);
}
}

private static Map<String, String> getSystemPropertyOrEnvironmentVariable() {
String traceHeader = System.getProperty(AWS_TRACE_HEADER_PROP);
jdoherty marked this conversation as resolved.
Show resolved Hide resolved
if (isEmptyOrNull(traceHeader)) {
traceHeader = System.getenv(AWS_TRACE_HEADER_ENV_KEY);
}
return isEmptyOrNull(traceHeader)
? Collections.emptyMap()
: Collections.singletonMap(AWS_TRACE_HEADER_PROPAGATOR_KEY, traceHeader);
}

public static boolean isEmptyOrNull(String value) {
return value == null || value.isEmpty();
}

private enum MapGetter implements TextMapGetter<Map<String, String>> {
INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
Expand All @@ -32,27 +33,68 @@ class AwsXrayEnvSpanLinksExtractorTest {
Attributes.of(AttributeKey.stringKey("source"), "x-ray-env");

@SystemStub final EnvironmentVariables environmentVariables = new EnvironmentVariables();
@SystemStub final SystemProperties systemProperties = new SystemProperties();

@Test
void shouldIgnoreIfEnvVarEmpty() {
void shouldIgnoreIfEnvVarAndSystemPropertyEmpty() {
// given
SpanLinksBuilder spanLinksBuilder = mock(SpanLinksBuilder.class);
environmentVariables.set("_X_AMZN_TRACE_ID", "");

systemProperties.set("com.amazonaws.xray.traceHeader", "");
// when
AwsXrayEnvSpanLinksExtractor.extract(spanLinksBuilder);
// then
verifyNoInteractions(spanLinksBuilder);
}

@Test
void shouldLinkAwsParentHeaderAndChooseSystemPropertyIfValidAndNotSampled() {
// given
SpanLinksBuilder spanLinksBuilder = mock(SpanLinksBuilder.class);
environmentVariables.set(
"_X_AMZN_TRACE_ID",
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=0");
systemProperties.set(
"com.amazonaws.xray.traceHeader",
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa7;Parent=0000000000000789;Sampled=0");
// when
AwsXrayEnvSpanLinksExtractor.extract(spanLinksBuilder);
// then
ArgumentCaptor<SpanContext> captor = ArgumentCaptor.forClass(SpanContext.class);
verify(spanLinksBuilder).addLink(captor.capture(), eq(EXPECTED_LINK_ATTRIBUTES));
SpanContext spanContext = captor.getValue();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.isSampled()).isFalse();
assertThat(spanContext.getSpanId()).isEqualTo("0000000000000789");
assertThat(spanContext.getTraceId()).isEqualTo("8a3c60f7d188f8fa79d48a391a778fa7");
}

@Test
void shouldLinkAwsParentHeaderIfValidAndNotSampled() {
// given
SpanLinksBuilder spanLinksBuilder = mock(SpanLinksBuilder.class);
environmentVariables.set(
"_X_AMZN_TRACE_ID",
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=0");
// when
AwsXrayEnvSpanLinksExtractor.extract(spanLinksBuilder);
// then
ArgumentCaptor<SpanContext> captor = ArgumentCaptor.forClass(SpanContext.class);
verify(spanLinksBuilder).addLink(captor.capture(), eq(EXPECTED_LINK_ATTRIBUTES));
SpanContext spanContext = captor.getValue();
assertThat(spanContext.isValid()).isTrue();
assertThat(spanContext.isSampled()).isFalse();
assertThat(spanContext.getSpanId()).isEqualTo("0000000000000456");
assertThat(spanContext.getTraceId()).isEqualTo("8a3c60f7d188f8fa79d48a391a778fa6");
}

@Test
void shouldLinkAwsParentHeaderIfValidAndNotSampledSystemProperty() {
// given
SpanLinksBuilder spanLinksBuilder = mock(SpanLinksBuilder.class);
systemProperties.set(
"com.amazonaws.xray.traceHeader",
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=0");
// when
AwsXrayEnvSpanLinksExtractor.extract(spanLinksBuilder);
// then
Expand All @@ -66,13 +108,12 @@ void shouldLinkAwsParentHeaderIfValidAndNotSampled() {
}

@Test
void shouldLinkAwsParentHeaderIfValidAndSampled() {
void shouldLinkAwsParentHeaderIfValidAndSampledSystemProperty() {
// given
SpanLinksBuilder spanLinksBuilder = mock(SpanLinksBuilder.class);
environmentVariables.set(
"_X_AMZN_TRACE_ID",
systemProperties.set(
"com.amazonaws.xray.traceHeader",
"Root=1-8a3c60f7-d188f8fa79d48a391a778fa6;Parent=0000000000000456;Sampled=1");

// when
AwsXrayEnvSpanLinksExtractor.extract(spanLinksBuilder);
// then
Expand Down