Skip to content

Commit

Permalink
Add span link attributes and fix var names
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerbenson committed Mar 3, 2023
1 parent 010acc0 commit 89d7bc6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.awslambdacore.v1_0.internal;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
Expand All @@ -28,6 +30,9 @@ public final class AwsXRayEnvSpanLinksExtractor implements SpanLinksExtractor<Aw
// lower-case map getter used for extraction
private static final String AWS_TRACE_HEADER_PROPAGATOR_KEY = "x-amzn-trace-id";

private static final Attributes LINK_ATTRIBUTES =
Attributes.of(AttributeKey.stringKey("source"), "x-ray-env");

@Override
public void extract(
SpanLinksBuilder spanLinks,
Expand All @@ -41,16 +46,16 @@ public static void extract(SpanLinksBuilder spanLinks) {
if (parentTraceHeader == null || parentTraceHeader.isEmpty()) {
return;
}
Context parentCtx =
Context xrayContext =
AwsXrayPropagator.getInstance()
.extract(
// see BaseTracer#extract() on why we're using root() here
Context.root(),
Collections.singletonMap(AWS_TRACE_HEADER_PROPAGATOR_KEY, parentTraceHeader),
MapGetter.INSTANCE);
SpanContext parent = Span.fromContext(parentCtx).getSpanContext();
if (parent.isValid()) {
spanLinks.addLink(parent);
SpanContext envVarSpanCtx = Span.fromContext(xrayContext).getSpanContext();
if (envVarSpanCtx.isValid()) {
spanLinks.addLink(envVarSpanCtx, LINK_ATTRIBUTES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.trace.data.StatusData;
Expand Down Expand Up @@ -108,8 +110,22 @@ void handlerLinksToInfrastructureTrace() {
span ->
span.hasName("my_function")
.hasKind(SpanKind.SERVER)
.hasTraceId("8a3c60f7d188f8fa79d48a391a778fa6")
.hasParentSpanId("0000000000000456")
.hasLinksSatisfying(
links ->
assertThat(links)
.singleElement()
.satisfies(
link -> {
assertThat(link.getSpanContext().getTraceId())
.isEqualTo("8a3c60f7d188f8fa79d48a391a778fa6");
assertThat(link.getSpanContext().getSpanId())
.isEqualTo("0000000000000456");
assertThat(link.getAttributes())
.isEqualTo(
Attributes.of(
AttributeKey.stringKey("source"),
"x-ray-env"));
}))
.hasAttributesSatisfying(
attrs ->
assertThat(attrs)
Expand Down

0 comments on commit 89d7bc6

Please sign in to comment.