Skip to content

Commit 812c2a4

Browse files
committed
feat(profiling): capture span execution thread at finish time in DDSpanContext
1 parent b765b79 commit 812c2a4

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ private void finishAndAddToTrace(final long durationNano) {
160160
wrapper.onSpanFinished();
161161
}
162162
this.metrics.onSpanFinished();
163+
// Capture the execution thread while still on the span's own finishing thread.
164+
// CoreTracer.write() is called later from the event loop; the info captured here is the
165+
// authoritative source for SpanNode thread attribution (see SpanExecutionThreadEvent).
166+
context.captureExecutionThread(
167+
Thread.currentThread().getId(), Thread.currentThread().getName());
163168
TraceCollector.PublishState publishState = context.getTraceCollector().onPublish(this);
164169
log.debug("Finished span ({}): {}", publishState, this);
165170
} else {

dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public class DDSpanContext
160160
private final boolean injectBaggageAsTags;
161161
private volatile int encodedOperationName;
162162
private volatile int encodedResourceName;
163+
private volatile long executionThreadId = 0;
164+
private volatile String executionThreadName = "";
163165

164166
/**
165167
* Metastruct keys are associated to the current span, they will not propagate to the children
@@ -387,6 +389,25 @@ public int getEncodedResourceName() {
387389
return encodedResourceName;
388390
}
389391

392+
public ProfilingContextIntegration getProfilingContextIntegration() {
393+
return profilingContextIntegration;
394+
}
395+
396+
void captureExecutionThread(long threadId, String threadName) {
397+
this.executionThreadId = threadId;
398+
this.executionThreadName = threadName;
399+
}
400+
401+
@Override
402+
public long getExecutionThreadId() {
403+
return executionThreadId;
404+
}
405+
406+
@Override
407+
public String getExecutionThreadName() {
408+
return executionThreadName;
409+
}
410+
390411
public String getServiceName() {
391412
return serviceName;
392413
}

0 commit comments

Comments
 (0)