Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -37,17 +37,20 @@ public void apply(Context context, ReadWriteSpan span) {
if (ctx == null) {
return;
}
if (ctx.getAccountId() != null) {
span.setAttribute(APPD_ATTR_ACCT, ctx.getAccountId());
}
if (ctx.getAppId() != null) {
span.setAttribute(APPD_ATTR_APP, ctx.getAppId());
}
if (ctx.getBusinessTransactionId() != null) {
span.setAttribute(APPD_ATTR_BT, ctx.getBusinessTransactionId());
}
if (ctx.getTierId() != null) {
span.setAttribute(APPD_ATTR_TIER, ctx.getTierId());
// Set attributes only for the root span
if (span.getParentSpanContext() == null) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is not what you really want. Instead of the root span you want the local root span, the first span in the trace for this service. Secondly can span.getParentSpanContext() be null at all? I'd guess that it would return an invalid span context (trace and span id are all zeros) instead. For ideas see https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/8b7e4f2bedc08cdcda2629b2b486bc9b8fd40851/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/LocalRootSpan.java#L49

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion. Thanks!

if (ctx.getAccountId() != null) {
span.setAttribute(APPD_ATTR_ACCT, ctx.getAccountId());
}
if (ctx.getAppId() != null) {
span.setAttribute(APPD_ATTR_APP, ctx.getAppId());
}
if (ctx.getBusinessTransactionId() != null) {
span.setAttribute(APPD_ATTR_BT, ctx.getBusinessTransactionId());
}
if (ctx.getTierId() != null) {
span.setAttribute(APPD_ATTR_TIER, ctx.getTierId());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,58 @@
import static com.splunk.opentelemetry.appd.AppdBonusSpanProcessor.APPD_ATTR_BT;
import static com.splunk.opentelemetry.appd.AppdBonusSpanProcessor.APPD_ATTR_TIER;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import org.junit.jupiter.api.Test;

class AppdBonusSpanProcessorTest {

@Test
void onStart() {
void shouldSetAppdAttributesOnRootSpan() {
// Given
Context context = mock();

AppdBonusContext appdContext = new AppdBonusContext("myacct", "myapp", "mybt", "mytier");
ReadWriteSpan span = mock();

when(context.get(CONTEXT_KEY)).thenReturn(appdContext);

ReadWriteSpan span = mock();

AppdBonusSpanProcessor testClass = new AppdBonusSpanProcessor();

// When
testClass.apply(context, span);

// Then
verify(span).setAttribute(APPD_ATTR_ACCT, "myacct");
verify(span).setAttribute(APPD_ATTR_APP, "myapp");
verify(span).setAttribute(APPD_ATTR_BT, "mybt");
verify(span).setAttribute(APPD_ATTR_TIER, "mytier");
}

@Test
void shouldNotSetAppdAttributesOnNestedSpans() {
// Given
Context context = mock();
AppdBonusContext appdContext = new AppdBonusContext("myacct", "myapp", "mybt", "mytier");
when(context.get(CONTEXT_KEY)).thenReturn(appdContext);

ReadWriteSpan span = mock();
SpanContext parentSpanContext = mock(SpanContext.class);
when(span.getParentSpanContext()).thenReturn(parentSpanContext);

AppdBonusSpanProcessor testClass = new AppdBonusSpanProcessor();

// When
testClass.apply(context, span);

// Then
verify(span, never()).setAttribute(APPD_ATTR_ACCT, "myacct");
verify(span, never()).setAttribute(APPD_ATTR_APP, "myapp");
verify(span, never()).setAttribute(APPD_ATTR_BT, "mybt");
verify(span, never()).setAttribute(APPD_ATTR_TIER, "mytier");
}
}