Skip to content

fix(telemetry): send the CLI usage error counter to Prometheus #5220

Description

@dan-petty

Problem or Motivation

The counter devops_cli_usage_error_total (src/devops_cli/telemetry/instruments.py:49) has 0 series in Prometheus. Loki holds 160 cli_usage_error records of the same errors (the 7 days to 2026-11-12, at about 19:45Z).

record_usage_error (src/devops_cli/telemetry/usage_errors.py:153-171) sends each usage error to three sinks: the counter, the log record cli_usage_error and the ledger. The log record arrives in Loki. The counter does not arrive in Prometheus. Other metrics of the CLI arrive: on 2026-11-12, devops_cli_command_total has 8 series and devops_cli_subcommand_duration_seconds has 6.

So a PromQL query on the counter gives no data, and a dashboard or a review row that reads it shows no usage error. A #4692 scope comment of 2026-11-11 adds a PromQL row on this counter. #5219 removes that row and reads Loki, but the counter must still work, because its module docstring and the metric list name it.

Proposed Solution & Architectural Design

  1. Find the cause with a failing test first (AGENTS.md rule 2). A possible cause: the root group UsageRecordingGroup (src/devops_cli/core/cli.py:201-226) records the error in make_context, before the command span opens in src/devops_cli/main.py, and the process exits with code 2 before the meter provider exports. The log bridge exports, so the fault is in the metric path.
  2. Fix the cause at its source: the counter goes through the same export path as devops_cli_command_total, and the metrics flush before the exit with code 2.
  3. Add no new counter and no new sink.

Target Subsystem

src/devops_cli/telemetry/ (usage_errors.py, instruments.py, the tracer and the exporter), src/devops_cli/core/cli.py.

Existing Tool Considered

The OpenTelemetry SDK metrics (MeterProvider.force_flush), which the project uses now. No new tool.

Acceptance Criteria

  • A test with an in-memory metric reader runs an unknown command and reads 1 on devops_cli_usage_error_total with the labels kind, command_path and caller. It fails before the fix. The fix PR cites its devops ci fail-to-pass run.
  • After the release, count(devops_cli_usage_error_total) in Prometheus gives 1 or more series in the hour after a usage error of a released CLI.
  • docs/TELEMETRY.md and the module docstring stay true.

Dependencies

None.

Related

Source: #5219

Alternatives check of 2026-11-28: in-use.
The repository already uses this tool.
Tool: open-telemetry/opentelemetry-python (https://github.com/open-telemetry/opentelemetry-python), Apache-2.0, last activity 2026-11-27, maintained.
Reason: Searches confirm the OpenTelemetry Python SDK, already in use, supplies the counter instrument, MeterProvider.force_flush and the Prometheus exporter; the only gap is the project's own flush call on the usage-error path before exit code 2.

Covers:

  • Counter instrument with kind, command_path and caller labels
  • MeterProvider.force_flush exports pending metrics before exit
  • Prometheus exporter and in-memory metric reader for offline tests

Lacks:

  • The project must call force_flush on the usage-error path itself
  • No export happens without that code change before exit code 2
  • Docstring and telemetry docs update stay the project's work

Proposed design

Problem statement

The CLI records usage errors to devops_cli_usage_error_total, but Prometheus holds no series for that counter. The same errors arrive in Loki, so the defect is in the metric export path. The change records the usage error, flushes the existing meter provider before the exit with code 2, and proves the counter with an in-memory reader test.

Acceptance criteria

Requirement: Usage error counter export

The CLI MUST export devops_cli_usage_error_total before it exits with code 2 for a usage error.

Scenario: Unknown command with an in-memory reader

  • WHEN A test runs an unknown command through the root CLI with an in-memory metric reader.
  • THEN The reader holds 1 on devops_cli_usage_error_total with the kind, command_path and caller labels.

Requirement: Meter provider flush

The CLI MUST flush the meter provider after it records a usage error and before it exits with code 2.

Scenario: Flush on an unknown command

  • WHEN A test replaces the meter provider with a stub and runs an unknown command through the root CLI.
  • THEN The stub records a force_flush call before the command exits with code 2.

Requirement: Usage error labels

The usage-error counter MUST keep the kind, command_path and caller labels.

Scenario: Labels of an unknown command

  • WHEN A test records an unknown command with the in-memory reader.
  • THEN The counter has one series with the expected kind, command_path and caller values.

Requirement: Telemetry documentation

The change MUST keep docs/TELEMETRY.md and the record_usage_error docstring true about the flush.

Scenario: Docs check

  • WHEN uv run devops ci --check --only docs runs on the change.
  • THEN The check passes.

Requirement: Prometheus series after release

A released CLI MUST send devops_cli_usage_error_total to Prometheus when a usage error occurs.

Scenario: PromQL query after a usage error

  • WHEN A released CLI records a usage error and a person runs the PromQL query count(devops_cli_usage_error_total) in Prometheus.
  • THEN The query returns 1 or more series in the hour after the error.

Existing Tool Considered

The OpenTelemetry Python SDK (Apache-2.0, maintained, last activity 2026-11-15) is already in use. It covers the counter instrument, MeterProvider.force_flush and shutdown, the Prometheus-compatible exporter and an in-memory metric reader. It lacks the project's own recording and flush order: the code must call the flush on the usage-error path, guarantee that the counter reaches Prometheus, and update the documentation. No new tool, counter or sink is needed.

Key questions

  • What makes the usage-error counter miss Prometheus while the log record arrives?: The shown code records the usage error in UsageRecordingGroup.make_context before the delegated command span. main.py flushes only the tracer on the delegated path, and the shown code has no metric flush before the Typer exit with code 2. The log record uses a different path and arrives. The failing test must confirm this cause. [sources: src/devops_cli/core/cli.py:201, src/devops_cli/main.py, src/devops_cli/telemetry/usage_errors.py:153, docs/TELEMETRY.md]
  • Where should the CLI flush the usage-error metric before it exits with code 2?: Call force_flush() on the existing meter provider in record_usage_error after emit(). This sends the pending delta on the same export path as devops_cli_command_total. Do not call shutdown(), because a long-lived MCP process would stop later telemetry. (best practice) [sources: src/devops_cli/telemetry/usage_errors.py:153, docs/TELEMETRY.md]
    • Chosen: Force flush after the usage-error emit. The usage error is the last telemetry event before Typer exits, and force_flush sends the pending delta without stopping the provider.
    • Rejected: Shut down the meter provider on each usage error. shutdown stops the provider, so later metrics in a long-lived MCP process would be lost.
    • Rejected: Register an atexit handler for the meter provider. The handler may not be registered on the usage-error path before Typer exits, and the order is not guaranteed.
  • How should the test prove the counter export offline?: Use an in-memory metric reader with a test MeterProvider, run an unknown command through the root group, and read devops_cli_usage_error_total after the flush. Assert 1 with the kind, command_path and caller labels. This fails before the fix. (best practice) [sources: tests/telemetry/test_usage_errors.py, src/devops_cli/telemetry/instruments.py]
    • Chosen: In-memory metric reader on the production recording path. It checks the real instrument and labels offline and is deterministic.
    • Rejected: Prometheus query in the unit test. It needs a live cluster and is not deterministic.
    • Rejected: Assert the cli_usage_error log record. The log already arrives, so it does not prove the metric export.
  • What documentation must change with the flush?: Update the record_usage_error docstring to state that it flushes the metric before the usage error exits. Keep docs/TELEMETRY.md true: it already says the provider flushes when the command finishes, so add the usage-error path only if the text does not cover it. (best practice) [sources: src/devops_cli/telemetry/usage_errors.py:153, docs/TELEMETRY.md]
    • Chosen: Update the module docstring and verify the telemetry reference. The docstring names the side effects of the function, and the reference must not contradict the new flush.
    • Rejected: Change no documentation. The function gains a side effect, and the acceptance criteria require the documents to stay true.
  • Does the fix need a new counter or a new sink?: No. Reuse USAGE_ERROR_TOTAL and emit(). Add only the flush on the existing path. (best practice) [sources: src/devops_cli/telemetry/instruments.py:49, src/devops_cli/telemetry/usage_errors.py:153]
    • Chosen: Reuse the existing counter and emit path. The counter exists and the other CLI metrics already use this path.
    • Rejected: Add a second usage-error counter. It duplicates the series and breaks the metric list.
    • Rejected: Add a new sink. The issue forbids a new sink, and the fault is in the flush order.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/bug
    scope/telemetry
    status/new
    value/medium
    effort/medium
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The issue is an internal telemetry defect, and it already names the tool that covers it: the OpenTelemetry Python metrics SDK, "which the project uses now" (its MeterProvider.force_flush and metric readers/exporters). The web searches returned no relevant alternatives (only unrelated MDN glossary pages), while the facts read confirm the SDK repository and package that provide the counter export, flush and in-memory reader the acceptance criteria need; the remaining work is a code fix in this project's own recording and shutdown path.

    The repository already uses this tool.

    Tool: open-telemetry/opentelemetry-python (https://github.com/open-telemetry/opentelemetry-python)

    • License: Apache-2.0
    • Last activity: 2026-11-15
    • Maintained: yes

    Covers:

    • The metrics SDK the CLI already uses to create the counter devops_cli_usage_error_total (MeterProvider and metric instruments)
    • MeterProvider.force_flush (and shutdown) flushes pending metric data before a process exits with code 2
    • A metric reader with a Prometheus-compatible exporter is the path that carries the counter series to Prometheus, the same path devops_cli_command_total uses
    • An in-memory metric reader gives the failing-then-passing test of the acceptance criteria

    Lacks:

    • It does not choose when this CLI records or flushes: the recording in UsageRecordingGroup.make_context before the command span opens, and the exit with code 2 before export, are this project's code and must be fixed there
    • It adds no guarantee that a counter recorded outside an export cycle reaches Prometheus; the fix (same export path as devops_cli_command_total, flush before exit) is not something the SDK does on its own
    • It does not update docs/TELEMETRY.md or the module docstring, which the acceptance criteria require to stay true

    Tools considered:

    Searches run:

    • OpenTelemetry Python SDK MeterProvider force_flush metric reader Prometheus exporter, results: 5
    • opentelemetry-python MeterProvider force_flush PeriodicExportingMetricReader shutdown export metrics, results: 5
    • OpenTelemetry Python metrics SDK documentation, results: 5

    Sources:

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

  6. added
    status/ready
    and removed
    status/new
    on Nov 17, 2026
  7. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 0857ee5 (see proposed design in issue body).

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

    @devops-cli-service
    Contributor

    Live since 2026-11-29 05:10 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/p2-mediumscope/telemetrysource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions