Skip to content

fix(ci-visibility): harden mocktracer, HTTP bodies, and EFD skips#4745

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 8 commits into
mainfrom
feat/ci-visibility-mocktracer-preservation
May 12, 2026
Merged

fix(ci-visibility): harden mocktracer, HTTP bodies, and EFD skips#4745
gh-worker-dd-mergequeue-cf854d[bot] merged 8 commits into
mainfrom
feat/ci-visibility-mocktracer-preservation

Conversation

@tonyredondo

@tonyredondo tonyredondo commented May 11, 2026

Copy link
Copy Markdown
Member

What does this PR do?

This PR fixes three CI Visibility issues found while validating Test Optimization in downstream repositories:

  1. It preserves mocktracer when CI Visibility starts after a package has already called mocktracer.Start().
  2. It drains and closes successful CI Visibility writer HTTP response bodies so test-cycle upload connections can shut down cleanly before goleak runs.
  3. It stops Early Flake Detection from retrying tests that cleanly skip on the first execution.

For the mocktracer startup path, tracer.Start() now keeps the existing civisibilitymocktracer installed as the process global tracer when CI Visibility starts later. Instead of replacing that mock-compatible wrapper, it injects the real CI Visibility tracer into it. Normal application spans keep going to the mock tracer, while CI Visibility test/session/module/suite spans go to the real tracer.

The PR also adds a finished-chunk submit hook so CI Visibility chunks are still submitted through the real tracer even though the process global tracer remains the mock wrapper. The mock wrapper now owns its real tracer delegate explicitly, protects delegate replacement and span routing with locks, keeps real-span routing state instance-local, and preserves shutdown behavior so user mocktracer.Stop() calls do not kill CI Visibility before package exit.

For the HTTP cleanup path, the CI Visibility trace writer now owns every non-nil body returned by ddTransport.send. It drains and closes those bodies on success and on the defensive (body, err) path, preserving retry behavior while allowing the HTTP transport to release keep-alive connections.

For the EFD skip path, clean skips now stop at the common EFD retry decision. That keeps pass/fail EFD retries unchanged, but avoids burning the retry budget on executions that only produce skip signal. The final-status prediction mirrors the same decision so the single skipped execution receives test.final_status=skip immediately and does not get retry tags.

No new environment variables are introduced, and this does not change /civisibility or /internal/civisibility.

Motivation

The downstream mocktracer failure happens when test packages start mocktracer in TestMain, and the test optimizer later starts CI Visibility through RunM.

Before this change, CI Visibility startup replaced the global civisibilitymocktracer with either the real *tracer.tracer or the CI Visibility noop wrapper. Code already using the v1 mocktracer path still expected the global tracer to be mock-compatible, so finishing adapted mock spans could panic with an interface conversion error.

Preserving the wrapper fixes that ownership problem without losing CI Visibility data: user test spans remain available through FinishedSpans(), and CI Visibility spans still reach the real tracer and submit to the test-cycle intake.

A later downstream run also exposed a goleak failure with net/http.(*persistConn).readLoop and net/http.(*persistConn).writeLoop after tests had passed. The CI Visibility writer was ignoring successful response bodies returned by the transport, leaving the client connection in use. Draining and closing those bodies makes shutdown deterministic for the HTTP transport.

Downstream EFD validation also showed output like skipped after 10 retries by Datadog's early flake detection. Retrying a clean skipped test does not add useful flakiness signal, so this PR stops scheduling EFD retries when the first execution is skipped && !failed.

Testing

  • go test ./ddtrace/mocktracer -count=1
  • go test ./ddtrace/mocktracer -race -count=1
  • go test ./ddtrace/mocktracer -race -count=3
  • go test ./ddtrace/mocktracer -run 'TestCIVisibilityMockTracer' -count=10
  • go test ./ddtrace/tracer -run 'TestSetGlobalTracerPreservingCIVisibilityMockTracer|TestGlobalTracer' -count=1
  • go test ./ddtrace/tracer -run 'TestSetGlobalTracerPreservingCIVisibilityMockTracer|TestGlobalTracer' -race -count=10
  • go test ./ddtrace/tracer -run 'TestCIVisibilityImplementsTraceWriter|TestCiVisibilityTraceWriterFlushRetries|TestCiVisibilityTraceWriterClosesHTTPResponseBody' -count=1
  • go test ./ddtrace/tracer -run 'TestCIVisibilityImplementsTraceWriter|TestCiVisibilityTraceWriterFlushRetries|TestCiVisibilityTraceWriterClosesHTTPResponseBody' -race -count=1
  • go test ./ddtrace/tracer -run 'TestCIVisibility|TestCiVisibility' -count=1
  • go test ./internal/civisibility/integrations/gotesting -count=1
  • go test ./internal/civisibility/integrations/gotesting/subtests -count=1
  • go test ./internal/civisibility/integrations/gotesting/failnow -count=1
  • git diff --check

Local note: full go test ./ddtrace/tracer -count=1 is blocked in this checkout by environment-sensitive DogStatsD autodetection expectations. The local agent reports DogStatsD port 8135, while existing tracer tests expect 8125 in startup-log/default-config assertions. The focused tracer tests for these changes pass.

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 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.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild. Make sure all nested modules are up to date by running make fix-modules locally.

@tonyredondo
tonyredondo force-pushed the feat/ci-visibility-mocktracer-preservation branch from 0d1dcee to 91d1828 Compare May 11, 2026 09:44
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented May 11, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 86.78%
Overall Coverage: 61.82% (+4.04%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 73a66d3 | Docs | Datadog PR Page | Give us feedback!

@codecov

codecov Bot commented May 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.78626% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.51%. Comparing base (3bcc46f) to head (73a66d3).

Files with missing lines Patch % Lines
ddtrace/mocktracer/civisibilitymocktracer.go 91.20% 5 Missing and 3 partials ⚠️
ddtrace/tracer/civisibility_mocktracer.go 69.23% 3 Missing and 1 partial ⚠️
ddtrace/tracer/spancontext.go 75.00% 2 Missing ⚠️
ddtrace/tracer/tracer.go 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
ddtrace/tracer/civisibility_writer.go 80.35% <100.00%> (ø)
...ivisibility/integrations/gotesting/final_status.go 81.96% <100.00%> (ø)
...sibility/integrations/gotesting/instrumentation.go 75.98% <100.00%> (ø)
ddtrace/tracer/spancontext.go 93.69% <75.00%> (ø)
ddtrace/tracer/tracer.go 88.79% <60.00%> (ø)
ddtrace/tracer/civisibility_mocktracer.go 69.23% <69.23%> (ø)
ddtrace/mocktracer/civisibilitymocktracer.go 89.62% <91.20%> (ø)

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

@tonyredondo
tonyredondo force-pushed the feat/ci-visibility-mocktracer-preservation branch 2 times, most recently from 171ce2d to e0158d4 Compare May 11, 2026 09:59
@tonyredondo
tonyredondo force-pushed the feat/ci-visibility-mocktracer-preservation branch from e0158d4 to deb1b21 Compare May 11, 2026 10:08
@pr-commenter

pr-commenter Bot commented May 11, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-05-12 14:06:20

Comparing candidate commit 73a66d3 in PR branch feat/ci-visibility-mocktracer-preservation with baseline commit 3bcc46f in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 273 metrics, 2 unstable metrics, 1 known flaky benchmarks.

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 ----------------------------------'

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

scenario:BenchmarkOTLPTraceWriterFlush

  • 🟩 execution_time [-4.622µs; -2.167µs] or [-7.232%; -3.390%]

@tonyredondo tonyredondo changed the title fix(ci-visibility): preserve mocktracer during startup fix(ci-visibility): preserve mocktracer and clean up HTTP bodies May 11, 2026
@tonyredondo
tonyredondo marked this pull request as ready for review May 11, 2026 15:08
@tonyredondo
tonyredondo requested review from a team as code owners May 11, 2026 15:08

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

Copy link
Copy Markdown

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: f98d3b0541

ℹ️ 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".

Comment thread ddtrace/mocktracer/civisibilitymocktracer.go Outdated

@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

@genesor genesor 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 !

@tonyredondo
tonyredondo requested a review from a team as a code owner May 12, 2026 10:58
@tonyredondo tonyredondo changed the title fix(ci-visibility): preserve mocktracer and clean up HTTP bodies fix(ci-visibility): harden mocktracer, HTTP bodies, and EFD skips May 12, 2026
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.

4 participants