Skip to content

Commit

Permalink
Add RemoteAttributesSpanProcessor component to awsxray
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrara committed Mar 13, 2023
1 parent 04a2a48 commit 410b08b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.awsxray;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

/**
* RemoteAttributesSpanProcessor copies attributes aws.remote.application and aws.remote.operation
* from the parent span to children after receiving them. These two attributes will be used as
* RemoteApplication and RemoteOperation dimension values in dependency metrics generation.
*/
public class RemoteAttributesSpanProcessor implements SpanProcessor {

private static final AttributeKey<String> REMOTE_APPLICATION =
AttributeKey.stringKey("aws.remote.application");
private static final AttributeKey<String> REMOTE_OPERATION =
AttributeKey.stringKey("aws.remote.operation");

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
Span parentSpan = Span.fromContextOrNull(parentContext);
if (!(parentSpan instanceof ReadableSpan)) {
return;
}
ReadableSpan parentReadableSpan = (ReadableSpan) parentSpan;

String remoteApplication = parentReadableSpan.getAttribute(REMOTE_APPLICATION);
if (remoteApplication != null) {
span.setAttribute(REMOTE_APPLICATION, remoteApplication);
}

String remoteOperation = parentReadableSpan.getAttribute(REMOTE_OPERATION);
if (remoteOperation != null) {
span.setAttribute(REMOTE_OPERATION, remoteOperation);
}
}

@Override
public boolean isStartRequired() {
return true;
}

@Override
public void onEnd(ReadableSpan span) {}

@Override
public boolean isEndRequired() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.awsxray;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class RemoteAttributesSpanProcessorTest {

private static final String REMOTE_APPLICATION = "aws.remote.application";
private static final String REMOTE_OPERATION = "aws.remote.operation";

private Tracer tracer;

@BeforeEach
public void setup() {
tracer =
SdkTracerProvider.builder()
.addSpanProcessor(new RemoteAttributesSpanProcessor())
.build()
.get("awsxray");
}

@Test
public void testRemoteAttributesInheritance() {
Span span = tracer.spanBuilder("parent").startSpan();

span.setAttribute(REMOTE_APPLICATION, "testApplication");
span.setAttribute(REMOTE_OPERATION, "testOperation");

ReadableSpan leafSpan = (ReadableSpan) createNestedSpan(span, 10);

assertThat(leafSpan.getParentSpanContext()).isNotNull();
assertThat(leafSpan.getName()).isEqualTo("child:1");
assertThat(leafSpan.getAttribute(AttributeKey.stringKey(REMOTE_APPLICATION)))
.isEqualTo("testApplication");
assertThat(leafSpan.getAttribute(AttributeKey.stringKey(REMOTE_OPERATION)))
.isEqualTo("testOperation");
}

private Span createNestedSpan(Span parentSpan, int depth) {
if (depth == 0) {
return parentSpan;
}
Span childSpan =
tracer
.spanBuilder("child:" + depth)
.setParent(Context.current().with(parentSpan))
.startSpan();
try {
return createNestedSpan(childSpan, depth - 1);
} finally {
childSpan.end();
}
}
}

0 comments on commit 410b08b

Please sign in to comment.