Skip to content

Commit 72e6296

Browse files
Metrics API: Fix text format parsing error (#6640)
* Metrics API: Fix text format parsing error resulting in unexpected end of input stream Signed-off-by: rickbrouwer <[email protected]> * Update an existing test to cover the empty new line case Signed-off-by: Jorge Turrado <[email protected]> --------- Signed-off-by: rickbrouwer <[email protected]> Signed-off-by: Jorge Turrado <[email protected]> Co-authored-by: Jorge Turrado <[email protected]>
1 parent b4c5bab commit 72e6296

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Here is an overview of all new **experimental** features:
9999
- **AWS Scalers**: Add AWS region to the AWS Config Cache key ([#6128](https://github.com/kedacore/keda/issues/6128))
100100
- **External Scaler**: Support server TLS without custom CA ([#6606](https://github.com/kedacore/keda/pull/6606))
101101
- **GCP Storage**: GCP Storage scaler ignores folders ([#6531](https://github.com/kedacore/keda/issues/6531))
102+
- **Metrics API**: Fix text format parsing error resulting in unexpected end of input stream ([#6559](https://github.com/kedacore/keda/issues/6559))
102103
- **NATS JetStream**: Support for looking up account using an ID ([#6611](https://github.com/kedacore/keda/pull/6611))
103104
- **Redis Streams**: Allow default value of 0 for activationLagCount ([#6478](https://github.com/kedacore/keda/issues/6478))
104105
- **Selenium Grid**: Scaler logic on platformName is set empty or `any` ([#6477](https://github.com/kedacore/keda/issues/6477))

pkg/scalers/metrics_api_scaler.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,22 @@ func getValueFromPrometheusResponse(body []byte, valueLocation string) (float64,
274274
metricName = v.Value
275275
}
276276
}
277+
277278
// Ensure EOL
278-
reader := strings.NewReader(strings.ReplaceAll(string(body), "\r\n", "\n"))
279+
bodyStr := strings.ReplaceAll(string(body), "\r\n", "\n")
280+
281+
// Check if newline is present
282+
if len(bodyStr) > 0 && !strings.HasSuffix(bodyStr, "\n") {
283+
bodyStr += "\n"
284+
}
285+
286+
reader := strings.NewReader(bodyStr)
279287
familiesParser := expfmt.TextParser{}
280288
families, err := familiesParser.TextToMetricFamilies(reader)
281289
if err != nil {
282-
return 0, err
290+
return 0, fmt.Errorf("prometheus format parsing error: %w", err)
283291
}
292+
284293
family, ok := families[metricName]
285294
if !ok {
286295
return 0, fmt.Errorf("metric '%s' not found", metricName)

pkg/scalers/metrics_api_scaler_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ func TestGetValueFromResponse(t *testing.T) {
133133
backend_queue_size{queueName="two", instance="zero"} 20
134134
# HELP random_metric Random metric generate to include noise
135135
# TYPE random_metric counter
136-
random_metric 10
137-
`)
136+
random_metric 10`)
137+
// Ending the file without new line is intended to verify
138+
// https://github.com/kedacore/keda/issues/6559
138139

139140
testCases := []struct {
140141
name string

0 commit comments

Comments
 (0)