Skip to content

feat(k8s): devops k8s logs runs LogQL metric queries #5486

Open

Description

@dan-petty

Problem or Motivation

devops k8s logs --query cannot run a LogQL metric query, such as count_over_time or quantile_over_time:

  • With loki.url set, query_loki in src/devops_cli/k8s/logql.py sends /loki/api/v1/query_range and 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_metrics in 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.md prints 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 --metrics mode on devops k8s logs
  • No reuse of query_loki_metrics or PrometheusQueryResult output
  • 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 --metrics and loki.url set against the fake Loki
  • THEN the fake Loki records a GET to /loki/api/v1/query with the query and no limit parameter

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" and reason="Failed" and values 3 and 1
  • 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.url and the kubectl fallback is selected
  • THEN the command exits with a KubernetesLoggingError whose message names loki.url and 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.md with devops k8s logs --metrics against 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 logs or a sibling command?: Add a --metrics flag to devops k8s logs. (best practice) [sources: src/devops_cli/k8s/logql.py:390, src/devops_cli/models/prometheus.py]
    • Chosen: --metrics flag on devops k8s logs. It reuses the existing query, namespace, loki.url and dry-run handling and keeps one command.
    • Rejected: sibling devops k8s logs metrics command. 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.
  • Does query_loki_metrics already run an instant LogQL metric query and parse each series?: Yes. It calls /loki/api/v1/query for each expression and returns PrometheusQueryResult objects. [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 PrometheusQueryResult and the existing Prometheus query output shape. (best practice) [sources: src/devops_cli/models/prometheus.py, src/devops_cli/k8s/logql.py:390]
    • Chosen: Reuse PrometheusQueryResult labels 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.
  • What should the kubectl fallback do with a metric query?: Refuse it with a KubernetesLoggingError whose message names loki.url and says the fallback cannot run a metric query. (best practice) [sources: src/devops_cli/k8s/logql.py]
    • Chosen: Explicit refusal with a loki.url hint. The fallback has no instant metric data, and the hint is actionable.
    • Rejected: Approximate from kubectl logs. kubectl logs gives lines, not an instant vector with labels and values.
    • Rejected: Return an empty result. An empty result hides the unsupported query.
  • Does a LogQL metric query use the log line limit?: No. query_loki_metrics sends no limit parameter, 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

  1. added
    source/agent
    type/feature
    scope/k8s
    status/new
    value/medium
    effort/low
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    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_metrics already 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)

    • License: AGPL-3.0
    • Last activity: 2026-11-17
    • Maintained: yes

    Covers:

    • Loki's HTTP API answers instant LogQL metric queries such as count_over_time and quantile_over_time
    • A metric query result carries each series' label set and value, which the project's existing query_loki_metrics already parses
    • The project already queries Loki through the loki.url setting

    Lacks:

    • No devops CLI surface: printing each series with its labels and value like devops prometheus query must be added in src/devops_cli/k8s/logql.py and src/devops_cli/commands/
    • The kubectl fallback's refusal message naming loki.url is project code, not something Loki provides
    • No test that a metric query prints series and values, or that a stream query keeps its output

    Tools considered:

    Searches run:

    • Grafana Loki open source log aggregation LogQL count_over_time quantile_over_time, results: 5
    • Loki HTTP API instant query LogQL metric query labels value matrix, results: 5
    • "loki" grafana github repository, results: 5
    • grafana loki api documentation query_range instant query metric queries, results: 5
    • LogQL metric query instant query endpoint returns vector of series with labels, results: 5

    Sources:

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

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

    @devops-cli-service
    Contributor

    Status set to Ready at 2b7f32c (see proposed design in issue body).

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/lowpriority/p2-mediumscope/k8ssource/agentstatus/readytype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions