Skip to content

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented Oct 4, 2025

What does this PR do?

Fixes an unintended behavior of Datadog::Tracing.continue_from!(digest, &block) where if multiple local root spans are created inside a the block, only the first one is associated with the digest. Any subsequence spans inside that block are associated with a new, unrelated trace.

Effectively, this PR makes this block do what it looks like it does:

# Capture TraceDigest
digest = Datadog::Tracing.active_trace.to_digest

# Then in another thread
Datadog::Tracing.continue_trace!(digest) do
  tracer.trace('first.span') {} # Both spans are associated
  tracer.trace('second.span') {} # with the digest
end

Before this PR, only the first.span would be associated with the digest.

Motivation:

While trying to address #3465, I wasn't able to get all background spans to be associated with the active span in the main calling thread. This was due to the bug that this PR fixes.

Change log entry

Yes. Tracing.continue_from! with a block maintains active trace until the block ends.

@marcotc marcotc requested review from a team as code owners October 4, 2025 01:14
@marcotc marcotc requested a review from vpellan October 4, 2025 01:14
Copy link

github-actions bot commented Oct 4, 2025

Thank you for updating Change log entry section 👏

Visited at: 2025-10-04 02:07:44 UTC

@marcotc marcotc force-pushed the fix-trace-auto-finish branch from 91fa0bb to eb4376a Compare October 4, 2025 01:26
@pr-commenter
Copy link

pr-commenter bot commented Oct 4, 2025

Benchmarks

Benchmark execution time: 2025-10-04 01:56:31

Comparing candidate commit eb4376a in PR branch fix-trace-auto-finish with baseline commit eae7fc6 in branch master.

Found 0 performance improvements and 1 performance regressions! Performance is the same for 43 metrics, 2 unstable metrics.

scenario:tracing - Tracing.log_correlation

  • 🟥 throughput [-7981.466op/s; -7693.495op/s] or [-7.359%; -7.094%]

Copy link

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage
Patch Coverage: 103.27%
Total Coverage: 101.97% (-0.01%)

View detailed report

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: eb4376a | Docs | Was this helpful? Give us feedback!

Copy link
Contributor

@maycmlee maycmlee left a comment

Choose a reason for hiding this comment

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

Nothing for docs to review

Copy link
Member

@p-datadog p-datadog left a comment

Choose a reason for hiding this comment

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

I think this branch is missing #4925, if so it should have master merged into it and formatters rerun.

@events = events || Events.new
@finished = false
@spans = []
@auto_finish = auto_finish
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@auto_finish = auto_finish
@auto_finish = !!auto_finish

start_trace(continue_from: continue_from)
else
active_trace
end
Copy link
Member

Choose a reason for hiding this comment

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

Was this rubocop autocorrected? If yes this branch needs master merged into it I think.

Copy link
Member

@Strech Strech left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏼

P.S I've left few non-blocking suggestions

Comment on lines +286 to +299
if block
# When a block is given, the trace will be active until the block finishes.
context.activate!(trace) do
yield
ensure # We have to flush even when an error occurs
# On block completion, force the trace to finish and flush its finished spans.
# Unfinished spans are lost as the {TraceOperation} has ended.
trace.finish!
flush_trace(trace)
end
else
# Otherwise, the trace will be bound to the current thread after this point
context.activate!(trace)
end
Copy link
Member

Choose a reason for hiding this comment

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

This could be streamlined with a guard-clause

Suggested change
if block
# When a block is given, the trace will be active until the block finishes.
context.activate!(trace) do
yield
ensure # We have to flush even when an error occurs
# On block completion, force the trace to finish and flush its finished spans.
# Unfinished spans are lost as the {TraceOperation} has ended.
trace.finish!
flush_trace(trace)
end
else
# Otherwise, the trace will be bound to the current thread after this point
context.activate!(trace)
end
# The trace will be bound to the current thread if no block is given
return context.activate!(trace) unless block
# When a block is given, the trace will be active until the block finishes.
context.activate!(trace) do
yield
ensure # We have to flush even when an error occurs
# On block completion, force the trace to finish and flush its finished spans.
# Unfinished spans are lost as the {TraceOperation} has ended.
trace.finish!
flush_trace(trace)
end

# Don't finish the span, so finished_span_count remains 0
end

# No spans should be flushed
Copy link
Member

Choose a reason for hiding this comment

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

I think comment is unnecessary

Suggested change
# No spans should be flushed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants