Skip to content

Commit b868224

Browse files
committed
relates to discussion kedacore#6565 (comment)
the final semaphore acquisition was still missing, which resulted in loop not waiting until all metrics were gathered from each metrics API server Signed-off-by: julian GUINARD <[email protected]>
1 parent 68d5637 commit b868224

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/scalers/metrics_api_scaler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ func getValueFromPrometheusResponse(body []byte, valueLocation string) (float64,
224224
// getValueFromJSONResponse uses provided valueLocation to access the numeric value in provided body using GJSON
225225
func getValueFromJSONResponse(body []byte, valueLocation string) (float64, error) {
226226
r := gjson.GetBytes(body, valueLocation)
227+
227228
if r.Type == gjson.String {
228229
v, err := resource.ParseQuantity(r.String())
229230
if err != nil {
@@ -400,6 +401,7 @@ func (s *metricsAPIScaler) aggregateMetricsFromMultipleEndpoints(ctx context.Con
400401
var err error
401402
var firstMetricEncountered bool
402403
var aggregation float64
404+
s.logger.V(1).Info(fmt.Sprintf("start iterating %d endpoint urls", len(endpointsUrls)))
403405
for _, endpointURL := range endpointsUrls {
404406
if err := sem.Acquire(ctx, 1); err != nil {
405407
s.logger.Error(err, "Failed to acquire semaphore")
@@ -437,6 +439,11 @@ func (s *metricsAPIScaler) aggregateMetricsFromMultipleEndpoints(ctx context.Con
437439
}
438440
}(endpointURL)
439441
}
442+
// Wait that all goroutines have released their semaphore acquisition
443+
if err := sem.Acquire(ctx, maxGoroutines); err != nil {
444+
s.logger.V(1).Error(err, "final semaphore acquisition failed")
445+
}
446+
s.logger.V(1).Info(fmt.Sprintf("ENDED iterating %d endpoint urls, aggregation is now %v", len(endpointsUrls), aggregation))
440447

441448
if nbErrors > 0 && nbErrors == len(endpointsUrls) {
442449
err = fmt.Errorf("could not get any metric successfully from the %d provided endpoints", len(endpointsUrls))
@@ -470,6 +477,7 @@ func (s *metricsAPIScaler) getMetricValueFromURL(ctx context.Context, url *strin
470477
return 0, err
471478
}
472479
v, err := GetValueFromResponse(b, s.metadata.ValueLocation, s.metadata.Format)
480+
473481
if err != nil {
474482
return 0, err
475483
}

0 commit comments

Comments
 (0)