Skip to content

fix: avoid panic when a metric has only percentile statistics - #1924

Open
pujitha24 wants to merge 1 commit into
prometheus-community:masterfrom
pujitha24:auto/issue-1702
Open

fix: avoid panic when a metric has only percentile statistics#1924
pujitha24 wants to merge 1 commit into
prometheus-community:masterfrom
pujitha24:auto/issue-1702

Conversation

@pujitha24

Copy link
Copy Markdown

Motivation:
createGetMetricStatisticsInput built a debug CLI-helper string by
unconditionally indexing statistics[0]. metric.Statistics entries are
split into classic statistics (Average, Sum, ...) and extendedStatistics
(percentiles such as p50/p90/p99, matched via promutil.Percentile).
When a metric's Statistics list contains only percentiles, the classic
statistics slice stays empty, so statistics[0] panics with
"index out of range [0] with length 0" and crashes the whole process.
This matches the report: a static-job metric (IngestionToInvocationStartLatency)
configured with statistics: [p50, p90, p99] triggers the panic on every
scrape.

Approach:
Guard the index access: only read statistics[0] for the debug log line
when len(statistics) > 0, otherwise use an empty string. The real
GetMetricStatisticsInput fields (Statistics, ExtendedStatistics) were
already built correctly beforehand and are unaffected; only the debug
log string construction was unsafe.

Validation:

  • Added pkg/clients/cloudwatch/input_test.go with
    TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics, which
    configures a metric with only percentile statistics (p50, p90, p99)
    and asserts createGetMetricStatisticsInput does not panic.
  • Confirmed the test fails before the fix: git stash (reverting the
    fix) then go test ./pkg/clients/cloudwatch/... -run TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics -v
    reproduces the exact panic
    ("runtime error: index out of range [0] with length 0" at
    input.go:75), matching the crash reported in the issue.
  • git stash pop restored the fix; the same test then passes.
  • go build ./... succeeds.
  • go test ./... (full suite) passes.
  • golangci-lint run ./pkg/clients/cloudwatch/... reports 0 issues.

Report: #1702
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com

Fixes #1702

Motivation:
createGetMetricStatisticsInput built a debug CLI-helper string by
unconditionally indexing statistics[0]. metric.Statistics entries are
split into classic statistics (Average, Sum, ...) and extendedStatistics
(percentiles such as p50/p90/p99, matched via promutil.Percentile).
When a metric's Statistics list contains only percentiles, the classic
statistics slice stays empty, so statistics[0] panics with
"index out of range [0] with length 0" and crashes the whole process.
This matches the report: a static-job metric (IngestionToInvocationStartLatency)
configured with statistics: [p50, p90, p99] triggers the panic on every
scrape.

Approach:
Guard the index access: only read statistics[0] for the debug log line
when len(statistics) > 0, otherwise use an empty string. The real
GetMetricStatisticsInput fields (Statistics, ExtendedStatistics) were
already built correctly beforehand and are unaffected; only the debug
log string construction was unsafe.

Validation:
- Added pkg/clients/cloudwatch/input_test.go with
  TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics, which
  configures a metric with only percentile statistics (p50, p90, p99)
  and asserts createGetMetricStatisticsInput does not panic.
- Confirmed the test fails before the fix: `git stash` (reverting the
  fix) then `go test ./pkg/clients/cloudwatch/... -run
  TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics -v`
  reproduces the exact panic
  ("runtime error: index out of range [0] with length 0" at
  input.go:75), matching the crash reported in the issue.
- `git stash pop` restored the fix; the same test then passes.
- `go build ./...` succeeds.
- `go test ./...` (full suite) passes.
- `golangci-lint run ./pkg/clients/cloudwatch/...` reports 0 issues.

Report: prometheus-community#1702
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
@pujitha24

Copy link
Copy Markdown
Author

Just checking in on this one — it's still green and rebased on main; happy to make any changes if something would help move review along.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Yace 0.62.1: panic: runtime error: index out of range [0] with length 0

1 participant