@@ -24,6 +24,7 @@ import (
2424 "bytes"
2525 "context"
2626 "fmt"
27+ "strings"
2728 "text/template"
2829 "time"
2930
@@ -83,11 +84,12 @@ func NewPrometheusClient(parentLogger logger.Logger, prometheusURL, namespace st
8384 }, nil
8485}
8586
86- // renderQuery renders the Prometheus query template with the current namespace, window size, and additional parameters
87- func (pc * PrometheusClient ) renderQuery (queryTemplate * template.Template , windowSize string ) (string , error ) {
87+ // renderQuery renders the Prometheus query template
88+ func (pc * PrometheusClient ) renderQuery (queryTemplate * template.Template , windowSize , resourceNameRegex string ) (string , error ) {
8889 templateData := make (map [string ]string )
8990 templateData ["Namespace" ] = pc .namespace
9091 templateData ["WindowSize" ] = windowSize
92+ templateData ["Resources" ] = resourceNameRegex
9193
9294 var queryBuffer bytes.Buffer
9395 if err := queryTemplate .Execute (& queryBuffer , templateData ); err != nil {
@@ -111,6 +113,15 @@ func (pc *PrometheusClient) extractWindowSizesForMetric(resources []scalertypes.
111113 return windowSizes
112114}
113115
116+ // buildResourceNameRegex creates a pipe-separated regex pattern from resource names for Prometheus query filtering (e.g., "func1|func2|func3")
117+ func (pc * PrometheusClient ) buildResourceNameRegex (resources []scalertypes.Resource ) string {
118+ resourceNames := make ([]string , len (resources ))
119+ for i , resource := range resources {
120+ resourceNames [i ] = resource .Name
121+ }
122+ return strings .Join (resourceNames , "|" )
123+ }
124+
114125// resolveFullMetricName finds the matching ScaleResource for a given metric name and window size,
115126// and returns the full metric name (e.g., "metric_name_per_1m").
116127func (pc * PrometheusClient ) resolveFullMetricName (resources []scalertypes.Resource , metricName , windowSize string ) (string , error ) {
@@ -139,6 +150,8 @@ func (pc *PrometheusClient) GetResourceMetrics(resources []scalertypes.Resource)
139150 continue
140151 }
141152
153+ resourceNameRegex := pc .buildResourceNameRegex (resources )
154+
142155 for windowSize := range windowSizes {
143156 fullMetricName , err := pc .resolveFullMetricName (resources , metricName , windowSize )
144157 if err != nil {
@@ -149,7 +162,7 @@ func (pc *PrometheusClient) GetResourceMetrics(resources []scalertypes.Resource)
149162 continue
150163 }
151164
152- query , err := pc .renderQuery (queryTemplate , windowSize )
165+ query , err := pc .renderQuery (queryTemplate , windowSize , resourceNameRegex )
153166 if err != nil {
154167 return nil , errors .Wrapf (err , "Failed to render query for metricName=%s, windowSize=%s" , metricName , windowSize )
155168 }
0 commit comments