Skip to content

[Python] DDTestLogsHandler SDK for test visibility log correlation#37367

Open
gnufede wants to merge 2 commits into
masterfrom
gnufede/logs-api-docs
Open

[Python] DDTestLogsHandler SDK for test visibility log correlation#37367
gnufede wants to merge 2 commits into
masterfrom
gnufede/logs-api-docs

Conversation

@gnufede

@gnufede gnufede commented Jun 9, 2026

Copy link
Copy Markdown
Member

What does this PR do? What is the motivation?

Documents the new ddtrace.testing.logs public API added in dd-trace-py#18078, which ships log records from background job workers to the Datadog logs intake correlated with Test Visibility traces.

The new section ("Out of process logs") is added to the existing Correlate Logs and Tests page, under the Python / Agentless tab. It covers:

  • DDTestLogsHandler — a logging.Handler that auto-detects agentless vs. EVP proxy mode from standard environment variables
  • ThreadLocalCorrelationFilter — for the common thread-per-worker case
  • A ContextVar-backed CorrelationFilter subclass — for asyncio workers

Merge instructions

Merge readiness:

  • Ready for merge

AI assistance

Used Claude Code to draft the documentation and code examples based on the source implementation in ddtrace/testing/logs.py.

Additional notes

The version constraint (ddtrace >= 4.11.0) is an estimate — the feature landed after v4.10.1. Update if the actual release version differs.

…round job workers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Pipelines

Fix all issues with BitsAI

⚠️ Warnings

🚦 1 Pipeline job failed

Code-Freeze | check-code-freeze-status   View in Datadog   GitHub Actions

Useful? React with 👍 / 👎

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

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Preview links (active after the build_preview check completes)

Modified Files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gnufede gnufede marked this pull request as ready for review June 9, 2026 10:26
@gnufede gnufede requested review from a team as code owners June 9, 2026 10:26
@rtrieu

rtrieu commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

/review

@github-actions github-actions Bot 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.

🤖 Automated review by Claude. AI-generated; verify before acting.

One style nit on the new section heading. Content is clear and code examples look reasonable for technical readers.

Reviewed ddf697bb7e97c81b1fcb38142f2a9bd8106ed675workflow run


If you use the **Datadog Agent** instead of agentless mode, set instead `DD_LOGS_INJECTION=true` in the environment.

#### Out of process logs

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.

Style: "Out-of-process" is a compound modifier and should be hyphenated when it modifies "logs".

Suggested change
#### Out of process logs
#### Out-of-process logs


##### Thread-per-worker

For the common case of one thread per test worker, use `ThreadLocalCorrelationFilter` to associate each thread's log records with the correct test trace:

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.

Suggestion: Drop "common case of" — it adds words without information. The phrasing also reads more directly as imperative guidance.

Suggested change
For the common case of one thread per test worker, use `ThreadLocalCorrelationFilter` to associate each thread's log records with the correct test trace:
For one thread per test worker, use `ThreadLocalCorrelationFilter` to associate each thread's log records with the correct test trace:

run_test(job.item)
```

`DDTestLogsHandler` flushes buffered records automatically when used as a context manager. Call `handler.close()` explicitly if you do not use the context manager form.

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.

Suggestion: "Explicitly" is filler here — handler.close() is already explicit by virtue of being a call. Tightening keeps the instruction direct.

Suggested change
`DDTestLogsHandler` flushes buffered records automatically when used as a context manager. Call `handler.close()` explicitly if you do not use the context manager form.
`DDTestLogsHandler` flushes buffered records automatically when used as a context manager. Call `handler.close()` if you do not use the context manager form.

handler.addFilter(correlation)
logging.getLogger().addHandler(handler)

while True:

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.

Suggestion: queue is referenced but never imported or defined in the snippet, which can trip up readers copying the example. Consider adding a brief comment to signal this is illustrative (e.g., # queue and run_test are provided by your worker framework) or initialize them so the snippet is self-contained.


##### Asyncio workers

For asyncio-based workers, subclass `CorrelationFilter` and use a `contextvars.ContextVar` for storage so each `asyncio.Task` sees its own correlation IDs:

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.

Suggestion: A one-line note on why CorrelationFilter needs subclassing here (the default ThreadLocalCorrelationFilter stores context in thread-locals, which don't propagate across asyncio.Task boundaries) would help readers understand the choice rather than just follow it.

@rtrieu rtrieu 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.

please see comments from the gitactions bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants