Repository navigation
Description
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
- 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 inmake_context, before the command span opens insrc/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. - 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. - 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_totalwith the labelskind,command_pathandcaller. It fails before the fix. The fix PR cites itsdevops ci fail-to-passrun. - 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.mdand the module docstring stay true.
Dependencies
None.
Related
- feat(telemetry): record each CLI usage error by name and kind #4839 (the usage error record), feat(telemetry): rank CLI usage errors and propose fixes #4840 (rank the usage errors), feat(roadmap): read project review facts through a catalog #4692 (the catalog row), feat(roadmap): report job grades and fix effects in the project review #5219 (the review section that found this problem).
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_totalwith thekind,command_pathandcallerlabels.
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_flushcall 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_pathandcallervalues.
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 docsruns 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_contextbefore the delegated command span.main.pyflushes 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 inrecord_usage_errorafteremit(). This sends the pending delta on the same export path asdevops_cli_command_total. Do not callshutdown(), 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_flushsends the pending delta without stopping the provider. - Rejected: Shut down the meter provider on each usage error.
shutdownstops the provider, so later metrics in a long-lived MCP process would be lost. - Rejected: Register an
atexithandler for the meter provider. The handler may not be registered on the usage-error path before Typer exits, and the order is not guaranteed.
- Chosen: Force flush after the usage-error emit. The usage error is the last telemetry event before Typer exits, and
- 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 readdevops_cli_usage_error_totalafter the flush. Assert 1 with thekind,command_pathandcallerlabels. 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_errorlog record. The log already arrives, so it does not prove the metric export.
- What documentation must change with the flush?: Update the
record_usage_errordocstring to state that it flushes the metric before the usage error exits. Keepdocs/TELEMETRY.mdtrue: 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_TOTALandemit(). 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
dan-petty commentedon Nov 12, 2026
Both concern the CLI usage error counter, but #4839 adds the recording mechanism while the candidate fixes the export path so the counter reaches Prometheus.devops-cli-service commentedon Nov 16, 2026
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_flushand 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)
Covers:
devops_cli_usage_error_total(MeterProvider and metric instruments)MeterProvider.force_flush(and shutdown) flushes pending metric data before a process exits with code 2devops_cli_command_totalusesLacks:
UsageRecordingGroup.make_contextbefore the command span opens, and the exit with code 2 before export, are this project's code and must be fixed theredevops_cli_command_total, flush before exit) is not something the SDK does on its owndocs/TELEMETRY.mdor the module docstring, which the acceptance criteria require to stay trueTools considered:
Searches run:
Sources:
Checked on 2026-11-16. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 17, 2026
Status set to Ready at 0857ee5 (see proposed design in issue body).
chore(main): release 0.245.0 (#7689)
devops-cli-service commentedon Nov 29, 2026
Released in v0.245.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.245.0
devops-cli-service commentedon Nov 29, 2026
Live since 2026-11-29 05:10 UTC