Skip to content

Fix panic when topk() reduces series below maxSeries in QueryRange - #6010

Merged
mdisibio merged 3 commits into
grafana:mainfrom
Syedowais312:fix/metrics-topk-maxseries-panic-clean
Dec 1, 2025
Merged

Fix panic when topk() reduces series below maxSeries in QueryRange#6010
mdisibio merged 3 commits into
grafana:mainfrom
Syedowais312:fix/metrics-topk-maxseries-panic-clean

Conversation

@Syedowais312

@Syedowais312 Syedowais312 commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

Fixes #6000

Summary

Fixes a crash in QueryRange when the internal series limit is reached while using topk() or bottomk().
These functions reduce the series count before truncation, causing an out-of-bounds slice.

Root Cause

MaxSeriesReached() triggers truncation using:

resp.Series = resp.Series[:maxSeries]

But after topk(10), the response may contain fewer series than maxSeries, leading to:

panic: slice bounds out of range

Fix

Added a conditional check:

if len(resp.Series) > maxSeries {
    resp.Series = resp.Series[:maxSeries]
}

This prevents slicing beyond available series.

Tests

  • Verified build: go build ./pkg/traceql
  • Ran combiner tests: go test ./modules/frontend/combiner
  • Tests pass.
@mdisibio

mdisibio commented Dec 1, 2025

Copy link
Copy Markdown
Contributor

Thanks for taking care of this. Fix looks good, can you add a changelog.md entry? And we'll merge.

@Syedowais312

Copy link
Copy Markdown
Contributor Author

Thanks for taking care of this. Fix looks good, can you add a changelog.md entry? And we'll merge.

Changelog entry added. Let me know if anything else is needed!
Thank you for the review!

Comment thread CHANGELOG.md Outdated
@mdisibio
mdisibio enabled auto-merge (squash) December 1, 2025 13:08
@mdisibio
mdisibio merged commit 1544bd5 into grafana:main Dec 1, 2025
36 of 37 checks passed
mattdurham pushed a commit to mattdurham/tempo that referenced this pull request Jun 18, 2026
…rafana#6010)

* fix(query-range): prevent slice panic when truncating series after topk()

* docs: Update CHANGELOG.md for metrics-topk-maxseries fix

* Update CHANGELOG.md

---------

Co-authored-by: Martin Disibio <mdisibio@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants