-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RemoteAttributesSpanProcessor component to awsxray
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/RemoteAttributesSpanProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ray/src/test/java/io/opentelemetry/contrib/awsxray/RemoteAttributesSpanProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |