Skip to content

Commit

Permalink
Filter returned metrics from api server based on queried name (#732)
Browse files Browse the repository at this point in the history
Signed-off-by: ycabrer <43866176+ycabrer@users.noreply.github.com>
  • Loading branch information
ycabrer authored Apr 8, 2020
1 parent e79249a commit 5d487de
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"strings"

kedav1alpha1 "github.com/kedacore/keda/pkg/apis/keda/v1alpha1"
"github.com/kedacore/keda/pkg/handler"
Expand Down Expand Up @@ -83,16 +84,27 @@ func (p *KedaProvider) GetExternalMetric(namespace string, metricSelector labels
}

for _, scaler := range scalers {
metrics, err := scaler.GetMetrics(context.TODO(), info.Metric, metricSelector)
if err != nil {
logger.Error(err, "error getting metric for scaler", "ScaledObject.Namespace", scaledObject.Namespace, "ScaledObject.Name", scaledObject.Name, "Scaler", scaler)
} else {
matchingMetrics = append(matchingMetrics, metrics...)
metricSpecs := scaler.GetMetricSpecForScaling()

for _, metricSpec := range metricSpecs {
// Filter only the desired metric
if strings.EqualFold(metricSpec.External.MetricName, info.Metric) {
metrics, err := scaler.GetMetrics(context.TODO(), info.Metric, metricSelector)
if err != nil {
logger.Error(err, "error getting metric for scaler", "ScaledObject.Namespace", scaledObject.Namespace, "ScaledObject.Name", scaledObject.Name, "Scaler", scaler)
} else {
matchingMetrics = append(matchingMetrics, metrics...)
}
}
}

scaler.Close()
}

if len(matchingMetrics) <= 0 {
return nil, fmt.Errorf("No matching metrics found for " + info.Metric)
}

return &external_metrics.ExternalMetricValueList{
Items: matchingMetrics,
}, nil
Expand Down

0 comments on commit 5d487de

Please sign in to comment.