Skip to content

chore(tracing): track spans_dropped telemetry for the trace writer pipeline#18967

Open
mabdinur wants to merge 5 commits into
mainfrom
munir/add-telemetry-to-AgentlessExport
Open

chore(tracing): track spans_dropped telemetry for the trace writer pipeline#18967
mabdinur wants to merge 5 commits into
mainfrom
munir/add-telemetry-to-AgentlessExport

Conversation

@mabdinur

@mabdinur mabdinur commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

AgentlessTraceWriter/AgentlessWriterClient previously reported no instrumentation telemetry, unlike NativeWriter and the LLMObs writer. This makes it hard to answer the operational question that matters most: how many spans are we losing, and where?

This PR adds span-level drop telemetry (TELEMETRY_NAMESPACE.TRACERS) across the writer pipeline. We deliberately track spans, not trace chunks:

  • spans_enqueued_for_serialization — spans accepted into the encoder buffer.
  • spans_dropped (tag reason) — spans that are definitively dropped, recorded once we are sure the span will never reach the intake. Reasons map to the four pipeline stages:
    • tp_drop — dropped by a trace processor in the SpanAggregator (filtering / whole-trace drop).
    • overfull_buffer — the encoder buffer could not fit the trace (BufferFull).
    • serialization_error — the trace could not be encoded/compressed (BufferItemTooLarge, NoEncodableSpansError, encode() failure, gzip failure).
    • api_error — the payload could not be delivered to the intake.
  • trace_api.requests / trace_api.responses (tag status_code) / trace_api.errors (tag type: timeout / network / status_code) — per-HTTP-attempt telemetry, including retries.

Retry-safety (only count once we are SURE)

spans_dropped is never recorded speculatively on an attempt that might still succeed:

  • Network/timeout failures are retried (_send_payload_with_backoff, RETRY_ATTEMPTS). The api_error drop is recorded only after all retries are exhausted, in _flush_single_payload.
  • An HTTP error status (>= 400) is not retried (the backoff stops as soon as a Response is returned), so it is recorded once, after that single definitive attempt.
  • _send_payload only emits per-attempt trace_api.* counters; it never records span drops.

Threading span counts through the pipeline

The buffer/encode/HTTP layers only knew the trace (chunk) count. AgentlessTraceJSONEncoder now tracks a pending_spans count so the writer can attribute a real span count to a dropped payload. Encoders that don't expose an integer span count (e.g. CIVisibility) report None and are skipped for span-level drops — trace_api.* and the existing DogStatsD metrics are unchanged.

Test plan

  • TestWriterTelemetry in tests/tracer/test_writer.py: spans enqueued; spans_dropped for overfull_buffer / serialization_error (buffer, encode, gzip) / api_error (HTTP status and retries-exhausted); no drop on success; no drop when span count is unknown; _send_payload records only trace_api.* (network / timeout / status_code), never span drops.
  • test_processors.py: tp_drop recorded for whole-trace and partial processor drops, and a 0-count no-op when nothing is dropped.
  • scripts/run-tests --venv ed437ab -- -- tests/tracer/test_writer.py tests/tracer/test_processors.py tests/tracer/test_encoders.py → 232 passed, 10 skipped. The only failures are two pre-existing @pytest.mark.subprocess tests that require a local trace agent (Network error: client error (Connect)), confirmed to fail identically on main.
  • scripts/lint fmt / typing clean on all touched files.
  • Release note added (other: category — internal telemetry addition, no user-facing API/behavior change).

…-trace-dotnet

AgentlessTraceWriter previously had no instrumentation telemetry, making it
hard to answer basic questions like how many spans were encoded, sent, or
dropped and why. This adds spans_enqueued_for_serialization, trace_chunks_sent,
trace_chunks_dropped (reason: overfull_buffer/serialization_error/api_error),
and trace_api.requests/responses/errors metrics, matching the metric and tag
naming used for the equivalent HTTP client telemetry in dd-trace-dotnet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@mabdinur mabdinur requested review from a team as code owners July 9, 2026 17:41
@mabdinur mabdinur requested review from juanjux and rachelyangdog July 9, 2026 17:41
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/_trace/processor/__init__.py                                    @DataDog/apm-sdk-capabilities-python
ddtrace/_trace/telemetry.py                                             @DataDog/apm-sdk-capabilities-python
ddtrace/internal/writer/writer.py                                       @DataDog/apm-core-python
tests/tracer/test_processors.py                                         @DataDog/apm-sdk-capabilities-python @DataDog/apm-core-python
tests/tracer/test_writer.py                                             @DataDog/apm-sdk-capabilities-python @DataDog/apm-core-python

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Pipelines  Tests

Fix all issues with BitsAI

⚠️ Warnings

🚦 9 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [arm64, cp315-cp315, v113741589-d2b8243-musllinux_1_2_aarch64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64]   View in Datadog   GitLab

View all 9 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

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

@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: ea84ea7a60

ℹ️ 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/internal/writer/writer.py Outdated
Comment thread tests/tracer/test_writer.py
@pr-commenter

pr-commenter Bot commented Jul 9, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-10 21:11:14

Comparing candidate commit a79c7d8 in PR branch munir/add-telemetry-to-AgentlessExport with baseline commit d5bab3c in branch main.

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

scenario:span-start

  • 🟥 execution_time [+1.315ms; +1.485ms] or [+8.570%; +9.678%]

scenario:telemetryaddmetric-1-count-metric-1-times

  • 🟥 execution_time [+192.741ns; +229.208ns] or [+9.201%; +10.941%]

@gh-worker-ownership-write-b05516 gh-worker-ownership-write-b05516 Bot removed the request for review from a team July 9, 2026 21:11
@mabdinur mabdinur requested a review from Copilot July 9, 2026 21:12
@mabdinur mabdinur added the changelog/no-changelog A changelog entry is not required for this PR. label Jul 9, 2026

Copilot AI 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.

Pull request overview

This PR adds instrumentation telemetry metrics for the shared HTTPWriter send pipeline so that agentless trace export (and any other writers using HTTPWriter) reports span/chunk enqueue, send attempts, drop reasons, and per-HTTP-attempt request/response/error counts—aligned with dd-trace-dotnet’s metric/tag naming.

Changes:

  • Added tracer telemetry recording to HTTPWriter for enqueue, drop reasons, chunk-send attempts, and HTTP request/response/error events.
  • Introduced new telemetry helper functions under ddtrace/_trace/telemetry.py for writer-related metrics.
  • Added unit tests covering the new telemetry behavior and error-path tagging.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
ddtrace/internal/writer/writer.py Records tracer telemetry metrics at key points in the HTTP writer enqueue/encode/send pipeline.
ddtrace/_trace/telemetry.py Adds telemetry helper functions for writer enqueue/sent/dropped and trace API request/response/error counts.
tests/tracer/test_writer.py Adds TestWriterTelemetry to validate telemetry calls for success and failure modes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/tracer/test_writer.py
@mabdinur mabdinur changed the title feat(tracing): add telemetry to AgentlessTraceWriter, aligned with dd-trace-dotnet feat(tracing): track spans_dropped telemetry for the trace writer pipeline Jul 10, 2026
Record trace-processor span drops via the internal telemetry writer that
SpanAggregator already imports, instead of ddtrace._trace.telemetry. The
_trace.processor -> _trace.telemetry edge introduced new import cycles
detected by the detect_circular_imports CI job.

Co-authored-by: Cursor <cursoragent@cursor.com>
@mabdinur mabdinur requested a review from a team as a code owner July 10, 2026 20:05
mabdinur and others added 2 commits July 10, 2026 16:31
Replace the specialized HTTPWriter _telemetry_* methods with a generic
record_trace_writer_metric helper, always pass an int span count, and
simplify the writer telemetry tests to a focused, non-exhaustive set.

Co-authored-by: Cursor <cursoragent@cursor.com>
@mabdinur mabdinur changed the title feat(tracing): track spans_dropped telemetry for the trace writer pipeline chore(tracing): track spans_dropped telemetry for the trace writer pipeline Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants