Skip to content

refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319

Description

@dan-petty

Problem

  • Export is private. OTelTelemetryClient [code: src/devops_cli/telemetry/tracer.py:690] posts traces (:1102) and metrics (:853) through the private _send_payload (:1232), which queues _send_payload_sync (:1216) on a background executor. The sync path posts OTLP/HTTP JSON with a client cached on the tracer by _get_http_client (:1201-1213). The collector probe test_connection (:1127) opens its own socket (:1143) or its own httpx2 client (:1180).
  • The gRPC exporter is dead code. _get_grpc_exporter (:1104-1125) has no caller in src; only tests/test_telemetry.py:440 calls it. Export is OTLP/HTTP JSON whatever protocol says (:703-711); the protocol only picks the probe (:1132). It is the only importer of the opentelemetry-exporter-otlp-proto-grpc dependency [code: pyproject.toml:40].
  • Tests patch or call the privates. 12 test files patch or call _send_payload or _send_payload_sync [measured: grep at 5c45b1e]. forbid_requests patches the private _resolve_telemetry_settings [code: tests/cluster_secret_fakes.py:231].
  • Every test exports. The per-test config turns telemetry on at a loopback collector [code: tests/conftest.py:465, :471], so each span queues a POST that the network guard refuses. test(cli): the hermetic floor fails a test that reaches an edge without a fake and resets all process-wide state after every test #1310 turns export off by default, and a test that checks telemetry opts in with this item's fake (Q28).

Acceptance criteria

Seam

  • An exporter Protocol names what the tracer needs of a collector: export one signal's payload (traces or metrics) and probe the collector. The real implementation is today's OTLP/HTTP JSON export and probe, moved out of the tracer. The tracer takes its exporter from one place, which get_tracer() (:1303) uses.
  • The background executor, the dry-run suppression (:1239) and the success and failure counters stay in the tracer and behave as today.
  • _get_grpc_exporter and _grpc_exporter (:719, :1104-1125, :1270-1275) are deleted, as no public behaviour reaches them (Q22), and opentelemetry-exporter-otlp-proto-grpc leaves the direct dependencies (pyproject.toml:40).

Fake and fixture

  • tests/support/fakes/otlp.py records each export (signal and payload) and offers the exported span names and attributes and metric names and points as plain values. It can answer an export with a failure status or a refused connection, and its probe answer is configurable.
  • A fixture opts the test into telemetry with the fake as exporter, restores the real exporter at teardown and returns it, registered once from the root conftest. Reading the fake first waits for queued exports through the tracer's public flush() (:1249); no test sleeps.

Contract suite

  • One suite in tests/telemetry/ runs against the fake and the real exporter, the real side posting to an OTLP/HTTP receiver on a loopback port the test owns. Cases: the receiver gets the payload sent, on the signal's path; a 2xx counts a success; a 4xx, a 5xx and a refused connection each count a failure with the same last_export_error on both sides; the probe reports a reachable and an unreachable collector.

Migration

Key questions

Strategic alignment

Proposed design

Problem statement

The telemetry exporter in OTelTelemetryClient posts traces and metrics through private methods, and 12 test files patch those privates. The gRPC exporter is dead code with no caller in src. Tests cannot read exported spans and metrics without patching internals. This item creates one exporter seam based on the SDK's SpanExporter and MetricExporter interface, deletes the gRPC path, and gives tests a shared fake and a contract suite so they check exported values, not HTTP requests.

Acceptance criteria

  • Criterion 1: The tracer takes its exporter from one place, and get_tracer() uses it.
    Verification: grep for get_tracer in src/devops_cli/telemetry/tracer.py shows it passes the exporter to the tracer constructor.
  • Criterion 2: The real exporter implements the SDK's SpanExporter and MetricExporter, serialises with the official encoders, and POSTs application/x-protobuf.
    Verification: The exporter module imports encode_spans and encode_metrics from opentelemetry.exporter.otlp.proto.common and sets Content-Type to application/x-protobuf.
  • Criterion 3: The background executor, dry-run suppression, and success and failure counters stay in the tracer and behave as today.
    Verification: tests/test_telemetry_collector_export.py tests for dry-run, shutdown drain, and failure counting pass without change to their assertions.
  • Criterion 4: _get_grpc_exporter, _grpc_exporter, and the opentelemetry-exporter-otlp-proto-grpc dependency are deleted.
    Verification: grep -r '_get_grpc_exporter|_grpc_exporter' src/ returns no results; pyproject.toml has no opentelemetry-exporter-otlp-proto-grpc line.
  • Criterion 5: Setting protocol to grpc raises an error that names the setting.
    Verification: A test sets OTEL_EXPORTER_OTLP_PROTOCOL=grpc and asserts the raised error message contains the setting name.
  • Criterion 6: A fake records each export (signal and payload) and offers span names, attributes, metric names, and points as plain values.
    Verification: A test exports a span and a metric through the fake, then reads the recorded span name and metric name as plain strings.
  • Criterion 7: The fake can answer an export with a failure status or a refused connection, and its probe answer is configurable.
    Verification: A test sets the fake to refuse, exports, and asserts the failure counter increments; a test sets the probe answer and asserts test_connection returns it.
  • Criterion 8: A fixture named otlp opts the test into telemetry with the fake as exporter, restores the real exporter at teardown, and is registered once from the root conftest.
    Verification: pytest --fixtures | grep otlp shows the fixture; a test that requests otlp sees the fake installed and a later test sees the real exporter restored.
  • Criterion 9: Reading the fake first waits for queued exports through the tracer's public flush(); no test sleeps.
    Verification: grep -r 'time.sleep|asyncio.sleep' tests/telemetry/ returns no results in the new tests.
  • Criterion 10: One contract suite in tests/telemetry/ runs against the fake and the real exporter on a loopback port the test owns.
    Verification: pytest tests/telemetry/test_contract.py passes; the real side binds a loopback port and the test asserts the receiver gets the payload on the signal's path.
  • Criterion 11: The contract suite covers: 2xx counts a success; 4xx, 5xx, and refused connection each count a failure with the same last_export_error on both sides; the probe reports reachable and unreachable.
    Verification: pytest tests/telemetry/test_contract.py -v shows all cases pass on both the fake and the real exporter.
  • Criterion 12: tests/test_telemetry_collector_export.py and the export tests in tests/test_telemetry.py use the fake or the contract suite and patch no _send_payload, _send_payload_sync, _get_http_client, or _grpc_exporter.
    Verification: grep -n '_send_payload|_send_payload_sync|_get_http_client|_grpc_exporter' tests/test_telemetry_collector_export.py tests/test_telemetry.py returns no results.
  • Criterion 13: Tests run offline and each new test runs under 1 s.
    Verification: pytest tests/telemetry/ --durations=10 shows no test exceeds 1 s; no external network calls are made.
  • Criterion 14: uv run devops ci passes.
    Verification: The CI check reports a passing status.

Key questions

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    scope/telemetry
    priority/p2-medium
    on Oct 7, 2026
  2. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Amendment proposed by the 2026-10-08 client-library audit: the seam is the OpenTelemetry SDK's exporter interface, not a Protocol around today's hand-built OTLP/HTTP JSON.

    • Why.
      • fix(telemetry): delta metrics from one process can reach the collector out of order #792 (amended 2026-10-04) already moves metrics to the SDK's MeterProvider.
      • The audit filed the matching item for spans: refactor(telemetry): spans are built and exported by the OpenTelemetry SDK, ....
      • The libraries-first rule covers protocol encoding. Keeping _to_otlp_any_value, the per-span POST and the hand-built resource (telemetry/tracer.py:111-129, :732-772, :1101-1102 at 05e9388) as "the real implementation" entrenches what those items delete.
    • Seam. The SDK's SpanExporter and MetricExporter.
      • The real implementation serialises with the official encoders (opentelemetry.exporter.otlp.proto.common.trace_encoder.encode_spans and metrics_encoder.encode_metrics).
      • It POSTs application/x-protobuf through the egress factory. The SDK's stock OTLP/HTTP exporters take only a requests.Session, so they would bypass the factory's connect-time check and refactor(cli): Unify HTTP and DNS seams with shared fakes #1314's HTTP fake.
    • Fake.
      • Preferred: refactor(cli): Unify HTTP and DNS seams with shared fakes #1314's HTTP fake, with tests decoding ExportTraceServiceRequest and ExportMetricsServiceRequest.
      • If an exporter-level fake is still wanted, use the SDK's own InMemorySpanExporter and InMemoryMetricReader rather than a new tests/support/fakes/otlp.py.
    • Unchanged. Deleting _get_grpc_exporter and dropping opentelemetry-exporter-otlp-proto-grpc still hold. grpc as protocol should then fail with an error naming the setting, instead of silently posting HTTP/JSON to a gRPC port (:703-711, :1216).
    • Probe. The collector probe (test_connection, :1127-1191, raw socket :1143 and client :1180) becomes a request through the factory client.

    Folded here instead of a new issue: the agent filing quota (#1153) is spent for this cycle, and this refactor shares this item's change surface. It is recorded here in full so it is not lost.

    refactor(telemetry): spans are built and exported by the OpenTelemetry SDK, and the hand-built OTLP encoder, second context store and traceparent formatters are removed

    Problem

    Lines cited at 05e9388 (release/v0.2.29).

    • The tracer hand-encodes OTLP/HTTP JSON.
      • _to_otlp_any_value (telemetry/tracer.py:111-129) encodes AnyValue by hand.
      • The resource is a hand-built list (:732-772) that names itself telemetry.sdk.name = "devops-cli-otel".
      • Each span becomes a dict that is POSTed on its own (_build_traces_payload([span_data]), :1101-1102), from a two-worker pool (:1196), over a raw httpx2.Client (:1213). The collector is therefore dialled outside the egress factory.
    • The gRPC setting silently drops every span. protocol resolves to grpc for OTEL_EXPORTER_OTLP_PROTOCOL=grpc or a :4317 endpoint (:703-711). _send_payload_sync (:1216) still POSTs HTTP/JSON to {endpoint}/v1/traces. _get_grpc_exporter (:1104-1125) has no caller. The failures are only counted.
    • Standard OpenTelemetry settings are ignored. Nothing in src reads OTEL_RESOURCE_ATTRIBUTES, OTEL_EXPORTER_OTLP_HEADERS, OTEL_TRACES_SAMPLER or OTEL_SDK_DISABLED.
    • There are two tracing pipelines and two context stores.
      • Logfire configures the OpenTelemetry SDK (telemetry/logfire.py:206-210).
      • LogfireOTelBridgeProcessor (:39-84) converts each SDK span back into the hand-built dict (record_completed_span, :84).
      • CLI spans keep their ids in the tracer's own ContextVars (tracer.py:686-687). A pydantic-ai span started inside trace_span finds its parent only where code goes through get_current_span_context (:1408-1430).
    • traceparent is formatted in four places: telemetry/propagation.py:82-95, server/app.py:98 (always sampled, 01), ai/run/__init__.py:67 and the tracer. Parsing is hand-written as well (propagation.py:105-136).
    • The SDK is already installed. opentelemetry-sdk and opentelemetry-exporter-otlp-proto-common 1.44.0 arrive through logfire and through the direct dependency opentelemetry-exporter-otlp-proto-grpc (pyproject.toml:40).
    • No other item moves spans. fix(telemetry): delta metrics from one process can reach the collector out of order #792 moves metrics to the SDK's MeterProvider. refactor(telemetry): OTLP export goes through one exporter seam, and tests read exported spans and metrics from a shared fake #1319, as written, keeps today's OTLP/HTTP JSON export as the real implementation of its seam.

    Proposed remedy

    Key questions

    Acceptance criteria

    • rg -n 'stringValue|resourceSpans|scopeSpans|kvlistValue' src/devops_cli prints nothing.
    • These are deleted: LogfireOTelBridgeProcessor, _current_trace_id_ctx, _current_span_id_ctx, _get_grpc_exporter, and the hand-written traceparent parsing and formatting. One function renders a traceparent.
    • A pydantic-ai span started inside trace_span has the CLI span as its parent: the same trace id, and the CLI span's id as parent span id.
    • The N spans one command ends produce one export request at the HTTP fake. Its body decodes as ExportTraceServiceRequest and carries the resource attributes from OTEL_RESOURCE_ATTRIBUTES.
    • OTEL_EXPORTER_OTLP_PROTOCOL=grpc fails with an error naming the setting.
    • A dry run makes no request (feat(cli): reachable --dry-run previews for delegated commands #412).
    • Tests run offline with fakes only at process and HTTP edges; each new test runs under 1 s.
    • changelog.d/<n>.md records the change; CHANGELOG.md and docs/ROADMAP.md are not edited.
    • uv run devops ci passes.

    Related: #792 (metrics through the SDK), #1319 (exporter seam), #1314 (HTTP seam), #1334, #813, #1418, #702, #1120.

    Found by the 2026-10-08 client-library audit.

  3. dan-petty commented on Oct 8, 2026

    @dan-petty
    OwnerAuthor

    Linked: #792, #813 and #1418 replace or fix other parts of the hand-built OTLP in telemetry/tracer.py, and #1381 lists this item's two clients (:1180, :1213); whichever lands first moves them. Found by the 2026-10-08 backlog review.

  4. 2 remaining items

  5. dan-petty commented on Oct 26, 2026

    @dan-petty
    OwnerAuthor

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

    • Priority: P2-Medium. This item is medium priority as it improves code structure and testability but does not address an immediate bug or security issue.
    • Value: High. High value because it improves the reliability and testability of telemetry exports by centralizing the export mechanism.
    • Effort: High. High effort due to the extensive refactoring required across multiple modules and the need to update tests accordingly.
    • Related: test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334, which asks for a different change. Both items are about telemetry tests and fakes, but the candidate focuses on refactoring OTLP export through a single exporter seam while item #1334 is about migrating test files to use edge fakes.
  6. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 69636b6 (see proposed design in issue body).

  7. added
    status/ready
    effort/high
    value/high
    on Oct 28, 2026
  8. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    PR #2541 builds this item. One decision differs from the refine block's criterion 2:

    • The seam is the collector edge, OtlpExporter in telemetry/exporter.py: export one signal's encoded request, and probe. It is not the SDK's SpanExporter and MetricExporter. A SpanExporter takes SDK ReadableSpan objects, and the tracer's spans stay hand-built dicts until the SDK builds them. That is the refactor the 2026-10-08 amendment recorded here because the filing quota was spent then. It is now refactor(telemetry): Use OTel SDK encoders for spans, metrics, logs #2536, which encodes with the official protobuf encoders and calls this seam, so the egress-checked client, the OTLP fake and its contract suite stay.
    • The rest of the amendment is in the PR: the export and probe clients are on the egress-checked factory, _get_grpc_exporter and opentelemetry-exporter-otlp-proto-grpc are gone, and a gRPC protocol or endpoint fails with an error that names the setting.
    • The fake is FakeCollector (tests/support/fakes/otlp.py, fixture otlp), not the SDK's in-memory doubles: the spans and logs are not SDK objects yet, and the in-memory doubles give no failure or probe answer for the contract suite to compare.
  9. removed
    status/ready
    on Oct 30, 2026
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/telemetrytype/refactorvalue/high

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions