Skip to content

fix(telemetry): avoid 500-series Loki error on 7d eval grades #7064

Done

Description

@dan-petty

Problem Description

uv run devops roadmap eval grades --window 7d without --repo fails with Loki HTTP 400 (maximum number of series (500) reached for a single query).
Query 1 (GradeQueryKind.COUNTS: sum by (roadmap_job, repo, version, success, infra) (count_over_time(... [7d]))) exceeds Loki's 500-series per-query limit because count_over_time evaluates over all unaggregated extracted and stream labels across 7 days before sum by (...) runs.
Adding --repo dan-petty/devops-cli or using --window 48h stays below 500 series, while an unfiltered 7-day report across all repositories fails.

Reproduction Steps

  1. Port-forward Loki to local port 13104 (or set loki.url).
  2. Run uv run devops roadmap eval grades --window 7d --loki-url http://127.0.0.1:13104.

Expected vs Actual Behavior

Expected: devops roadmap eval grades --window 7d returns the 7-day grade table across all managed repositories.

Actual: Loki rejects Query 1 (COUNTS over [7d]) with HTTP 400 Bad Request because the internal series count before aggregation passes 500.

Environment Diagnostics

  • Query builder: src/devops_cli/roadmap/job_grade_report.py:337-379 (_lines, _query)
  • Verified against live cluster Loki on 2026-11-25 (first_passes and drafted succeeded; counts failed with 500-series error).

Logs, Traces, or Terminal Output

✗ Loki failed after 1 attempt in 0.9 s: HTTP 400 Bad Request: maximum number of series (500) reached for a single query; consider reducing query cardinality by adding more specific stream selectors, reducing the time range, or aggregating results with functions like sum(), count() or topk(). The service refused the request: correct the request, then try again.

Proposed design

Problem statement

Keep only the labels that each grade query uses, and split a count window when Loki still rejects it. devops roadmap eval grades --window 7d fails without --repo. Loki answers the COUNTS query with HTTP 400 and the 500-series limit. The query passes every extracted label to count_over_time, so a 7-day range holds more series than the limit. The fix removes the unused labels at the source and adds a bounded window split for the count queries.

Acceptance criteria

Requirement: Counts query label set

The COUNTS grade query MUST pass only the labels that its aggregation names to the range aggregation.

Scenario: Seven-day counts query

  • WHEN a test builds the COUNTS query for the 7d window with no repo
  • THEN the expression has a keep stage before [7d] whose labels equal the sum by labels, and no other extracted label reaches the range aggregation.

Requirement: Classes query label set

The CLASSES grade query MUST pass only the labels that its aggregation and its filters name to the range aggregation.

Scenario: Seven-day classes query

  • WHEN a test builds the CLASSES query for the 7d window
  • THEN the keep labels equal the sum by labels plus success and infra, and the success="false" and infra="false" filters stay.

Requirement: Drafted query label set

The DRAFTED grade query MUST pass only the labels that its aggregation names to the range aggregation.

Scenario: Seven-day drafted query

  • WHEN a test builds the DRAFTED query for the 7d window
  • THEN the keep labels equal the sum by labels, and the roadmap_job, result and infra filters stay.

Requirement: First-passes query label set

The FIRST_PASSES grade query MUST pass only the labels that its aggregation names to the range aggregation.

Scenario: Seven-day first-passes query

  • WHEN a test builds the FIRST_PASSES query for the 7d window
  • THEN the keep labels equal the sum by labels, and the success and infra filters stay.

Requirement: Efficiency query label set

Each efficiency grade query MUST pass only the labels that its template aggregates by, plus the label that it unwraps, to the range aggregation.

Scenario: Seven-day efficiency query

  • WHEN a test builds each efficiency query for the 7d window
  • THEN the keep labels equal the template's sum by labels plus the unwrapped count label.

Requirement: Split on the series limit

A count grade query MUST split its window in half and sum the parts when Loki rejects it for the series limit.

Scenario: Counts query on a store over the series limit

  • WHEN the fake Loki answers the [7d] COUNTS query with HTTP 400 and the maximum number of series message, and answers each [84h] part
  • THEN the report sends two queries and its count for each job and repo equals the sum of the two parts.

Requirement: No split on another 400

The report MUST NOT split a query when Loki answers HTTP 400 with a message that does not name the series limit.

Scenario: A query with a syntax error

  • WHEN the fake Loki answers HTTP 400 with parse error
  • THEN the report sends one query and returns the Loki error with its status.

Existing Tool Considered

No alternatives check has run. Grafana Loki's LogQL keep label filter covers the need: it drops extracted labels before the range aggregation, and the project already uses the sibling drop operator in k8s/devops/dashboards/roadmap-service-telemetry.json. The operator lacks control of the result series count, so the report also splits a count window when Loki rejects it for the series limit. The change adds one helper and one clause to each query in src/devops_cli/roadmap/job_grade_report.py; it builds no library.

Key questions

  • Which change lowers the series count of the 7-day grade queries?: Keep only the labels that each query aggregates or filters on, with LogQL keep. Add a bounded window split for the count queries as a fallback. Do not raise the cluster limit. (best practice) [sources: src/devops_cli/roadmap/job_grade_report.py:349, k8s/devops/dashboards/roadmap-service-telemetry.json:1317]
    • Chosen: Keep only the used labels. It removes the unused labels from the series identity at the source, and it adds one clause to each query.
    • Rejected: Raise max_query_series in the Loki configuration. It changes a shared cluster limit, costs memory, and hides the next cardinality regression.
    • Rejected: Split every window into fixed 48h parts. It sends 45 requests for a 90-day trend before any error, and it cannot merge the average queries.
    • Rejected: Remove version from the COUNTS group-by. It changes the report output and needs a second query for the version set.
  • Does the deployed Loki support the keep operator?: Yes. The dashboard already uses drop, and the LogQL reference documents keep in the same label-filter section. The deployed Loki serves the dashboard query, so it has both operators. [sources: k8s/devops/dashboards/roadmap-service-telemetry.json:1317]
  • Does the keep clause change the counts that the report shows?: No. Each query aggregates or filters only on the labels that it keeps. A dropped label never changes a count, a class or a first-pass value. [sources: src/devops_cli/roadmap/job_grade_report.py:349]
  • How does the report detect the series-limit error?: Match HTTP 400 and the substring maximum number of series in the Loki message. Split only on that error. (best practice) [sources: src/devops_cli/roadmap/job_grade_report.py:349, tests/support/fakes/loki.py]
    • Chosen: Match the status and the message. It splits only the series-limit error and lets every other 400 reach the user.
    • Rejected: Split on every HTTP 400. It hides a syntax or selector error and multiplies requests.
    • Rejected: Split every count query. It sends needless requests when the query is under the limit.
  • How does the report split a window that exceeds the series limit?: Halve only the window that Loki rejected, and retry each half. Sum the count results. Stop at a 1h floor. (best practice) [sources: src/devops_cli/roadmap/job_grade_report.py:349]
    • Chosen: Halve the rejected window, then recurse. It sends the fewest requests, and 48h is known to pass, so one or two levels usually suffice.
    • Rejected: Split every window into fixed 48h parts. It sends 45 requests for a 90-day trend before any error.
    • Rejected: Do not split. A result over 500 series still fails after the label fix.
  • Can the split be tested offline?: Yes. tests/support/fakes/loki.py answers LogQL metric queries, so a test makes it reject the full window and answer each part. [sources: tests/support/fakes/loki.py]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-25: in-use.
The repository already uses this tool.
Tool: grafana/loki (https://github.com/grafana/loki), AGPL-3.0, last activity 2026-11-25, maintained.
Reason: Searches found no separate library; the Loki the project already queries offers the LogQL keep filter, which drops unused labels before the range aggregation, but it cannot bound result series, so the report still needs its own window split.

Covers:

  • keep drops extracted labels before count_over_time, shrinking the series identity
  • The sibling drop operator already serves the project's dashboard queries
  • sum by over the kept labels returns the same counts

Lacks:

  • No per-query cap on result series count
  • No client-side halving and summing of a rejected window
  • No merge of average queries across split parts

Activity

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

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

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (8)

    Tools considered:

    Searches run:

    • LogQL keep label filter reduce series cardinality before range aggregation, results: 5
    • Loki "maximum number of series" 500 per query reduce cardinality keep labels, results: 5
    • grafana loki LogQL label filter keep drop operators documentation, results: 5
    • loki query "maximum number of series" limit github issue, results: 5
    • grafana.com docs loki logql label filters keep, results: 5
    • LogQL keep label filter example "keep" count_over_time, results: 5
    • Loki query sharding split long range queries automatically, results: 5
    • Grafana Loki documentation LogQL "label filter expression" keep and drop, results: 5

    Sources:

  6. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 25, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 25, 2026
  8. devops-cli-service commented on Nov 25, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-25 06:46 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