Skip to content

refactor(telemetry): Use OTel SDK encoders for spans, metrics, logs #2536

Open
1 of 1 issue completed
Open

Description

@dan-petty

Problem or Motivation

The 2026-10-08 client-library audit added this work to #1319 as a comment only because the agent filing quota was spent then. #1319 delivers the exporter seam: OtlpExporter and HttpOtlpExporter in src/devops_cli/telemetry/exporter.py, the OTLP fake tests/support/fakes/otlp.py with its contract suite, the move of the export and probe clients onto the egress-checked factory, and the refusal of gRPC. The export requests are still built by hand:

  • telemetry/tracer.py encodes OTLP/JSON by hand: _to_otlp_any_value, the hand-built resource attribute list (telemetry.sdk.name = devops-cli-otel), _build_traces_payload, _build_logs_payload and _encode_sdk_metrics_data with its helpers.
  • Each span is one export request, so the N spans of one command send N requests.
  • LogfireOTelBridgeProcessor (telemetry/logfire.py) turns each SDK span back into the hand-built dict, and CLI spans keep their ids in the tracer's own context variables.
  • traceparent is formatted in several places (telemetry/propagation.py, server/app.py, ai/run/__init__.py and the tracer), and parsed by hand.
  • Nothing in src reads OTEL_RESOURCE_ATTRIBUTES, OTEL_EXPORTER_OTLP_HEADERS, OTEL_TRACES_SAMPLER or OTEL_SDK_DISABLED.

Proposed Solution & Architectural Design

  • One SDK TracerProvider, shared with Logfire, with a BatchSpanProcessor. The SDK builds the Resource from OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES.
  • An SDK SpanExporter, MetricExporter and LogExporter encode with the official encoders (opentelemetry.exporter.otlp.proto.common: encode_spans, encode_metrics, encode_logs) and hand 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, so the egress-checked client and the contract suite stay.
  • The OTLP fake decodes protobuf with the opentelemetry.proto request classes and keeps its plain-value views.
  • TraceContextTextMapPropagator carries context over HTTP headers and, through a getter and setter for the upper-case keys, over TRACEPARENT and TRACESTATE in a child's environment.
  • trace_span, traced and SpanHandle keep their public signatures as thin wrappers over the SDK span.
  • Deleted: the hand-built encoders, LogfireOTelBridgeProcessor, the tracer's context variables and the hand-written traceparent parsing and formatting.
  • When the change is too large for one pull request, it becomes sub-issues, for example spans and propagation first, then metrics and logs.

Target Subsystem

Telemetry: src/devops_cli/telemetry/ (tracer, logfire bridge, propagation, logging bridge), server/app.py, ai/run/__init__.py, and the OTLP fake.

Existing Tool Considered

The OpenTelemetry SDK (opentelemetry-sdk, already a direct dependency) and its official OTLP encoders (opentelemetry-exporter-otlp-proto-common). The SDK's own OTLP/HTTP exporters take only a requests.Session, which would bypass the egress-checked client and the OTLP fake, so the SDK exporters wrap the #1319 seam instead. Nothing is built beyond thin exporter classes; the change deletes the hand-built encoding.

Acceptance criteria

Related

#1319 (the exporter seam it builds on), #1307, #1334, #1314.

Source: #1319

Proposed design

Problem statement

The telemetry subsystem hand-builds OTLP/JSON export requests and sends one HTTP request per span. It hand-parses and formats traceparent in four places. It ignores the OTEL_RESOURCE_ATTRIBUTES, OTEL_EXPORTER_OTLP_HEADERS, OTEL_TRACES_SAMPLER, and OTEL_SDK_DISABLED environment variables. This refactor replaces the hand-built encoding with the OpenTelemetry SDK official protobuf encoders and BatchSpanProcessor. It keeps the egress-checked exporter seam from #1319.

Acceptance criteria

Requirement: No hand-built OTLP JSON identifiers

The source tree SHALL contain no hand-built OTLP JSON field identifiers.

Scenario: grep for OTLP JSON identifiers

  • WHEN rg -n 'stringValue|resourceSpans|scopeSpans|kvlistValue' src/devops_cli is run
  • THEN the command prints nothing

Requirement: Pydantic-ai span parent-child

A pydantic-ai span started inside trace_span SHALL have the CLI span as its parent.

Scenario: span parent-child relationship

  • WHEN a pydantic-ai span is started inside a trace_span context
  • THEN the pydantic-ai span has the same trace id as the CLI span and the CLI span id as its parent span id

Requirement: Batched export

The N spans one command ends SHALL produce one export request at the OTLP fake.

Scenario: multiple spans one request

  • WHEN a command ends N spans
  • THEN the OTLP fake receives one export request whose body decodes as ExportTraceServiceRequest and carries the resource attributes of OTEL_RESOURCE_ATTRIBUTES

Requirement: Single traceparent renderer

One function SHALL render a traceparent.

Scenario: traceparent rendering

  • WHEN code needs to format a traceparent header
  • THEN it calls the single rendering function

Requirement: Dry run makes no request

A dry run SHALL make no export request.

Scenario: dry run no export

  • WHEN a command runs in dry-run mode
  • THEN no export request is sent to the collector

Requirement: Contract suite with protobuf

The contract suite of #1319 SHALL pass with protobuf bodies on both sides.

Scenario: contract suite protobuf

  • WHEN the contract suite runs the same cases over the OTLP fake and over HttpOtlpExporter
  • THEN all cases pass with application/x-protobuf content type

Requirement: Offline tests under one second

Each new test SHALL run offline in under one second.

Scenario: test duration

  • WHEN each new test runs
  • THEN it completes in under one second without network access

Requirement: CI passes

uv run devops ci SHALL pass.

Scenario: CI gate

  • WHEN uv run devops ci is run
  • THEN all checks pass

Existing Tool Considered

opentelemetry-sdk (already a direct dependency) and opentelemetry-exporter-otlp-proto-common (the official OTLP protobuf encoders: encode_spans, encode_metrics, encode_logs). The SDK's own OTLP/HTTP exporters take only a requests.Session, which bypasses the egress-checked client of #1319. The project builds three thin exporter classes (SpanExporter, MetricExporter, LogExporter) that call the official encoders and hand the application/x-protobuf body to the OtlpExporter protocol, and updates the OTLP fake to decode protobuf with opentelemetry.proto request classes. The written cost is the three thin exporter classes and the updated OTLP fake decoder; the change deletes the hand-built encoders, LogfireOTelBridgeProcessor, the tracer context variables, and the hand-written traceparent code.

Key questions

  • Which library provides the OTLP protobuf encoders?: opentelemetry-exporter-otlp-proto-common provides encode_spans, encode_metrics, and encode_logs. The project calls these functions and hands the resulting application/x-protobuf body to the OtlpExporter protocol of refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319. (best practice) [sources: src/devops_cli/telemetry/exporter.py, openspec/changes/archive/2026-10-28-1319-otlp-exporter-seam/proposal.md]
  • How do the SDK exporters integrate with the refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319 OtlpExporter seam?: Three thin classes (SpanExporter, MetricExporter, LogExporter) implement the SDK exporter interfaces. Each calls the official encoder and hands the protobuf body to the OtlpExporter.export method with content_type application/x-protobuf. The egress-checked client and the contract suite stay unchanged. (best practice) [sources: src/devops_cli/telemetry/exporter.py, tests/support/fakes/otlp.py]
    • Chosen: Thin exporter classes wrapping the OtlpExporter protocol. Keeps the egress-checked client, the contract suite, and the OTLP fake. Adds only three small classes.
    • Rejected: Replace the OtlpExporter seam with the SDK exporters. Loses the egress-checked client and the contract suite. The SDK exporters use requests.Session directly.
  • Which propagator handles traceparent over HTTP headers and child environment?: TraceContextTextMapPropagator from opentelemetry-api carries context over HTTP headers. A getter and setter for the upper-case keys TRACEPARENT and TRACESTATE carry it in a child process environment. This replaces the hand-written parsing and formatting in telemetry/propagation.py, server/app.py, ai/run/init.py, and the tracer. (best practice) [sources: src/devops_cli/telemetry/propagation.py]
    • Chosen: TraceContextTextMapPropagator with env getter and setter. The standard W3C propagator. One function renders a traceparent. The env getter and setter cover child processes.
    • Rejected: Keep hand-written traceparent parsing and formatting. The goal is to delete the hand-written code. It is duplicated in four places.
  • How does the OTLP fake decode protobuf export requests?: The fake decodes the application/x-protobuf body with the opentelemetry.proto request classes (ExportTraceServiceRequest, ExportMetricsServiceRequest, ExportLogsServiceRequest). It keeps its plain-value views (ExportedSpan, metric points, log records) so existing tests read the same shape. (best practice) [sources: tests/support/fakes/otlp.py]
    • Chosen: Decode with opentelemetry.proto request classes. The official protobuf classes parse the wire format correctly. The fake keeps its plain-value views for test readability.
    • Rejected: Keep decoding JSON. The body will now be protobuf. JSON decoding will fail.
  • Does the change fit in one pull request?: No. The change touches the tracer, the logfire bridge, propagation, the logging bridge, metrics, server/app.py, ai/run/init.py, the OTLP fake, and the contract suite. The body suggests splitting into spans and propagation first, then metrics and logs. (best practice) [sources: src/devops_cli/telemetry/tracer.py, src/devops_cli/telemetry/logfire.py:40, src/devops_cli/telemetry/propagation.py, tests/support/fakes/otlp.py]
    • Chosen: Split into two pull requests: spans and propagation, then metrics and logs. Each PR is smaller and reviewable. The spans and propagation PR delivers the BatchSpanProcessor, the propagator, and the traceparent cleanup. The metrics and logs PR delivers the MetricExporter and LogExporter.
    • Rejected: One large pull request. Too many files and too much new code for one review. The body itself suggests splitting.
  • How does the SDK build the Resource from environment variables?: The SDK TracerProvider reads OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES to build the Resource. This replaces the hand-built resource attribute list (telemetry.sdk.name = devops-cli-otel) in the tracer. (best practice) [sources: src/devops_cli/telemetry/tracer.py]
    • Chosen: Let the SDK read OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES. The standard OpenTelemetry environment variables. The SDK already parses them. This deletes the hand-built resource attribute list.
    • Rejected: Keep the hand-built resource attribute list. Does not support OTEL_RESOURCE_ATTRIBUTES. The goal is to support the standard variables.
  • What happens to the public API of trace_span, traced, and SpanHandle?: They keep their public signatures as thin wrappers over the SDK span. Callers see no change. The internal implementation switches from the hand-built tracer to the SDK TracerProvider. (best practice) [sources: src/devops_cli/telemetry/tracer.py, src/devops_cli/telemetry/init.py]
    • Chosen: Keep public signatures as thin wrappers over SDK spans. Callers see no change. The wrappers are small and testable.
    • Rejected: Expose SDK spans directly to callers. Breaks every caller of trace_span, traced, and SpanHandle. Too large a change for this issue.
  • What happens to LogfireOTelBridgeProcessor?: It is deleted. The SDK TracerProvider is shared with Logfire, so Logfire spans enter the same BatchSpanProcessor and export through the same seam. The bridge that turned each SDK span back into a hand-built dict is no longer needed. (best practice) [sources: src/devops_cli/telemetry/logfire.py:40, src/devops_cli/telemetry/init.py:101]
    • Chosen: Delete LogfireOTelBridgeProcessor and share the TracerProvider with Logfire. The bridge exists only because the tracer and Logfire used separate span pipelines. A shared TracerProvider removes the need.
    • Rejected: Keep the bridge. The goal is to delete hand-built code. The bridge is hand-built and becomes redundant with a shared TracerProvider.
  • Is opentelemetry-sdk already a direct dependency of this project?: Yes. The body states it is already a direct dependency. The knowledge base document for OpenTelemetry confirms the SDK is in use. [sources: src/devops_cli/ai/knowledge_base/devops_cli/libraries/opentelemetry.md:51]
  • Does the refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319 exporter seam exist and is it delivered?: Yes. The OtlpExporter protocol, HttpOtlpExporter, and the OTLP fake are in the codebase. The refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319 change is archived in openspec/changes/archive/2026-10-28-1319-otlp-exporter-seam/. [sources: src/devops_cli/telemetry/exporter.py, tests/support/fakes/otlp.py, openspec/changes/archive/2026-10-28-1319-otlp-exporter-seam/proposal.md]

Fit

Needs splitting into 2 items:

  1. Spans and propagation: SDK TracerProvider with BatchSpanProcessor, TraceContextTextMapPropagator with env getter and setter, delete LogfireOTelBridgeProcessor, update server/app.py and ai/run/init.py, OTLP fake decodes protobuf traces, contract suite passes with protobuf traces
  2. Metrics and logs: SDK MetricExporter and LogExporter with official encoders, OTLP fake decodes protobuf metrics and logs, contract suite passes with protobuf metrics and logs

Suspected block

None.

Dependencies

#1319

Alternatives check of 2026-11-04: not-applicable.
Class: refactor, no search.
Tool: none found.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    source/agent
    type/refactor
    status/new
    value/medium
    effort/high
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 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/refactor. The title is prefixed refactor and the body replaces hand-built encoders with SDK-provided ones without changing the public API.
    • Priority: P2-Medium. It improves performance and maintainability but the current hand-built code functions, so it is not urgent.
    • Value: Medium. Users gain batched exports, OTEL_* environment variable support, and reduced request count per command.
    • Effort: High. The change touches tracer, logfire bridge, propagation, server app, ai/run, and the OTLP fake, and the body notes it may split into sub-issues.
    • Related: feat(telemetry): opentelemetry w3c traceparent context propagation, metric sdk & span waterfall optimization research #319, which asks for a different change. Both work in the telemetry subsystem on traceparent propagation and the OpenTelemetry SDK, but #319 adds initial propagation and metric SDK while the candidate replaces the encoding layer with official SDK protobuf encoders and BatchSpanPro
  3. added
    priority/p2-medium
    on Oct 28, 2026
  4. devops-cli-service commented on Oct 29, 2026

    @devops-cli-service
    Contributor

    The proposal at 0f7bc26 needs splitting into 2 items. A person splits the item into the items below, or replies in a comment or edits the body to keep one item, and the next refine round looks at the item again.

    1. Spans and propagation: SDK TracerProvider with BatchSpanProcessor, TraceContextTextMapPropagator with env getter and setter, delete LogfireOTelBridgeProcessor, update server/app.py and ai/run/init.py, OTLP fake decodes protobuf traces, contract suite passes with protobuf traces
    2. Metrics and logs: SDK MetricExporter and LogExporter with official encoders, OTLP fake decodes protobuf metrics and logs, contract suite passes with protobuf metrics and logs
  5. added
    scope/telemetry
    on Oct 30, 2026
  6. devops-cli-service commented on Nov 4, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: refactor. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-04. A person decides what this means for the item. The check changes no label and closes no issue.

  7. changed the title [-]refactor(telemetry): spans, metrics and logs are built and encoded by the OpenTelemetry SDK and sent as OTLP protobuf through the exporter seam[/-] [+]refactor(telemetry): Use OTel SDK encoders for spans, metrics, logs[/+] on Nov 6, 2026
  8. dan-petty commented on Nov 6, 2026

    @dan-petty
    OwnerAuthor

    The job shortened the title of this item. The summary had 122 characters, and the limit is 60. The type and the scope stay.

    • Before: refactor(telemetry): spans, metrics and logs are built and encoded by the OpenTelemetry SDK and sent as OTLP protobuf through the exporter seam
    • After: refactor(telemetry): Use OTel SDK encoders for spans, metrics, logs
    • Source of the summary: the background model
  9. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor

    A measure for the point "Each span is one export request", on main (752c6ed). Tracer ends each span with _send_payload("/v1/traces", self._build_traces_payload([span_data])) (src/devops_cli/telemetry/tracer.py line 1415). Each program that core/process.py starts opens a span subprocess.<bin> (line 905). The project review of #4692 starts one openspec show program for each archived change: a 7-day devops roadmap review run makes 329 OpenSpec reads. So, with tracing on, one review sends about 350 trace requests. The BatchSpanProcessor of the design of this item removes that cost.

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/highpriority/p2-mediumscope/telemetrysource/agentstatus/newtype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions