Skip to content

fix(tracer): use background context instead of panicking on nil ctx in ContextWithSpan#4313

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
mainfrom
ben.db/ctx-with-span
Mar 30, 2026
Merged

fix(tracer): use background context instead of panicking on nil ctx in ContextWithSpan#4313
gh-worker-dd-mergequeue-cf854d[bot] merged 4 commits into
mainfrom
ben.db/ctx-with-span

Conversation

@genesor

@genesor genesor commented Dec 24, 2025

Copy link
Copy Markdown
Member

What does this PR do?

When tracer.ContextWithSpan receives a nil context, create a context.Background() instead of panicking. This is consistent with how StartSpanFromContext already handles nil contexts.

Motivation

We spent nearly an hour with @igoragoli debugging a future contrib that was not showing this panic properly.
Rather than panicking, gracefully handling nil context is safer and consistent with the existing pattern in StartSpanFromContext.

Changes

  • Replace the explicit panic in ContextWithSpan with a fallback to context.Background()
  • Update doc comment to document the nil context behavior
  • Update test to assert no panic and correct span propagation on nil context

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running ./scripts/lint.sh locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@genesor
genesor requested a review from igoragoli December 24, 2025 17:13
@genesor
genesor requested a review from a team as a code owner December 24, 2025 17:13
@genesor
genesor requested a review from mtoffl01 December 24, 2025 17:13
@pr-commenter

pr-commenter Bot commented Dec 24, 2025

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-03-30 08:40:28

Comparing candidate commit 59b7717 in PR branch ben.db/ctx-with-span with baseline commit aca1a62 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 215 metrics, 9 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

@codecov

codecov Bot commented Dec 24, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.67%. Comparing base (aca1a62) to head (59b7717).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
ddtrace/tracer/context.go 60.86% <100.00%> (ø)

... and 436 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread ddtrace/tracer/context.go Outdated
Comment thread ddtrace/tracer/context.go
@genesor
genesor requested a review from a team as a code owner March 26, 2026 16:15
@genesor
genesor requested a review from kakkoyun March 26, 2026 16:15

@darccio darccio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@darccio darccio changed the title chore(doc): doocument tracer.ContextWithSpan panic chore(doc): document tracer.ContextWithSpan panic Mar 26, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Mar 26, 2026

Copy link
Copy Markdown

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 60.05% (+3.88%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 59b7717 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@genesor
genesor force-pushed the ben.db/ctx-with-span branch from 5c1d161 to 2a044fc Compare March 26, 2026 16:24
@genesor genesor changed the title chore(doc): document tracer.ContextWithSpan panic fix(tracer): use background context instead of panicking on nil ctx in ContextWithSpan Mar 27, 2026

@mtoffl01 mtoffl01 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

f a user has a bug where they accidentally pass nil instead of a context that carried a deadline, cancellation signal, or other values:
Before this change: immediate panic surfaces the bug.
After this change: the code silently proceeds with a bare context.Background(). The span is created and parented, but the context is now detached — no cancellation propagation, no deadline, no user-stored values. This could mask a real bug in the caller's code.

Comment thread ddtrace/tracer/context.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants