Repository navigation
Description
Problem or Motivation
devops k8s logs --query cannot run a LogQL metric query, such as count_over_time or quantile_over_time:
- With
loki.urlset,query_lokiinsrc/devops_cli/k8s/logql.pysends/loki/api/v1/query_rangeand reads each result as a log stream (stream,values). A metric query returns a matrix, which has no stream lines. - With no
loki.url, the kubectl fallback refuses the query: "The kubectl fallback supports only label="value" matchers and |= filters." query_loki_metricsin the same module runs metric queries, but only the job grade report calls it.
So an agent cannot measure the queries Q9 (image pull time) and Q14 (Kubernetes events) of docs/K8S_PERFORMANCE.md with a devops command. It must use Grafana.
Proposed Solution & Architectural Design
Add an instant metric mode to devops k8s logs, or a sibling command, that calls query_loki_metrics and prints each series with its labels and value, as devops prometheus query does. Refuse a metric query in the kubectl fallback with a message that names loki.url.
Acceptance Criteria
- A test proves that a metric query prints each series with its labels and its value.
- A test proves that a stream query keeps its current output.
- Live proof: Q14 of
docs/K8S_PERFORMANCE.mdprints the count of each event reason.
Target Subsystem
Kubernetes logs (src/devops_cli/k8s/logql.py, src/devops_cli/commands/).
Existing Tool Considered
The Loki HTTP API already gives the instant query, and query_loki_metrics already parses it. No new tool. Cost to build (estimate): 100,000 tokens and 1 hour.
Related
docs/K8S_PERFORMANCE.md (#5444).
Alternatives check of 2026-11-23: in-use.
The repository already uses this tool.
Tool: grafana/loki (https://github.com/grafana/loki), AGPL-3.0, last activity 2026-11-23, maintained.
Reason: Searches found Grafana Loki's own CLI, logcli, runs a LogQL metric query as an instant query and prints each series with its labels and value, so Q9 and Q14 can be measured without Grafana; it adds no devops k8s logs mode.
Covers:
- Runs a LogQL metric query as an instant query from a terminal
- Prints each returned series with its label set and value
- Reaches the configured Loki HTTP API with no Grafana UI
Lacks:
- No
--metricsmode ondevops k8s logs - No reuse of
query_loki_metricsorPrometheusQueryResultoutput - Kubectl fallback still refuses a metric query without naming
loki.url
Proposed design
Problem statement
Add a --metrics mode to devops k8s logs so an agent can run a LogQL metric query from the CLI. devops k8s logs --query cannot run a LogQL metric query. With loki.url set, it reads only log streams. With no loki.url, the kubectl fallback refuses the query. An agent cannot measure Q9 and Q14 of docs/K8S_PERFORMANCE.md with a devops command, so it must use Grafana.
Acceptance criteria
Requirement: Metric query uses instant endpoint
devops k8s logs --metrics SHALL send each LogQL metric query to /loki/api/v1/query as an instant query.
Scenario: Instant query request
- WHEN a metric query runs with
--metricsandloki.urlset against the fake Loki - THEN the fake Loki records a GET to
/loki/api/v1/querywith the query and nolimitparameter
Requirement: Metric series output
The metric mode SHALL print each returned series with its label set and its value.
Scenario: Two metric series
- WHEN the fake Loki returns two vector series with labels
reason="Pulled"andreason="Failed"and values3and1 - THEN the command prints one row for each series with its labels and value
Requirement: Stream query output
A stream query SHALL keep its current output when --metrics is absent.
Scenario: Stream query unchanged
- WHEN a stream query runs against the fake Loki and returns log lines
- THEN the command prints the same entries in the same order as before the change
Requirement: Fallback refusal
The kubectl fallback MUST refuse a metric query with an error that names loki.url.
Scenario: Metric query with no Loki URL
- WHEN a metric query runs with no
loki.urland the kubectl fallback is selected - THEN the command exits with a
KubernetesLoggingErrorwhose message namesloki.urland says the fallback cannot run a metric query
Requirement: Q14 event reason counts
The metric mode SHALL show the count of each Kubernetes event reason for Q14 of docs/K8S_PERFORMANCE.md.
Scenario: Q14 live proof
- WHEN the live check runs the Q14 query of
docs/K8S_PERFORMANCE.mdwithdevops k8s logs --metricsagainst the configured Loki - THEN the command prints one row for each event reason with its count
Existing Tool Considered
grafana/loki (in-use): the alternatives check of 2026-11-17 in the issue body records the verdict and lists what the tool covers and lacks. The proposal adds the missing CLI surface. A --metrics mode on devops k8s logs calls query_loki_metrics and prints each series with its labels and value. The kubectl fallback refuses a metric query with a message that names loki.url.
Key questions
- Should the metric mode be a flag on
devops k8s logsor a sibling command?: Add a--metricsflag todevops k8s logs. (best practice) [sources: src/devops_cli/k8s/logql.py:390, src/devops_cli/models/prometheus.py]- Chosen:
--metricsflag ondevops k8s logs. It reuses the existing query, namespace,loki.urland dry-run handling and keeps one command. - Rejected: sibling
devops k8s logs metricscommand. It duplicates option and documentation work for one mode. - Rejected: auto-detect a metric query. A parser that tells metric from stream expressions is brittle, so an explicit flag is clear.
- Chosen:
- Does
query_loki_metricsalready run an instant LogQL metric query and parse each series?: Yes. It calls/loki/api/v1/queryfor each expression and returnsPrometheusQueryResultobjects. [sources: src/devops_cli/k8s/logql.py:390, src/devops_cli/models/prometheus.py] - How should the command print a metric series?: Print each series with its label set and value. Reuse
PrometheusQueryResultand the existing Prometheus query output shape. (best practice) [sources: src/devops_cli/models/prometheus.py, src/devops_cli/k8s/logql.py:390]- Chosen: Reuse
PrometheusQueryResultlabels and value. The result type already holds each series, and the issue asks for this output. - Rejected: Print raw Loki JSON. It exposes transport detail and is not the query output.
- Rejected: Print only values. It loses the label set that identifies each series.
- Chosen: Reuse
- What should the kubectl fallback do with a metric query?: Refuse it with a
KubernetesLoggingErrorwhose message namesloki.urland says the fallback cannot run a metric query. (best practice) [sources: src/devops_cli/k8s/logql.py]- Chosen: Explicit refusal with a
loki.urlhint. The fallback has no instant metric data, and the hint is actionable. - Rejected: Approximate from
kubectl logs.kubectl logsgives lines, not an instant vector with labels and values. - Rejected: Return an empty result. An empty result hides the unsupported query.
- Chosen: Explicit refusal with a
- Does a LogQL metric query use the log line limit?: No.
query_loki_metricssends nolimitparameter, and a metric query counts lines in Loki. [sources: src/devops_cli/k8s/logql.py:390, docs/guidelines/agentic-jobs.md:97]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 13, 2026
Both target the devops k8s logs command in src/devops_cli/k8s/logql.py, but #89 delivered the stream query engine while the candidate adds metric query support to the same command.devops-cli-service commentedon Nov 17, 2026
Alternatives check: in-use
Reason: The web searches answered with unrelated pages, so the check rests on the issue text and the read facts of grafana/loki (open, actively pushed, latest release v3.7.10). Loki itself already serves instant LogQL metric queries and returns each series with its labels and value, which is exactly the capability the issue needs — the issue says as much and notes
query_loki_metricsalready parses that response; only the CLI wiring (printing and the fallback refusal) is missing.The repository already uses this tool.
Tool: grafana/loki (https://github.com/grafana/loki)
Covers:
Lacks:
devops prometheus querymust be added in src/devops_cli/k8s/logql.py and src/devops_cli/commands/Tools considered:
Searches run:
Sources:
Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 18, 2026
Status set to Ready at 2b7f32c (see proposed design in issue body).