Skip to content

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

Closed

Description

@dan-petty

Problem or Motivation

devops-cli exports spans through its own tracer (src/devops_cli/telemetry/tracer.py). No process sets an OpenTelemetry SDK TracerProvider. The OpenTelemetry API spans of pydantic-ai (stage, refine, review, implement and the harness Coder), of the MCP client (fastmcp and the MCP SDK) and of httpx go to the no-op provider of the API.

Evidence, 2026-11-11:

  • Tempo has one instrumentation scope, devops-cli.telemetry. In 7 days it has 0 invoke_agent, chat, execute_tool or MCP spans.
  • Only devops review --logfire calls logfire.configure (commands/review.py:194-208). That call sets no exporter to the cluster collector.
  • The own tracer keeps its trace and span ids in its own context variables (tracer.py:815-816). A library span cannot find the own span as its parent, so it cannot nest under a row span such as service.job.
  • logfire_agent_turn and AgentTurnHandle (telemetry/logfire.py) write up to 500 characters of prompt and reply text on a span, and telemetry.capture_llm_content does not gate them. Only devops telemetry test --logfire calls them.

Do not use OTEL_EXPORTER_OTLP_ENDPOINT for this. The stock OTLP exporters bypass the egress-checked client (SECURITY.md) and the dry-run rule (#412), and every child process inherits the variable.

Proposed Solution & Architectural Design

  • One SDK TracerProvider for the library spans, with a BatchSpanProcessor. Its SpanExporter encodes the spans with the official OTLP encoder (opentelemetry-exporter-otlp-proto-common, encode_spans) and gives the application/x-protobuf body to the OtlpExporter seam of refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319 (telemetry/exporter.py). A dry run and a disabled tracer send nothing.
  • One context: the own spans put their span context in the OpenTelemetry API context, and a new own span reads its parent from that context. So model and tool spans nest under the row span, and an own span in a tool call nests under the tool span. The tracer context variables are deleted.
  • pydantic-ai: Agent.instrument_all(InstrumentationSettings(...)) once for each process, when the process builds its first pydantic-ai model. include_content follows telemetry.capture_llm_content (off by default), include_binary_content=False, include_model_request_parameters=False, no metrics, the default format version.
  • MCP: the native spans of fastmcp and the MCP SDK, through the global provider. No logfire.instrument_mcp, so each call makes one span.
  • httpx: the OpenTelemetry httpx instrumentor on the gateway client only. Trace context and baggage go to the LiteLLM gateway, not to GitHub or other hosts.
  • No Logfire cloud: no send_to_logfire, no token.
  • Delete logfire_agent_turn, AgentTurnHandle, their render helpers and devops telemetry test --logfire. The library spans replace them. No shim.

Target Subsystem

Telemetry: src/devops_cli/telemetry/ (tracer, a new library span module, logfire bridge), the pydantic-ai provider factory (ai/providers/__init__.py), devops telemetry test, and the OTLP fake (tests/support/fakes/otlp.py).

Existing Tool Considered

The OpenTelemetry SDK (TracerProvider, BatchSpanProcessor, SpanExporter), the official encoder opentelemetry-exporter-otlp-proto-common, opentelemetry-instrumentation-httpx (HTTPX2ClientInstrumentor.instrument_client), the pydantic-ai instrumentation (Agent.instrument_all, InstrumentationSettings, documented as "Using OpenTelemetry without Logfire") and the native MCP spans. All are installed at the locked versions. The only code to write is one thin SpanExporter over the seam.

Acceptance criteria

  • A test with the OTLP fake runs a pydantic-ai agent with one tool call inside a row span. The fake gets invoke_agent, chat and execute_tool spans in the trace of the row span, nested under it, with gen_ai.usage.input_tokens, gen_ai.usage.output_tokens and gen_ai.usage.cache_read.input_tokens, and with no prompt text.
  • In a dry run, the same agent run sends 0 export requests.
  • A request on the gateway client has a traceparent header. A request on another client of the egress factory gets no traceparent from the instrumentation.
  • logfire_agent_turn and AgentTurnHandle do not exist.
  • Tests run offline; each new test runs in under 1 s. uv run devops ci passes.

Related

Sub-issue of #2536 (the spans and propagation part). Builds on #1319. #412 (dry run). #606 (LLM content off by default).

Proposed design

Problem statement

devops-cli exports its own spans, but no process installs an OpenTelemetry SDK TracerProvider. The OpenTelemetry API spans of pydantic-ai, the MCP client and httpx go to the no-op provider. They cannot nest under the own row spans. This change installs one global SDK provider with a batched OTLP exporter over the egress-checked seam, moves the own tracer to the OpenTelemetry context, instruments pydantic-ai and the gateway httpx client, and removes the Logfire agent-turn path. It does not use OTEL_EXPORTER_OTLP_ENDPOINT.

Acceptance criteria

Requirement: Global SDK provider

The library telemetry SHALL use one global SDK TracerProvider with a BatchSpanProcessor and a SpanExporter over the process OtlpExporter.

Scenario: Provider construction

  • WHEN A test builds the library telemetry with export enabled.
  • THEN The global provider holds one BatchSpanProcessor that uses the process SpanExporter.

Requirement: Library spans nest under the row span

The telemetry SHALL export pydantic-ai invoke_agent, chat and execute_tool spans in the trace of the active row span.

Scenario: Agent run inside a row span

  • WHEN A test runs a pydantic-ai agent with one tool call inside a row span and reads the OTLP fake.
  • THEN The fake holds invoke_agent, chat and execute_tool spans with the trace id of the row span and a parent chain to it.

Requirement: Token usage

The telemetry SHALL export gen_ai.usage.input_tokens, gen_ai.usage.output_tokens and gen_ai.usage.cache_read.input_tokens on the chat span.

Scenario: Model reports token usage

  • WHEN A test runs the instrumented agent with a model response that reports input, output and cache-read token counts.
  • THEN The chat span in the OTLP fake holds the three token attributes with the reported values.

Requirement: Content capture off

The telemetry MUST NOT export prompt or reply text on pydantic-ai spans when telemetry.capture_llm_content is false.

Scenario: Content capture is off

  • WHEN A test runs the instrumented agent with telemetry.capture_llm_content false and reads the exported spans.
  • THEN No exported span holds the prompt text or the reply text.

Requirement: Content capture on

The telemetry SHALL set pydantic-ai include_content from telemetry.capture_llm_content.

Scenario: Content capture is on

  • WHEN A test runs the instrumented agent with telemetry.capture_llm_content true.
  • THEN The exported chat span holds the prompt and reply text.

Requirement: Dry run

The telemetry MUST send zero export requests for library spans during a dry run.

Scenario: Dry run with an agent

  • WHEN A test starts a dry run, runs the instrumented agent with one tool call, and reads the OTLP fake.
  • THEN The fake holds zero export requests.

Requirement: One OpenTelemetry context

The tracer SHALL read and write the active span context through the OpenTelemetry API context and MUST NOT keep its own trace or span id variables.

Scenario: Own span inside a library span

  • WHEN A test opens a library span, starts an own span inside it, and reads both exported spans.
  • THEN The own span has the library span as its parent, and _current_trace_id_ctx and _current_span_id_ctx do not exist.

Requirement: Gateway trace context

The telemetry SHALL add a traceparent header to each gateway client request and MUST NOT add one to another egress client request.

Scenario: Gateway and non-gateway requests

  • WHEN A test sends one request through the instrumented gateway client and one through another client of the egress factory.
  • THEN The gateway request has a traceparent header and the other request has none from the instrumentation.

Requirement: Native MCP spans

The MCP client SHALL export its native spans through the global SDK provider and MUST NOT call logfire.instrument_mcp.

Scenario: MCP tool call inside a row span

  • WHEN A test runs a fastmcp tool call inside a row span with the global provider set.
  • THEN The OTLP fake holds one MCP span in the trace of the row span and logfire.instrument_mcp is not called.

Requirement: Removed Logfire agent turn

The codebase MUST NOT export logfire_agent_turn, AgentTurnHandle or their render helpers and MUST NOT offer devops telemetry test --logfire.

Scenario: Removed API

  • WHEN A test imports devops_cli.telemetry and runs devops telemetry test --help.
  • THEN The import has no logfire_agent_turn or AgentTurnHandle, and the help output has no --logfire option.

Requirement: OTLP fake decodes protobuf

The OTLP fake SHALL decode application/x-protobuf trace bodies with the official OTLP message.

Scenario: Protobuf export

  • WHEN A test exports one span through the SDK provider to the OTLP fake.
  • THEN The fake exposes the span with its name, trace id and parent span id.

Requirement: Shared exporter

The SDK SpanExporter MUST share the process OtlpExporter and MUST NOT close it on shutdown.

Scenario: Provider shutdown

  • WHEN A test shuts down the SDK provider and then exports an own span.
  • THEN The own span reaches the OTLP fake and the shared OtlpExporter is still open.

Requirement: Offline fast tests

The new tests SHALL run offline and each new test MUST finish in under 1 s.

Scenario: Offline test run

  • WHEN A test runs the new telemetry tests with no network.
  • THEN Every new test passes and each takes less than 1 s.

Existing Tool Considered

No alternatives check has run. The OpenTelemetry SDK, opentelemetry-exporter-otlp-proto-common, the pydantic-ai instrumentation, the native MCP spans and the httpx instrumentor cover the need. They lack one adapter: a SpanExporter that uses encode_spans and sends application/x-protobuf to the egress-checked OtlpExporter. The project builds and keeps that adapter. Cost: about 20,000 to 30,000 tokens to build, about 5,000 to 10,000 tokens each year to keep and fix, and about 1 to 2 days elapsed.

Key questions

  • Which context holds the parent of an own span and of a library span?: Use the OpenTelemetry API context as the only parent source. An own span attaches a NonRecordingSpan with its SpanContext, and a new own span reads the current span context. Delete _current_trace_id_ctx and _current_span_id_ctx. (best practice) [sources: src/devops_cli/telemetry/tracer.py:815]
    • Chosen: OpenTelemetry API context. Library spans read the current span from this context, so they nest under an own row span.
    • Rejected: Own context variables. A library span cannot read these variables, so it becomes a new root.
    • Rejected: Logfire context. It adds a Logfire dependency and keeps a second context.
  • Which encoding does the SDK span exporter use?: Use the official OTLP protobuf encoder encode_spans and send application/x-protobuf through the OtlpExporter seam. Update the OTLP fake to decode the protobuf body. (best practice) [sources: src/devops_cli/telemetry/tracer.py, tests/support/fakes/otlp.py]
    • Chosen: Official OTLP protobuf encoder. It follows the OTLP schema and needs no hand-kept JSON mapping.
    • Rejected: Hand-built OTLP/JSON. It keeps a second mapping of every span field and diverges from the SDK model.
    • Rejected: Stock OTLP/HTTP exporter. It bypasses the egress-checked client and the dry-run rule.
  • Does pydantic-ai export prompt and reply content?: No by default. Set include_content from telemetry.capture_llm_content, which is false by default. Set include_binary_content false and include_model_request_parameters false. (best practice) [sources: src/devops_cli/telemetry/tracer.py]
  • Where and how is pydantic-ai instrumentation installed?: Call Agent.instrument_all once for each process, when the process builds its first pydantic-ai model. Guard the call with a module-level flag. Use the global SDK provider, no metrics and the default format version. (best practice) [sources: src/devops_cli/ai/providers/init.py, src/devops_cli/ai/harness/slots.py]
    • Chosen: Agent.instrument_all once at first model build. It covers stage, refine, review, implement and the harness Coder with one call.
    • Rejected: Instrument each Agent at construction. A new construction path can miss the call and export no span.
    • Rejected: logfire.configure. It adds Logfire cloud settings and does not use the project exporter seam.
  • Which httpx clients get trace context?: Instrument only the gateway client with the OpenTelemetry httpx instrumentor. Do not install the instrumentor globally, so GitHub and other egress requests get no traceparent from it. (best practice) [sources: SECURITY.md, src/devops_cli/telemetry/tracer.py]
    • Chosen: Gateway client only. Trace context goes only to the LiteLLM gateway, as the egress model requires.
    • Rejected: Global httpx instrumentor. It sends trace context to GitHub and to every other host of the egress factory.
    • Rejected: No httpx instrumentation. It loses the gateway span and the trace link to the LLM call.
  • Does the old Logfire agent-turn API stay as a shim?: No. Delete logfire_agent_turn, AgentTurnHandle, their render helpers and devops telemetry test --logfire. The library spans replace them. (best practice) [sources: src/devops_cli/telemetry/logfire.py:97, src/devops_cli/telemetry/init.py:18, tests/telemetry/test_telemetry_logfire.py]
    • Chosen: Delete the API. Only tests call it, and a shim keeps a second content path that the setting does not gate.
    • Rejected: Keep a shim. It keeps duplicate span and content code and a second path to maintain.
  • How does the OTLP fake read protobuf trace exports?: Decode ExportTraceServiceRequest with the generated protobuf message and convert each span to a plain dict. Accept application/x-protobuf and keep the JSON path for existing tests. (best practice) [sources: tests/support/fakes/otlp.py]
    • Chosen: Decode protobuf in the fake. It keeps the test in process and offline, with no collector.
    • Rejected: Keep JSON only. The SDK exporter sends protobuf, so the fake would reject every export.
    • Rejected: Run a loopback collector. It adds a network listener and a slower test.
  • Who owns the OtlpExporter that the SDK provider and the own tracer share?: The process tracer owns the exporter. The custom SpanExporter MUST NOT close it; its shutdown flushes only. (best practice) [sources: src/devops_cli/telemetry/tracer.py, src/devops_cli/telemetry/exporter.py]
    • Chosen: Process tracer owns the exporter. The SDK exporter flushes spans and leaves the shared egress client open.
    • Rejected: SDK provider closes the exporter. It breaks own span export after the provider shuts down.
    • Rejected: Two separate exporters. It doubles the egress client and the connection state.
  • How does the MCP client get spans without duplicate spans?: Use the native spans of fastmcp and the MCP SDK through the global SDK provider. Do not call logfire.instrument_mcp. (best practice) [sources: src/devops_cli/ai/mcp/remote.py, SECURITY.md]
    • Chosen: Native MCP spans. The global provider catches them and each call makes one span.
    • Rejected: logfire.instrument_mcp. It adds a second span and a Logfire dependency.
    • Rejected: No MCP spans. It loses tool call visibility.
  • How does the SDK provider follow install_exporter and reset_tracer?: Build the provider from the process tracer exporter on first use. Reset the SDK provider and the pydantic-ai instrumentation when reset_tracer runs, so the next span uses the installed exporter. (best practice) [sources: src/devops_cli/telemetry/tracer.py, tests/support/fakes/otlp.py]
    • Chosen: Reset with the tracer. The OTLP fake installed by a test becomes the exporter of the next library span.
    • Rejected: Build the provider once. A test fake installed after the first build never receives a span.
    • Rejected: Use a separate exporter. It duplicates the egress client and the dry-run check.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/feature
    scope/telemetry
    status/new
    value/high
    effort/medium
    on Nov 11, 2026
  2. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/feature. The feat prefix of the title gives the type.
    • Scope: scope/telemetry. The (telemetry) scope of the title gives the scope.
    • Priority: P1-High. Zero AI agent spans reach Tempo today, so core observability for the agent workflow is missing.
    • Value: High. Without this change, pydantic-ai, MCP, and httpx spans are invisible to operators.
    • Effort: Medium. The exporter is a thin wrapper, but context unification, three instrumentors, and logfire deletion add moderate work.
    • Related: refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319, which asks for a different change. Both work in the telemetry export path; #1319 delivered the OtlpExporter seam, and the candidate adds the SDK TracerProvider and library span instrumentation that feed into that seam.
  3. added
    priority/p1-high
    on Nov 11, 2026
  4. added
    owner/agent
    on Nov 11, 2026
  5. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  6. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 9087b39 (see proposed design in issue body).

  7. added
    status/in-progress
    and removed
    status/ready
    on Nov 11, 2026
  8. added
    status/in-review
    and removed
    status/in-progress
    on Nov 11, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 11, 2026
  10. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-11 14:13 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p1-highscope/telemetrysource/agenttype/featurevalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions