@@ -24,6 +24,7 @@ import (
2424 "bytes"
2525 "context"
2626 "fmt"
27+ "strings"
2728 "text/template"
2829 "time"
2930
@@ -80,11 +81,12 @@ func NewPrometheusClient(parentLogger logger.Logger, prometheusURL, namespace st
8081 }, nil
8182}
8283
83- // renderQuery renders the Prometheus query template with the current namespace, window size, and additional parameters
84- func (pc * PrometheusClient ) renderQuery (queryTemplate * template.Template , windowSize string ) (string , error ) {
84+ // renderQuery renders the Prometheus query template
85+ func (pc * PrometheusClient ) renderQuery (queryTemplate * template.Template , windowSize , resourceNameRegex string ) (string , error ) {
8586 templateData := make (map [string ]string )
8687 templateData ["Namespace" ] = pc .namespace
8788 templateData ["WindowSize" ] = windowSize
89+ templateData ["Resources" ] = resourceNameRegex
8890
8991 var queryBuffer bytes.Buffer
9092 if err := queryTemplate .Execute (& queryBuffer , templateData ); err != nil {
@@ -108,6 +110,15 @@ func (pc *PrometheusClient) extractWindowSizesForMetric(resources []scalertypes.
108110 return windowSizes
109111}
110112
113+ // buildResourceNameRegex creates a pipe-separated regex pattern from resource names for Prometheus query filtering (e.g., "func1|func2|func3")
114+ func (pc * PrometheusClient ) buildResourceNameRegex (resources []scalertypes.Resource ) string {
115+ resourceNames := make ([]string , len (resources ))
116+ for i , resource := range resources {
117+ resourceNames [i ] = resource .Name
118+ }
119+ return strings .Join (resourceNames , "|" )
120+ }
121+
111122// resolveFullMetricName finds the matching ScaleResource for a given metric name and window size,
112123// and returns the full metric name (e.g., "metric_name_per_1m").
113124func (pc * PrometheusClient ) resolveFullMetricName (resources []scalertypes.Resource , metricName , windowSize string ) (string , error ) {
@@ -136,6 +147,8 @@ func (pc *PrometheusClient) GetResourceMetrics(resources []scalertypes.Resource)
136147 continue
137148 }
138149
150+ resourceNameRegex := pc .buildResourceNameRegex (resources )
151+
139152 for windowSize := range windowSizes {
140153 fullMetricName , err := pc .resolveFullMetricName (resources , metricName , windowSize )
141154 if err != nil {
@@ -146,7 +159,7 @@ func (pc *PrometheusClient) GetResourceMetrics(resources []scalertypes.Resource)
146159 continue
147160 }
148161
149- query , err := pc .renderQuery (queryTemplate , windowSize )
162+ query , err := pc .renderQuery (queryTemplate , windowSize , resourceNameRegex )
150163 if err != nil {
151164 return nil , errors .Wrapf (err , "Failed to render query for metricName=%s, windowSize=%s" , metricName , windowSize )
152165 }
0 commit comments