Skip to content

Add nil option for metric_spec to specify metrics to not be scraped. #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/epp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func run() error {

// Set up mapper for metric scraping.
mapping, err := backendmetrics.NewMetricMapping(
ctx,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to pass the ctx, just check here and log which metrics are set to an empty string and will not be scraped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

*totalQueuedRequestsMetric,
*kvCacheUsagePercentageMetric,
*loraInfoMetric,
Expand Down
20 changes: 19 additions & 1 deletion pkg/epp/backend/metrics/metrics_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ limitations under the License.
package metrics

import (
"context"
"fmt"
"strings"

"sigs.k8s.io/controller-runtime/pkg/log"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)

// MetricSpec represents a single metric's specification.
Expand All @@ -41,6 +45,9 @@ type MetricMapping struct {
// "metric_name{label1=value1}"
// "metric_name{label1=value1,label2=value2}"
func stringToMetricSpec(specStr string) (*MetricSpec, error) {
if specStr == "" {
return nil, nil // Allow empty strings to represent nil MetricSpecs
}
specStr = strings.TrimSpace(specStr)
metricName := specStr
labels := make(map[string]string)
Expand Down Expand Up @@ -90,7 +97,7 @@ func stringToMetricSpec(specStr string) (*MetricSpec, error) {
}

// NewMetricMapping creates a MetricMapping from string values.
func NewMetricMapping(queuedStr, kvUsageStr, loraReqInfoStr string) (*MetricMapping, error) {
func NewMetricMapping(ctx context.Context, queuedStr, kvUsageStr, loraReqInfoStr string) (*MetricMapping, error) {
queuedSpec, err := stringToMetricSpec(queuedStr)
if err != nil {
return nil, fmt.Errorf("error parsing WaitingRequests: %w", err)
Expand All @@ -109,5 +116,16 @@ func NewMetricMapping(queuedStr, kvUsageStr, loraReqInfoStr string) (*MetricMapp
LoraRequestInfo: loraReqInfoSpec,
}

logger := log.FromContext(ctx)
if mapping.TotalQueuedRequests == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: TotalQueuedRequests")
}
if mapping.KVCacheUtilization == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: KVCacheUtilization")
}
if mapping.LoraRequestInfo == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: LoraRequestInfo")
}

return mapping, nil
}
15 changes: 6 additions & 9 deletions pkg/epp/backend/metrics/metrics_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestStringToMetricSpec(t *testing.T) {
name: "empty string",
input: "",
want: nil,
wantErr: true,
wantErr: false,
},
{
name: "no labels",
Expand Down Expand Up @@ -152,21 +152,18 @@ func TestStringToMetricSpec(t *testing.T) {
t.Errorf("stringToMetricSpec() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if got != nil { // handles if we got a nil spec and didn't expect an error
t.Errorf("stringToMetricSpec() = %v, want %v", got, tt.want)
return
}
} else {
if got == nil {
t.Fatalf("stringToMetricSpec() = got nil but wanted %v", tt.want)
if tt.want != nil && got != nil { // compare maps directly
if tt.want.Labels == nil {
tt.want.Labels = make(map[string]string)
}
if !reflect.DeepEqual(got.MetricName, tt.want.MetricName) {
t.Errorf("stringToMetricSpec() got MetricName = %v, want %v", got.MetricName, tt.want.MetricName)
}
if !reflect.DeepEqual(got.Labels, tt.want.Labels) {
t.Errorf("stringToMetricSpec() got Labels = %v, want %v", got.Labels, tt.want.Labels)
}
} else if tt.want != got { // handles if one is nil and the other isn't
t.Errorf("stringToMetricSpec() = %v, want %v", got, tt.want)
}
})
}
Expand Down