diff --git a/pkg/util/kubernetes/autoscalers/processor.go b/pkg/util/kubernetes/autoscalers/processor.go index 3a3bfdb0d81097..9107e9177e59c9 100644 --- a/pkg/util/kubernetes/autoscalers/processor.go +++ b/pkg/util/kubernetes/autoscalers/processor.go @@ -122,10 +122,14 @@ func (p *Processor) UpdateExternalMetrics(emList map[string]custommetrics.Extern var err error updated = make(map[string]custommetrics.ExternalMetricValue) - batch := []string{} + uniqueQueries := make(map[string]struct{}, len(emList)) + batch := make([]string, 0, len(emList)) for _, e := range emList { q := getKey(e.MetricName, e.Labels, aggregator, rollup) - batch = append(batch, q) + if _, found := uniqueQueries[q]; !found { + uniqueQueries[q] = struct{}{} + batch = append(batch, q) + } } metrics, err := p.QueryExternalMetric(batch) diff --git a/pkg/util/kubernetes/autoscalers/processor_test.go b/pkg/util/kubernetes/autoscalers/processor_test.go index 776fa9953cbca4..a0e196cee2439b 100644 --- a/pkg/util/kubernetes/autoscalers/processor_test.go +++ b/pkg/util/kubernetes/autoscalers/processor_test.go @@ -107,6 +107,46 @@ func TestProcessor_UpdateExternalMetrics(t *testing.T) { }, }, }, + { + "perform unique from list of input externalMetrics", + map[string]custommetrics.ExternalMetricValue{ + "id1": { + MetricName: metricName, + Labels: map[string]string{"foo": "bar"}, + Valid: false, + }, + "id2": { + MetricName: metricName, + Labels: map[string]string{"foo": "bar"}, + Valid: false, + }, + }, + []datadog.Series{ + { + Metric: &metricName, + Points: []datadog.DataPoint{ + makePoints(1531492452000, 12), + makePoints(penTime, 14), // Force the penultimate point to be considered fresh at all time(< externalMaxAge) + makePoints(0, 27), + }, + Scope: makePtr("foo:bar"), + }, + }, + map[string]custommetrics.ExternalMetricValue{ + "id1": { + MetricName: "requests_per_s", + Labels: map[string]string{"foo": "bar"}, + Value: 14, + Valid: true, + }, + "id2": { + MetricName: "requests_per_s", + Labels: map[string]string{"foo": "bar"}, + Value: 14, + Valid: true, + }, + }, + }, { "do not update valid sparse metric", map[string]custommetrics.ExternalMetricValue{ diff --git a/releasenotes-dca/notes/fix-duplicate-queries-external-metrics-a9489dd3eb4d2ca8.yaml b/releasenotes-dca/notes/fix-duplicate-queries-external-metrics-a9489dd3eb4d2ca8.yaml new file mode 100644 index 00000000000000..94c116f3fab770 --- /dev/null +++ b/releasenotes-dca/notes/fix-duplicate-queries-external-metrics-a9489dd3eb4d2ca8.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fix issue in Cluster Agent when using external metrics without DatadogMetrics where multiple HPAs using the same metricName + Labels would prevent all HPAs (except 1st one) to get values from Datadog