Skip to content

feat: inspectable tracer#4512

Open
darccio wants to merge 28 commits intomainfrom
dario.castane/rd/inspectable-tracer-v2
Open

feat: inspectable tracer#4512
darccio wants to merge 28 commits intomainfrom
dario.castane/rd/inspectable-tracer-v2

Conversation

@darccio
Copy link
Copy Markdown
Member

@darccio darccio commented Mar 9, 2026

What does this PR do?

Adds an inspectable tracer that replaces the other four different ways to inspect spans and mocking the tracer.

Migrates a few example tests that we using any of the different existing approaches in the codebase.

Important

  • testracer.Start starts a test tracer but it doesn't register it as global tracer.
  • testtracer.Bootstrap starts a test tracer, a test agent, and registers the former as global tracer.
  • We are not able to run our tests in parallel, although some of the migrated ones could be parallelized already because they don't depend on the public ddtrace/tracer API.

Motivation

  • Replace existing implementations, although now we keep the original APIs for them.
  • Reduce code to maintain.
  • Exercize the tracer in tests, don't mock it.
  • Reduces dependencies although they are not completely removed as they still hang as indirect.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • New code is free of linting errors. You can check this by running make lint locally.
  • New code doesn't break existing tests. You can check this by running make test locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • All generated files are up to date. You can check this by running make generate locally.

Unsure? Have a question? Request a review!

@darccio darccio force-pushed the dario.castane/rd/inspectable-tracer-v2 branch from 8212d72 to 3c74aaf Compare March 9, 2026 15:03
@datadog-datadog-prod-us1
Copy link
Copy Markdown

datadog-datadog-prod-us1 bot commented Mar 9, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 35.83%
Overall Coverage: 60.01% (+4.05%)

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

@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 9, 2026

Benchmarks

Benchmark execution time: 2026-03-25 16:10:32

Comparing candidate commit b2e7bd2 in PR branch dario.castane/rd/inspectable-tracer-v2 with baseline commit d52d8ea 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 ----------------------------------'

darccio added 4 commits March 9, 2026 16:35
…es by replacing FreePort+ListenAndServe with a pre-bound FreeListener+Serve and adding a TestCasePreBootstrap interface so os/lfi.go can configure AppSec env vars before the tracer starts
@darccio
Copy link
Copy Markdown
Member Author

darccio commented Mar 9, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 078b37add2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

o := append([]StartOption{
WithAgentAddr(agent.Addr()),
}, opts...)
tracer, err := newTracer(o...)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Invoke full startup path for inspectable tracer

startInspectableTracer creates the tracer with newTracer(...) but never executes the extra initialization done in tracer.Start (notably AppSec startup and other post-construction runtime hooks). This means options passed through tracertest.Bootstrap, such as tracer.WithAppSecEnabled(true) in the orchestrion harness, are not actually activated, so AppSec-dependent integration scenarios can silently lose coverage or fail inconsistently. The inspectable bootstrap path should run the same startup hooks as Start before returning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This requires a significant refactor. Doing it in a future PR.

@darccio
Copy link
Copy Markdown
Member Author

darccio commented Mar 10, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f6465bb5f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
drained:
tracer.traceWriter.flush()
tracer.traceWriter.(*agentTraceWriter).wg.Wait()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid assuming agentTraceWriter in inspectable flush path

This hard cast panics when the tracer is configured with a non-agent writer (for example log-to-stdout or CI Visibility modes), even though Start accepts arbitrary tracer options/environment. A Flush() call from such tests will crash instead of flushing; guard the cast or handle non-agent writers explicitly.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To do in another PR.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 32.97003% with 246 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.60%. Comparing base (d52d8ea) to head (b2e7bd2).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
ddtrace/x/llmobstest/collector.go 0.00% 82 Missing ⚠️
ddtrace/x/agenttest/agent.go 0.00% 72 Missing ⚠️
ddtrace/x/agenttest/span.go 0.00% 43 Missing ⚠️
ddtrace/tracer/tracertest.go 68.14% 28 Missing and 8 partials ⚠️
ddtrace/tracer/option.go 45.45% 5 Missing and 1 partial ⚠️
internal/llmobs/llmobs.go 88.23% 3 Missing and 1 partial ⚠️
internal/llmobs/transport/transport.go 0.00% 3 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
ddtrace/tracer/tracer.go 88.05% <100.00%> (ø)
internal/llmobs/config/config.go 0.00% <ø> (ø)
internal/llmobs/transport/transport.go 0.00% <0.00%> (ø)
internal/llmobs/llmobs.go 77.51% <88.23%> (ø)
ddtrace/tracer/option.go 83.28% <45.45%> (ø)
ddtrace/tracer/tracertest.go 68.14% <68.14%> (ø)
ddtrace/x/agenttest/span.go 0.00% <0.00%> (ø)
ddtrace/x/agenttest/agent.go 0.00% <0.00%> (ø)
ddtrace/x/llmobstest/collector.go 0.00% <0.00%> (ø)

... and 431 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.

@darccio
Copy link
Copy Markdown
Member Author

darccio commented Mar 11, 2026

@codex review

@darccio darccio marked this pull request as ready for review March 11, 2026 12:59
@darccio darccio requested review from a team as code owners March 11, 2026 12:59
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 63cc74f440

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
drained:
tracer.traceWriter.flush()
tracer.traceWriter.(*agentTraceWriter).wg.Wait()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Guard flush wait against non-agent trace writers

startInspectableTracer accepts arbitrary tracer.StartOptions, but the custom flush handler unconditionally casts tracer.traceWriter to *agentTraceWriter; if callers pass options that select a different writer (for example log/stdout or CI Visibility modes), Flush() will panic at runtime instead of flushing. This makes the new ddtrace/x/tracertest helper fragile in valid option combinations and can crash integration tests unexpectedly.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Already mentioned previously. I'll fix it when needed.

// FlushSync flushes all currently buffered data and blocks until the HTTP send completes.
func (l *LLMObs) FlushSync() {
done := make(chan struct{})
l.flushSyncCh <- done
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid blocking FlushSync when LLMObs is not running

FlushSync always sends on the unbuffered flushSyncCh without checking lifecycle state, so calling it after Stop() (or before Run() starts receiving) blocks forever because no goroutine is left to read that channel. Since this commit wires llmobs.FlushSync() into tracer test flushing, that deadlock path can hang test runs whenever LLMObs is stopped out of band.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good to know. I'll fix it in another PR.

@darccio darccio added the AI Assisted AI/LLM assistance used in this PR (partially or fully) label Mar 11, 2026
Copy link
Copy Markdown
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

I did an initial pass. I will need more time on this. This PR does a lot. But I can see it's neatly separated into commits. So I'll do a commit-by-commit review.

// flushHandler is called by the worker when an explicit flush is triggered
// (i.e. when Flush() is called). It defaults to defaultFlushHandler.
// Tests can override this to provide stronger flush semantics.
flushHandler func(done chan<- struct{})
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.

I will have to revisit https://github.com/DataDog/dd-trace-go/pull/3995/changes
or close after this PR.

// flushHandler is called by the worker when an explicit flush is triggered
// (i.e. when Flush() is called). It defaults to defaultFlushHandler.
// Tests can override this to provide stronger flush semantics.
flushHandler func(done chan<- struct{})
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.

nit: Maybe we should have an interface type Flasher and FlushFunc for future and possibly more complicated flushing mechanisms.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. Should we do it in a followup PR?

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.

sgtm

@darccio
Copy link
Copy Markdown
Member Author

darccio commented Mar 12, 2026

@kakkoyun I agree that the PR does a lot, but the core is in ddtrace/x.

The rest are adapted tests and a fix for Orchestrion integration tests to avoid flakiness that the new free-wait approach unfortunately introduced.

darccio and others added 2 commits March 12, 2026 06:13
Co-authored-by: Kemal Akkoyun <kakkoyun@users.noreply.github.com>
@github-actions github-actions bot added the apm:ecosystem contrib/* related feature requests or bugs label Mar 12, 2026
@darccio darccio requested a review from kakkoyun March 13, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Assisted AI/LLM assistance used in this PR (partially or fully) apm:ecosystem contrib/* related feature requests or bugs team:apm-go

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants