Skip to content

feat(telemetry): send pydantic-ai spans through the exporter seam - #4538

Merged
dan-petty merged 5 commits into
mainfrom
feat/4515-pydantic-ai-spans-exporter-seam
Nov 11, 2026
Merged

dan-petty merged 5 commits into
mainfrom
feat/4515-pydantic-ai-spans-exporter-seam

Conversation

@dan-petty

@dan-petty dan-petty commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

pydantic-ai, the MCP client and the LLM gateway client make their spans through the OpenTelemetry API, and no process set an SDK tracer provider, so Tempo held no agent, model, tool or MCP span.

A process that builds a pydantic-ai model with telemetry export on now sets one SDK provider. It sends each batch as OTLP protobuf through the exporter seam of #1319, so the egress check and the dry-run rule apply. Prompt and tool text follow telemetry.capture_llm_content. Agent spans nest under the row span.

Trace context goes to the LLM gateway only; MCP requests carry none. The --logfire bridge sends nothing to Logfire's cloud and does not start while an OTEL_EXPORTER_OTLP_*ENDPOINT variable is set. Its token setting is deleted.

logfire_agent_turn, AgentTurnHandle and devops telemetry test --logfire are deleted: they wrote ungated prompt text.

Closes #4515

Change: openspec/changes/archive/2026-11-11-4515-send-pydantic-ai-spans-through-the/

@dan-petty dan-petty added type/feature Type feature: a new function scope/telemetry OpenTelemetry tracing spans, Prometheus metrics, and Jaeger labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Checks and the live check

Local runs

  • uv run devops ci test tests/telemetry/test_library_spans.py: 5 tests. Before the change (base c3f62a9d), the nesting test and the gateway test failed with KeyError: 'invoke_agent' and KeyError: 'POST': no library span reached the OTLP fake.
  • Mutation checks: with the dry-run check removed from SeamSpanExporter, test_a_dry_run_exports_no_agent_span fails; with include_content=True, test_agent_spans_hold_no_prompt_or_tool_text_by_default fails.
  • tests/ai, tests/roadmap/stage, tests/roadmap/implement, tests/server, tests/telemetry and tests/http: all pass.
  • Full uv run devops ci on the branch: 22,709 tests passed and 1 failed, test_binding_inside_the_worker_would_capture_nothing. Cause: test_pydantic_agent_keeps_private_host_out_of_telemetry_when_capture_off runs a real logfire.configure, which patches ThreadPoolExecutor.submit for the whole process; with the own spans in the OpenTelemetry context, the patch carried a span into a later worker. Fix in commit "restore the executors after a real Logfire configure"; the pair passes in one process, and tests/telemetry passes in one process and on 4 workers. The other rows passed (test tree and swallowed-failures ratchet after their fixes).
  • Startup import gate: pass. The SDK loads only in a process that builds a pydantic-ai model, and pydantic_ai already imports the SDK, so that process gets no new import.

Live check after release (a person, after the new image runs in roadmap-service and after one stage or refine round)

In Grafana Explore, Tempo data source, run:

{ resource.service.name = "roadmap-service" && name =~ "invoke_agent.*" }

Expected: at least one trace. Open it: invoke_agent <agent> sits under service.job, with chat <model> and execute_tool <tool> spans under it.

To check the nesting and the cache tokens in one query:

{ resource.service.name = "roadmap-service" && name = "service.job" } >> { name =~ "chat .*" && span.gen_ai.usage.cache_read.input_tokens > 0 }

For the runner Job, use resource.service.name = "devops-cli-job". The gateway HTTP spans are POST spans under chat, with scope opentelemetry.instrumentation.httpx.

Also watch tempo_distributor_attributes_truncated_total after the release: pydantic-ai writes gen_ai.tool.definitions and the message skeleton on each chat span, and Tempo cuts an attribute at 2,048 bytes by default.

No ConfigMap, Secret or manifest change is necessary: the spans use telemetry.enabled and telemetry.endpoint, which the pods already have.

Notes

  • The own request_headers (http/entry.py) still puts a traceparent (no baggage) on the requests that devops-cli builds to its own cluster services (Vault, crawl4ai, SearXNG, service probes). This change does not touch that path.
  • the --logfire option of the review commands still sends to Logfire's cloud when a token is configured; none is configured. With the bridge on, the agent spans also go to the collector.

pydantic-ai, the MCP client and the gateway client make their spans
through the OpenTelemetry API, and no process set an SDK tracer
provider, so Tempo held no agent, model, tool or MCP span.

library_spans sets one SDK provider for each process when the process
builds its first pydantic-ai model. Its exporter encodes each batch
with the official OTLP encoder and gives the protobuf body to the
exporter of the process's tracer, so the spans take the egress-checked
client and are not sent in a dry run. The own spans keep their ids in
the OpenTelemetry API context, so library spans nest under them. Only
the gateway client gets the httpx instrumentor.

logfire_agent_turn, AgentTurnHandle and `devops telemetry test
--logfire` are deleted: the agent spans replace them.

Refs #4515
The bridge test ran the real logfire.configure, which patches the
executors of the process, so a later context test saw a span. The test
now puts the processors of the bridge on its own provider. The reset
shuts the library provider down, the telemetry document names the
option of the review commands, and the swallowed-failures ceilings of
tracer.py and logfire.py go down.

Refs #4515
logfire.configure patches submit of ThreadPoolExecutor and
ProcessPoolExecutor for the whole process. Now that the own spans live
in the OpenTelemetry context, the patch carried a span into the worker
of a later test in the same process. The test puts the originals back
at teardown.

Refs #4515
@dan-petty
dan-petty force-pushed the feat/4515-pydantic-ai-spans-exporter-seam branch from fb5275a to 64540f6 Compare November 11, 2026 07:52
@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 08:49
@dan-petty
dan-petty marked this pull request as draft November 11, 2026 09:40
dan-petty added a commit that referenced this pull request Nov 11, 2026
The review of #4538 found two channels that the owner decision of
2026-11-11 does not allow.

The Logfire bridge set send_to_logfire=True when a token was set, and
Logfire adds its stock OTLP exporter when an OTEL_EXPORTER_OTLP_*ENDPOINT
variable is set. The bridge now runs Logfire as a local SDK: it sets
send_to_logfire=False, passes no token and does not start while such a
variable is set. telemetry.logfire_token, telemetry.logfire_send_to_logfire,
get_logfire_token and the logfire_token keyring entry are deleted.

An own span is now in the OpenTelemetry API context, so the MCP SDK and
fastmcp put traceparent and baggage in the _meta of each MCP request.
The global propagator now injects nothing and still extracts, and the
gateway client injects with its own propagator in the request hook of
the httpx instrumentor.

Refs #4515
@dan-petty
dan-petty force-pushed the feat/4515-pydantic-ai-spans-exporter-seam branch from 64540f6 to 7b64c6d Compare November 11, 2026 09:40
@dan-petty

Copy link
Copy Markdown
Owner Author

Review findings: what I did

Commit 943d9f76 (fix(telemetry): send nothing to Logfire's cloud and no trace to MCP) fixes both findings. The OpenSpec archive (7b64c6dd) is the last commit again.

1. major: the --logfire bridge sent spans to Logfire's cloud (src/devops_cli/telemetry/logfire.py) - CONFIRMED, FIXED

  • Check: on 64540f6a, LogfireBridge.configure set send_to_logfire=True when a token was set. logfire/_internal/config.py:1412-1421 (5.2.0) adds BatchSpanProcessor(OTLPSpanExporter()) when OTEL_EXPORTER_OTLP_ENDPOINT or a signal endpoint variable is set. Logfire 5.2.0 has no option that stops these stock exporters (configure and AdvancedOptions have none).
  • Fix: the bridge always sets send_to_logfire=False and passes no token. It refuses to start (LogfireConfigurationError, which names the variable) while OTEL_EXPORTER_OTLP_ENDPOINT, ..._TRACES_ENDPOINT, ..._METRICS_ENDPOINT or ..._LOGS_ENDPOINT is set, so Logfire never adds the stock exporter beside the seam. The token has no use now, so telemetry.logfire_token, telemetry.logfire_send_to_logfire (no code read it), get_logfire_token, the logfire_token keyring entry and the token rows of devops telemetry status and devops telemetry logfire are deleted. A config file that still holds these keys loads: the section drops a key it does not declare.
  • The --logfire help text no longer names "agent turn tracing", which this PR deleted. TELEMETRY.md (generator.py), the spec and the design now say what the bridge does.
  • Tests: test_the_bridge_sends_nothing_to_logfire_cloud_also_with_a_token, test_the_bridge_refuses_to_start_when_logfire_would_add_its_own_exporter (4 cases).

2. major: MCP requests carried traceparent (src/devops_cli/telemetry/tracer.py) - CONFIRMED, FIXED

  • Check: a new test runs an agent with a tool of an in-process roadmap MCP server (RoadmapMcpToolset) in a service.job span with baggage. On 64540f6a, the server got traceparent and baggage in the _meta of the tool request. The MCP SDK injects through the global propagator with no option to stop it (mcp/shared/_otel.py:39); fastmcp also injects (fastmcp/telemetry.py:152).
  • Owner decision of 2026-11-11: trace context and baggage go to the LiteLLM gateway only. So I stopped the channel and did not document it as kept.
  • Fix: telemetry/propagation.py confine_api_propagation replaces the global propagator, once for each process, with one that injects nothing and extracts with the propagator it replaces. It runs before an own span enters the API context (_enter_span), before the library provider is made and before the bridge configures Logfire (Logfire wraps the global propagator). The gateway client injects the trace context and baggage of its HTTP client span with its own W3C propagator, in the request hook of the httpx instrumentor (the hook runs in the span, before the instrumentor copies the headers).
  • Extraction stays: FastAPI's native server span attaches the context that it extracts. My first version used CompositePropagator([]), whose extract returns None; test_both_metrics_routes_parse_via_prometheus_client then failed with AttributeError. test_the_global_propagator_reads_incoming_trace_context_and_injects_none keeps this fixed.
  • Effect: a roadmap MCP server span starts its own trace. If you want MCP server spans in the agent trace, that needs a new owner decision.
  • Tests: test_an_mcp_request_in_a_row_span_carries_no_trace_context, test_the_global_propagator_reads_incoming_trace_context_and_injects_none. The gateway test still sees the traceparent of the POST span.

Fail-to-pass

  • uv run devops ci fail-to-pass --base 7165a0ea (this PR before the fixes): 12 failed on base, among them test_an_mcp_request_in_a_row_span_carries_no_trace_context, test_the_global_propagator_reads_incoming_trace_context_and_injects_none, test_the_bridge_sends_nothing_to_logfire_cloud_also_with_a_token and the 4 refusal cases. All pass on the head.
  • uv run devops ci fail-to-pass --base origin/main: 10 failed on base (test_library_spans does not collect on main). All pass on the head.
  • Local: 2014 passed in tests/telemetry tests/roadmap/test_roadmap_mcp.py tests/ai/mcp tests/commands/test_telemetry_monitoring_dry_run.py tests/commands/test_cli_startup_imports.py tests/docs tests/invariants tests/server; 314 passed in the config and key audit tests; devops docs generate --check and devops openspec check pass.

Live steps

None are necessary. The live ConfigMap holds no telemetry.logfire_token or telemetry.logfire_send_to_logfire key (k8s/ has none), and a file that still holds them loads. On a workstation, an old logfire_token keyring entry has no reader now; you can delete it with your keyring tool.

The review of #4538 found two channels that the owner decision of
2026-11-11 does not allow.

The Logfire bridge set send_to_logfire=True when a token was set, and
Logfire adds its stock OTLP exporter when an OTEL_EXPORTER_OTLP_*ENDPOINT
variable is set. The bridge now runs Logfire as a local SDK: it sets
send_to_logfire=False, passes no token and does not start while such a
variable is set. telemetry.logfire_token, telemetry.logfire_send_to_logfire,
get_logfire_token and the logfire_token keyring entry are deleted.

An own span is now in the OpenTelemetry API context, so the MCP SDK and
fastmcp put traceparent and baggage in the _meta of each MCP request.
The global propagator now injects nothing and still extracts, and the
gateway client injects with its own propagator in the request hook of
the httpx instrumentor.

Refs #4515
@dan-petty
dan-petty force-pushed the feat/4515-pydantic-ai-spans-exporter-seam branch from 7b64c6d to 64abfd2 Compare November 11, 2026 10:00
@dan-petty

Copy link
Copy Markdown
Owner Author

Update after the first check run

  • Static Analysis failed on 7b64c6dd: the test-tree gate check_every_piece_of_process_wide_state_in_src_is_reset_by_the_floor found that confine_api_propagation kept process-wide state (a functools.cache) with no reset. Now the function keeps the replaced propagator in module state under a lock, and reset_api_propagation puts it back. tests/conftest.py calls the reset after each test. uv run devops ci --only test-tree --no-cache passes (12 cases, 0 failed).
  • PR body: the body had 1553 characters before the trailers (limit 1000). It now has 908.
  • The fix commit is now 6ddbc097 and the OpenSpec archive 64abfd20 is the last commit. Local: 2328 passed in the telemetry, MCP, server, docs, invariants and config tests.

@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 10:20
@dan-petty
dan-petty merged commit fcf2a7a into main Nov 11, 2026
38 checks passed
@dan-petty
dan-petty deleted the feat/4515-pydantic-ai-spans-exporter-seam branch November 11, 2026 11:54
dan-petty pushed a commit that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.112.0](v0.111.0...v0.112.0)
(2026-11-11)


### Features

* **k8s:** export gateway spans without key metadata
([#4520](#4520))
([7234d71](7234d71))
* **k8s:** set the homelab domain in container env values
([#4608](#4608))
([a3bcf7c](a3bcf7c))
* **telemetry:** send pydantic-ai spans through the exporter seam
([#4538](#4538))
([fcf2a7a](fcf2a7a))


### Bug Fixes

* **ai:** price cached prompt tokens of chat calls
([#4584](#4584))
([f84bbe6](f84bbe6))
* **devcontainer:** stop uv noise lines in agent shells
([#4592](#4592))
([9ba375c](9ba375c))
* **k8s:** delete gateway management bodies from spans
([#4551](#4551))
([b835d3c](b835d3c))
* **k8s:** set the kube context and namespace on devops pods
([#4514](#4514))
([9087b39](9087b39))
* **roadmap:** a refused spec after the archive is a plan defect
([#4549](#4549))
([1db29f7](1db29f7))
* **roadmap:** count and log the reason of a left-out stage item
([#4541](#4541))
([5600179](5600179))
* **roadmap:** default the step ceiling to twice the budget
([#4556](#4556))
([1dfb6c8](1dfb6c8))
* **roadmap:** explain --trace shows the argument digest
([#4526](#4526))
([c92195f](c92195f))
* **roadmap:** explain totals count dropped model events
([#4543](#4543))
([fe0e979](fe0e979))
* **roadmap:** give each eval verify run its own report file
([#4534](#4534))
([ad27f2d](ad27f2d))
* **roadmap:** one event loop and a time limit for decide_all
([#4585](#4585))
([ec51e01](ec51e01))
* **roadmap:** scoped updates send requests with no row scope
([#4531](#4531))
([5f75d25](5f75d25))
* **roadmap:** the caller gate reads an added parameter span
([#4548](#4548))
([1ba07c1](1ba07c1))
* **roadmap:** the implement shell waits for its command
([#4536](#4536))
([571e044](571e044))
* **roadmap:** the module-test and caller gates read imports and calls
([#4490](#4490))
([8a22e82](8a22e82))
* **service:** put trace ids on Service JSON log lines
([#4511](#4511))
([0d7a0d2](0d7a0d2))


### Performance Improvements

* **ai:** keep intake embeddings between CLI runs
([#4546](#4546))
([812871e](812871e))
* **roadmap:** intake reads only the issues that changed
([#4552](#4552))
([7dfd155](7dfd155))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/telemetry OpenTelemetry tracing spans, Prometheus metrics, and Jaeger type/feature Type feature: a new function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(telemetry): send pydantic-ai spans through the exporter seam

1 participant