[Python] DDTestLogsHandler SDK for test visibility log correlation#37367
[Python] DDTestLogsHandler SDK for test visibility log correlation#37367gnufede wants to merge 2 commits into
Conversation
…round job workers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Preview links (active after the
|
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
/review |
There was a problem hiding this comment.
🤖 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 ddf697bb7e97c81b1fcb38142f2a9bd8106ed675 — workflow run
|
|
||
| If you use the **Datadog Agent** instead of agentless mode, set instead `DD_LOGS_INJECTION=true` in the environment. | ||
|
|
||
| #### Out of process logs |
There was a problem hiding this comment.
Style: "Out-of-process" is a compound modifier and should be hyphenated when it modifies "logs".
| #### 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: |
There was a problem hiding this comment.
Suggestion: Drop "common case of" — it adds words without information. The phrasing also reads more directly as imperative guidance.
| 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. |
There was a problem hiding this comment.
Suggestion: "Explicitly" is filler here — handler.close() is already explicit by virtue of being a call. Tightening keeps the instruction direct.
| `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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
please see comments from the gitactions bot
What does this PR do? What is the motivation?
Documents the new
ddtrace.testing.logspublic 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— alogging.Handlerthat auto-detects agentless vs. EVP proxy mode from standard environment variablesThreadLocalCorrelationFilter— for the common thread-per-worker caseContextVar-backedCorrelationFiltersubclass — for asyncio workersMerge instructions
Merge readiness:
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.