fix: avoid panic when a metric has only percentile statistics - #1924
Open
pujitha24 wants to merge 1 commit into
Open
fix: avoid panic when a metric has only percentile statistics#1924pujitha24 wants to merge 1 commit into
pujitha24 wants to merge 1 commit into
Conversation
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>
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics, which
configures a metric with only percentile statistics (p50, p90, p99)
and asserts createGetMetricStatisticsInput does not panic.
git stash(reverting thefix) then
go test ./pkg/clients/cloudwatch/... -run TestCreateGetMetricStatisticsInput_OnlyExtendedStatistics -vreproduces 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 poprestored 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