Skip to content

Commit

Permalink
AppSec/Profiling hooks for root span finished run only once
Browse files Browse the repository at this point in the history
We had some hooks in `CoreTracer.write` meant to be run whenever a root
span is finished. However, they were effectively called not just when
the root span finished, but also whenever a partial flush on a child
span was performed. This ended up calling the hooks multiple teams, and
earlier than expected.
  • Loading branch information
smola committed May 22, 2024
1 parent 08f3098 commit e080203
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 10 additions & 14 deletions dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ PropagationTags.Factory getPropagationTagsFactory() {
@Override
public void onRootSpanFinished(AgentSpan root, EndpointTracker tracker) {
profilingContextIntegration.onRootSpanFinished(root, tracker);
// request context is propagated to contexts in child spans
// Assume here that if present it will be so starting in the top span
RequestContext requestContext = root.getRequestContext();
if (requestContext != null) {
try {
requestContext.close();
} catch (IOException e) {
log.warn("Error closing request context data", e);
}
}
}

@Override
Expand Down Expand Up @@ -969,20 +979,6 @@ void write(final List<DDSpan> trace) {
// reporting fail without this, so will need to be fixed first.
writer.incrementDropCounts(writtenTrace.size());
}
if (null != rootSpan) {
onRootSpanFinished(rootSpan, rootSpan.getEndpointTracker());

// request context is propagated to contexts in child spans
// Assume here that if present it will be so starting in the top span
RequestContext requestContext = rootSpan.getRequestContext();
if (requestContext != null) {
try {
requestContext.close();
} catch (IOException e) {
log.warn("Error closing request context data", e);
}
}
}
}

private List<DDSpan> interceptCompleteTrace(List<DDSpan> trace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ PublishState onPublish(final DDSpan span) {
// write method.
healthMetrics.onFinishSpan();
COMPLETED_SPAN_COUNT.incrementAndGet(this);
return decrementRefAndMaybeWrite(span == getRootSpan());
final DDSpan rootSpan = getRootSpan();
if (span == rootSpan) {
tracer.onRootSpanFinished(rootSpan, rootSpan.getEndpointTracker());
}
return decrementRefAndMaybeWrite(span == rootSpan);
}

@Override
Expand Down

0 comments on commit e080203

Please sign in to comment.