Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ native histogram under the instrument's own name, with no `_bucket`, `_sum` or
| `devops_cli_ai_spend_usd_total` | Counter | `USD` | Approximate LLM and embedding spend, by operation, provider, model, server and backend. Each request that a model answers adds to it, and a local request adds 0. A hosted gateway call is priced at the deployment model that the gateway names, and its model label is the model that the client asked for, such as the route name. |
| `devops_cli_ai_json_repairs_total` | Counter | `1` | LLM JSON replies repaired after strict parsing failed, by entry point source. |
| `devops_cli_ai_gateway_slot_wait_seconds` | Histogram | `s` | Time a chat call to the LLM gateway waited in this process for one of the gateway's backend slots, by outcome: ok once it held one, or timeout. |
| `devops_cli_ai_ollama_slot_wait_seconds` | Histogram | `s` | Time a call to Ollama waited in this process for a slot on one of its nodes, by priority (high, normal or as_available) and outcome: ok once it held one, or timeout. |
| `devops_cli_rag_query_duration_ms` | Histogram | `ms` | RAG retrieval time, by stage: embedding, search, ranking, or total for the whole query. |
| `devops_cli_qdrant_retries_total` | Counter | `1` | Qdrant requests retried after a transient error, by operation and error type. |
| `devops_cli_rag_points` | Gauge | `1` | Points stored in RAG vector collections, by collection. |
Expand Down Expand Up @@ -102,6 +103,7 @@ LLM spans follow the OpenTelemetry GenAI semantic conventions, which live in `op
- **Inference spans**: `ai.llm.dispatch` and `ai.llm.stream` are CLIENT spans of the `gen_ai.inference.client` type. Both carry `gen_ai.operation.name` (`chat`), `gen_ai.provider.name` (`anthropic` for the `claude` provider, other provider ids unchanged) and `gen_ai.request.model`, plus `server.address` and `server.port` when one backend host served the request. Only `ai.llm.dispatch` carries the reply's usage (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) and `gen_ai.response.model`. `ai.llm.stream` carries `gen_ai.request.stream` and `gen_ai.response.time_to_first_chunk` in seconds. Both carry `gen_ai.response.finish_reasons` with the reason the provider gave for the reply's end (`stop`, `length`, `content_filter` or `tool_call`), and leave it out when the provider gave none.
- **Wrapper spans**: `ai.llm.chat` and `ai.client.chat_structured` are INTERNAL and set no operation, usage or response keys, so each request is counted once.
- **Gateway slot wait**: A chat call to the LLM gateway first waits in the process for one of the gateway's backend slots. The wait is an INTERNAL `ai.llm.gateway_slot` span under the call's `ai.llm.dispatch` or `ai.llm.stream` span, with `llm.gateway.slots` and `llm.gateway.slot_wait_seconds`, and an observation of `devops_cli_ai_gateway_slot_wait_seconds`. The call's `llm.wall_seconds`, its time to first chunk and its spend's duration start once it holds the slot, so the wait is not counted as inference.
- **Ollama slot wait**: A call to Ollama first waits in the process for a slot on one of its nodes, queued by priority: `high` first, `as_available` last. Each call observes its wait once in `devops_cli_ai_ollama_slot_wait_seconds`, by `priority` and by `outcome`: `ok` once it holds a slot, or `timeout` when its deadline passes with no slot. A call that finds a free slot observes about 0 s, so the quantiles cover every call.
- **Private keys**: Anything the conventions do not define is written under `llm.*`, such as `llm.request.priority`, `llm.tokens_per_second` and `llm.usage.cost_usd`, never under `gen_ai.*`.
- **Check**: `tests/test_telemetry_semconv.py` reads every Python file under `src/devops_cli/`, comments and docstrings included. It fails on a quoted string starting with `gen_ai.` that is not a current attribute key or metric name in the snapshot, and on any f-string that builds one.
- **Refresh**: A person moves the pin with `devops telemetry semconv refresh --commit <sha>`, which needs weaver on PATH and network access, then fixes what the check reports. The gate never runs weaver.
78 changes: 78 additions & 0 deletions k8s/monitoring/dashboards/ai-spend.json
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,84 @@
"refId": "A"
}
]
},
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"description": "The 95th percentile of the time a call to Ollama waited in its process for a slot on one of its nodes, over the last hour, by priority (high, normal or as_available), with the count of calls that asked for a slot on the right axis. A call that timed out counts at the wait it had. Approximate: increase() needs two samples of a series inside its window, so a series' first sample, and any series seen only once in the window, add nothing, and delta points from one process can arrive out of order (#792). The counts lost can fall in any bucket, so a quantile can come out high or low.",
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"drawStyle": "line",
"lineInterpolation": "smooth"
},
"min": 0,
"unit": "s"
},
"overrides": [
{
"matcher": {
"id": "byRegexp",
"options": "^calls "
},
"properties": [
{
"id": "unit",
"value": "short"
},
{
"id": "custom.axisPlacement",
"value": "right"
},
{
"id": "custom.axisLabel",
"value": "calls"
}
]
}
]
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 36
},
"id": 20,
"options": {
"legend": {
"displayMode": "list",
"placement": "bottom"
}
},
"title": "Ollama Slot Wait by Priority (p95)",
"type": "timeseries",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"expr": "histogram_quantile(0.95, sum by (priority) (increase(devops_cli_ai_ollama_slot_wait_seconds[1h])))",
"legendFormat": "p95 {{{{priority}}}}",
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "prometheus"
},
"expr": "histogram_count(sum by (priority) (increase(devops_cli_ai_ollama_slot_wait_seconds[1h])))",
"legendFormat": "calls {{{{priority}}}}",
"refId": "B"
}
]
}
],
"refresh": "10s",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-28
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Design

## The instrument

`devops_cli_ai_ollama_slot_wait_seconds` is a histogram in seconds with two labels:

- `priority`: the `RequestPriority` value that the call waited with (`high`, `normal` or `as_available`). The value is the resolved priority: the argument when the caller gives one, else the `current_request_priority` of the context.
- `outcome`: `ok` when the call holds a slot, `timeout` when its deadline passed with no slot. The words are those of `devops_cli_ai_gateway_slot_wait_seconds`, so one query reads both histograms the same way. An `as_available` call with no free slot has a deadline of 0 s, so it observes `timeout` at once.

## Where the wait is measured

The clock starts before the call takes `ollama_slot_condition` and stops when `_wait_for_slot` returns or raises its `AIClientError`. The wait for the lock is part of the queue, so it counts. `emit` runs after the call leaves the condition, so the export never holds the lock that the other callers wait on.

Each call observes once. A call that finds a free slot observes about 0 s. The quantiles then describe all calls, and the count of the histogram is the count of slot requests.

## The panel

"Ollama Slot Wait by Priority (p95)" sits in the free half of the last row of `ai-spend.json` (x 12, y 36), beside "Response Cache Hit Share". It charts:

- `histogram_quantile(0.95, sum by (priority) (increase(devops_cli_ai_ollama_slot_wait_seconds[1h])))`, the p95 wait over the last hour by priority, outcomes together. A timeout counts at the wait that it had, so a timeout pushes the quantile up.
- `histogram_count(sum by (priority) (increase(devops_cli_ai_ollama_slot_wait_seconds[1h])))`, the slot requests of the hour, so a p95 over a few calls is visible as such.

The description states the limits of `increase()` that `test_every_panel_counting_with_increase_states_its_limits` asks for.

## Rejected alternatives

- **The name `devops_cli_ai_slot_wait_seconds` that the issue proposed.** Rejected: since #815 was filed, `devops_cli_ai_gateway_slot_wait_seconds` records the gateway's slot. A name with no pool reads as if it covered both waits, and it covers only Ollama's.
- **One histogram for both pools, with a `pool` label.** Rejected: the gateway's slot has no priority, so its points would carry an empty `priority`. The gateway histogram also has a span and docs of its own (`ai.llm.gateway_slot`). A rename of it is not part of this issue.
- **A span for the Ollama wait, as the gateway has.** Not asked by #815. The metric covers the need, and a span adds one span to each Ollama call.
- **Gauges of the lease counts and queue lengths.** Rejected by the issue: each command is its own process, so a gauge keeps only the last process's value.
- **Observe only the calls that waited.** Rejected: the quantiles would then ignore the calls that had no wait, and the count would not show how often calls queue.

## Test

The tests call `acquire_ollama_slot` and read the exported points through the `captured` fixture. No test patches a devops-cli name.

- A call with a free slot sends one point with its priority and outcome `ok`.
- A `normal` call that waits behind a held slot sends one point with outcome `ok` after the holder releases. The test waits until `get_ollama_waiting_counts` shows the call in the queue, then releases the slot.
- An `as_available` call with no free slot raises `AIClientError` and sends one point with outcome `timeout`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Proposal

## Why

Issue #815: a call to Ollama waits in the process for a slot on one of its nodes. `acquire_ollama_slot` (`src/devops_cli/ai/client/network.py`) queues the calls by priority: a `high` call goes first, a `normal` call waits while a `high` call waits, and an `as_available` call waits while any other call waits and leaves one slot free. No metric records this wait. A slow call that queued in the process looks like a slow reply from the model, and nobody can see whether the priority order starves the `normal` and `as_available` calls.

The lease and queue counts are module state of one process (`active_ollama_requests`, `waiting_priority_requests`). Each `devops` command is a short-lived process, so a gauge of these counts would hold only the last process's value. The wait of each call is data of that call, and a histogram sends it as a delta that the collector adds up across processes.

## What Changes

- Add the histogram `devops_cli_ai_ollama_slot_wait_seconds` to `INSTRUMENTS` (`src/devops_cli/telemetry/instruments.py`), labelled by `priority` (`high`, `normal` or `as_available`) and `outcome` (`ok` or `timeout`).
- `acquire_ollama_slot` observes the wait of each call once, through `emit`: when it holds a slot, with outcome `ok`, and when its deadline passes with no slot, with outcome `timeout`. A call that finds a free slot observes a wait of about 0 s, so the quantiles cover all calls.
- Add the panel "Ollama Slot Wait by Priority (p95)" to `k8s/monitoring/dashboards/ai-spend.json`. It charts the p95 wait by priority, and the timeout share by priority.
- `docs/TELEMETRY.md` names the new histogram and says what its wait covers (generated by `devops docs generate --sync-readme`).
- The change carries a spec delta, because a metric and a dashboard panel are observable. It creates the capability `llm-slot-wait`.
- No panel shows lease counts or queue lengths. They are per-process state, and #815 keeps them out.
- This change fits in one pull request.

## Capabilities

### New Capabilities

- `llm-slot-wait`: the wait of an LLM call for an Ollama slot in the process, as a histogram by priority and outcome, and its dashboard panel.

### Modified Capabilities

None.

## Impact

- Code: `src/devops_cli/ai/client/network.py`, `src/devops_cli/telemetry/instruments.py`, `src/devops_cli/docs/generator.py`.
- Dashboard: `k8s/monitoring/dashboards/ai-spend.json`. Argo CD applies it after the merge.
- Tests: `tests/test_ollama_dynamic_leasing.py`, `tests/test_telemetry_instruments.py`.
- Docs: `docs/TELEMETRY.md`.
- Metric volume: one histogram point for each priority and outcome that occurs in a flush, so at most six series.

## Feasibility

Checked on 2026-10-28 against `origin/main` at bb46660c.

- `acquire_ollama_slot` and `_wait_for_slot` in `src/devops_cli/ai/client/network.py` queue the calls by `RequestPriority` (`high`, `normal`, `as_available`; `src/devops_cli/ai/client/models.py`). `OllamaClient._ollama_messages` and its stream variant (`src/devops_cli/ai/client/ollama.py`) take the slot for each attempt, and `track_ollama_url` takes it for a single URL.
- No metric records this wait. `grep -rn slot_wait src/` finds only `devops_cli_ai_gateway_slot_wait_seconds`, which `_wait_for_gateway_slot` sends for the gateway's `BoundedSemaphore`. That semaphore has no priority, so its histogram has no `priority` label.
- `get_ollama_active_leases` and `get_ollama_waiting_counts` have no caller in `src/` other than the exports of `devops_cli.ai.client`. The issue's statement holds.
- `emit` (`src/devops_cli/telemetry/instruments.py`) sends a histogram as an exponential delta, which Prometheus stores as a native histogram that `histogram_quantile` reads (#792). `tests/test_telemetry_instruments.py` checks every query of `ai-spend.json` against `INSTRUMENTS`.

## Existing Tool Considered

OpenTelemetry's histogram instrument, through the client that devops-cli already uses (`OTelTelemetryClient`), and Grafana's `histogram_quantile` over a Prometheus native histogram cover the need. The change adds one instrument entry, one observation in the slot code and one panel. It builds no tool and deletes no internal code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# llm-slot-wait Specification

## Purpose

The wait of an LLM call for a slot in the process. A call to Ollama waits for a slot on one of its nodes, queued by priority. The wait is a histogram by priority and outcome, and a dashboard panel charts it, so a call that queued in the process is told apart from a slow reply.

## ADDED Requirements

### Requirement: A call to Ollama observes its wait for a slot by priority and outcome
Each call that asks for an Ollama slot SHALL send one observation of the histogram `devops_cli_ai_ollama_slot_wait_seconds`, in seconds, with the label `priority` set to the priority that the call waited with and the label `outcome` set to `ok` when it holds a slot or `timeout` when its deadline passes with no slot.

#### Scenario: A call finds a free slot
- **WHEN** a `high` call asks for a slot on a node with a free slot
- **THEN** one observation is sent with `priority` `high` and `outcome` `ok`

#### Scenario: A call waits for a held slot
- **WHEN** a `normal` call asks for the one slot of a node that another call holds
- **AND** the other call releases the slot while the `normal` call waits
- **THEN** the `normal` call sends one observation with `priority` `normal` and `outcome` `ok`, and a wait above 0 s

#### Scenario: An as-available call finds no free slot
- **WHEN** an `as_available` call asks for the one slot of a node that another call holds
- **THEN** the call fails with an `AIClientError`
- **AND** one observation is sent with `priority` `as_available` and `outcome` `timeout`

### Requirement: The AI spend dashboard charts the p95 Ollama slot wait by priority
`k8s/monitoring/dashboards/ai-spend.json` SHALL have a panel that charts the 0.95 quantile of `devops_cli_ai_ollama_slot_wait_seconds` summed by `priority`, and the count of its observations by `priority`.

#### Scenario: The dashboard queries the slot wait
- **WHEN** a test reads the queries of `ai-spend.json`
- **THEN** one query is the 0.95 `histogram_quantile` of `devops_cli_ai_ollama_slot_wait_seconds` summed by `priority`
- **AND** every query names only series in `INSTRUMENTS`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Tasks

## 1. The Ollama slot wait is a histogram by priority and outcome

- [x] 1.1 Write the failing tests `test_a_call_with_a_free_slot_observes_its_wait_by_priority`, `test_a_call_that_waits_for_a_held_slot_observes_the_wait` and `test_an_as_available_call_with_no_free_slot_observes_a_timeout` in `tests/test_ollama_dynamic_leasing.py`, and see them fail with `uv run devops ci test tests/test_ollama_dynamic_leasing.py`
- [x] 1.2 Add `AI_OLLAMA_SLOT_WAIT` (`devops_cli_ai_ollama_slot_wait_seconds`) to `INSTRUMENTS`, and observe the wait in `acquire_ollama_slot` through `emit`; verify with `uv run devops ci test tests/test_ollama_dynamic_leasing.py tests/test_ai_client_telemetry.py`
- [x] 1.3 Add the Ollama slot wait to the LLM telemetry section of `src/devops_cli/docs/generator.py`, and regenerate the documentation; verify with `uv run devops docs generate --sync-readme` and `uv run devops docs check`

## 2. The AI spend dashboard charts the p95 wait by priority

- [x] 2.1 Add `devops_cli_ai_ollama_slot_wait_seconds` p95 by `priority` to the expected quantiles and groups of `test_the_dashboards_chart_latency_errors_reviews_and_findings_devops_cli_sends` in `tests/test_telemetry_instruments.py`, and see it fail with `uv run devops ci test tests/test_telemetry_instruments.py`
- [x] 2.2 Add the panel "Ollama Slot Wait by Priority (p95)" to `k8s/monitoring/dashboards/ai-spend.json`; verify with `uv run devops ci test tests/test_telemetry_instruments.py tests/test_grafana_dashboards.py tests/test_k8s_monitoring_dashboards.py`
- [x] 2.3 Regenerate the documentation; the dashboard changes no command, option or message; verify with `uv run devops docs generate --sync-readme` and `uv run devops docs check`

## Workflow follow-up

- Pending a person: after the merge, Argo CD applies `ai-spend.json`. Run a command that calls Ollama directly, then confirm that the panel "Ollama Slot Wait by Priority (p95)" shows a series for its priority.
- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
Loading
Loading