Skip to content

Commit 33635de

Browse files
BenjaminBraunDevkfswain
authored andcommitted
Add nil option for metric_spec to specify metrics to not be scraped. (kubernetes-sigs#503)
* Add nil option for metric_spec to specify metrics to not be scraped. * Add logging when a metric is not being scraped when set as an empty string. * Move unscraped metric setup logging to main. * Update Dockerfile go version from 1.23 to 1.24
1 parent b9ffb7b commit 33635de

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

cmd/epp/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func run() error {
163163
setupLog.Error(err, "Failed to create metric mapping from flags.")
164164
return err
165165
}
166+
verifyMetricMapping(*mapping, setupLog)
166167

167168
pmf := backendmetrics.NewPodMetricsFactory(&backendmetrics.PodMetricsClientImpl{MetricMapping: mapping}, *refreshMetricsInterval)
168169
// Setup runner.
@@ -304,3 +305,16 @@ func validateFlags() error {
304305

305306
return nil
306307
}
308+
309+
func verifyMetricMapping(mapping backendmetrics.MetricMapping, logger logr.Logger) {
310+
if mapping.TotalQueuedRequests == nil {
311+
logger.Info("Not scraping metric: TotalQueuedRequests")
312+
}
313+
if mapping.KVCacheUtilization == nil {
314+
logger.Info("Not scraping metric: KVCacheUtilization")
315+
}
316+
if mapping.LoraRequestInfo == nil {
317+
logger.Info("Not scraping metric: LoraRequestInfo")
318+
}
319+
320+
}

pkg/epp/backend/metrics/metrics_spec.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type MetricMapping struct {
4141
// "metric_name{label1=value1}"
4242
// "metric_name{label1=value1,label2=value2}"
4343
func stringToMetricSpec(specStr string) (*MetricSpec, error) {
44+
if specStr == "" {
45+
return nil, nil // Allow empty strings to represent nil MetricSpecs
46+
}
4447
specStr = strings.TrimSpace(specStr)
4548
metricName := specStr
4649
labels := make(map[string]string)

pkg/epp/backend/metrics/metrics_spec_test.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestStringToMetricSpec(t *testing.T) {
3232
name: "empty string",
3333
input: "",
3434
want: nil,
35-
wantErr: true,
35+
wantErr: false,
3636
},
3737
{
3838
name: "no labels",
@@ -152,21 +152,18 @@ func TestStringToMetricSpec(t *testing.T) {
152152
t.Errorf("stringToMetricSpec() error = %v, wantErr %v", err, tt.wantErr)
153153
return
154154
}
155-
if tt.wantErr {
156-
if got != nil { // handles if we got a nil spec and didn't expect an error
157-
t.Errorf("stringToMetricSpec() = %v, want %v", got, tt.want)
158-
return
159-
}
160-
} else {
161-
if got == nil {
162-
t.Fatalf("stringToMetricSpec() = got nil but wanted %v", tt.want)
155+
if tt.want != nil && got != nil { // compare maps directly
156+
if tt.want.Labels == nil {
157+
tt.want.Labels = make(map[string]string)
163158
}
164159
if !reflect.DeepEqual(got.MetricName, tt.want.MetricName) {
165160
t.Errorf("stringToMetricSpec() got MetricName = %v, want %v", got.MetricName, tt.want.MetricName)
166161
}
167162
if !reflect.DeepEqual(got.Labels, tt.want.Labels) {
168163
t.Errorf("stringToMetricSpec() got Labels = %v, want %v", got.Labels, tt.want.Labels)
169164
}
165+
} else if tt.want != got { // handles if one is nil and the other isn't
166+
t.Errorf("stringToMetricSpec() = %v, want %v", got, tt.want)
170167
}
171168
})
172169
}

0 commit comments

Comments
 (0)