Skip to content

Commit c1f66db

Browse files
committed
address review comments
Signed-off-by: Etai Lev Ran <[email protected]>
1 parent 085e7a5 commit c1f66db

File tree

10 files changed

+39
-34
lines changed

10 files changed

+39
-34
lines changed

cmd/epp/runner/runner.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,16 @@ func setupMetricsV1(setupLog logr.Logger) (datalayer.EndpointFactory, error) {
492492
return pmf, nil
493493
}
494494

495+
// This function serves two (independent) purposes:
496+
// - creating data sources and configuring their extractors.
497+
// - configuring endpoint factory with the provided source.
498+
// In the future, data sources and extractors might be configured via
499+
// a file. Once done, this (and registering the sources with the
500+
// endpoint factory) should be moved accordingly.
501+
// Regardless, registration of all sources (e.g., if additional sources
502+
// are to be configured), must be done before the EndpointFactory is initialized.
495503
func setupDatalayer(logger logr.Logger) (datalayer.EndpointFactory, error) {
496-
// create and register a metrics data source and extractor. In the future,
497-
// data sources and extractors might be configured via a file. Once done,
498-
// this (and registering the sources with the endpoint factory) should
499-
// be moved accordingly.
504+
// create and register a metrics data source and extractor.
500505
source := dlmetrics.NewDataSource(*modelServerMetricsScheme,
501506
*modelServerMetricsPath,
502507
*modelServerMetricsHttpsInsecureSkipVerify,
@@ -515,6 +520,7 @@ func setupDatalayer(logger logr.Logger) (datalayer.EndpointFactory, error) {
515520
return nil, err
516521
}
517522

523+
// TODO: this could be moved to the configuration loading functions once ported over.
518524
sources := datalayer.GetSources()
519525
for _, src := range sources {
520526
logger.Info("data layer configuration", "source", src.Name(), "extractors", src.Extractors())

pkg/epp/datalayer/collector_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ func (d *DummySource) Collect(ctx context.Context, ep Endpoint) error {
4444
}
4545

4646
func defaultEndpoint() Endpoint {
47-
ms := NewEndpoint()
4847
pod := &PodInfo{
4948
NamespacedName: types.NamespacedName{
5049
Name: "pod-name",
5150
Namespace: "default",
5251
},
5352
Address: "1.2.3.4:5678",
5453
}
55-
ms.UpdatePod(pod)
54+
ms := NewEndpoint(pod, nil)
5655
return ms
5756
}
5857

pkg/epp/datalayer/endpoint.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ type ModelServer struct {
4848
attributes *Attributes
4949
}
5050

51-
// NewEndpoint return a new (uninitialized) ModelServer.
52-
func NewEndpoint() *ModelServer {
53-
return &ModelServer{
51+
// NewEndpoint returns a new ModelServer with the given PodInfo and Metrics.
52+
func NewEndpoint(pod *PodInfo, metrics *Metrics) *ModelServer {
53+
if pod == nil {
54+
pod = &PodInfo{}
55+
}
56+
if metrics == nil {
57+
metrics = NewMetrics()
58+
}
59+
ep := &ModelServer{
5460
attributes: NewAttributes(),
5561
}
56-
}
57-
58-
// NewInitializedEndpoint returns a new ModelServer with the given PodInfo and Metrics.
59-
func NewInitializedEndpoint(pod *PodInfo, metrics *Metrics) *ModelServer {
60-
ep := NewEndpoint()
6162
ep.UpdatePod(pod)
6263
ep.UpdateMetrics(metrics)
6364
return ep

pkg/epp/datalayer/factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func (lc *EndpointLifecycle) NewEndpoint(parent context.Context, inpod *PodInfo,
7878
return nil
7979
}
8080

81-
endpoint := NewInitializedEndpoint(inpod, NewMetrics())
82-
collector := NewCollector() // for full backward compatibility, set the logger and poolinfo
81+
endpoint := NewEndpoint(inpod, nil)
82+
collector := NewCollector() // TODO or full backward compatibility, set the logger and poolinfo
8383

8484
if _, loaded := lc.collectors.LoadOrStore(key, collector); loaded {
8585
// another goroutine already created and stored a collector for this endpoint.

pkg/epp/datalayer/metrics/datasource.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func (dataSrc *DataSource) Collect(ctx context.Context, ep datalayer.Endpoint) e
115115
return true // continue iteration
116116
})
117117

118-
// TODO: decide if to log only or return the errors
119118
if len(errs) != 0 {
120119
return errors.Join(errs...)
121120
}

pkg/epp/datalayer/metrics/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (ext *Extractor) Extract(ctx context.Context, data any, ep datalayer.Endpoi
146146
}
147147

148148
if len(errs) != 0 {
149-
logger.V(logutil.TRACE).Info("Failed to refresh metrics:", "err", errors.Join(errs...))
149+
return errors.Join(errs...)
150150
}
151151
return nil
152152
}

pkg/epp/datalayer/metrics/extractor_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/google/go-cmp/cmp"
2424
dto "github.com/prometheus/client_model/go"
25+
"k8s.io/utils/ptr"
2526

2627
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer"
2728
)
@@ -39,7 +40,7 @@ func TestExtractorExtract(t *testing.T) {
3940
t.Fatalf("failed to create extractor: %v", err)
4041
}
4142

42-
ep := datalayer.NewInitializedEndpoint(&datalayer.PodInfo{}, datalayer.NewMetrics())
43+
ep := datalayer.NewEndpoint(nil, nil)
4344
if ep == nil {
4445
t.Fatal("expected non-nil endpoint")
4546
}
@@ -59,8 +60,8 @@ func TestExtractorExtract(t *testing.T) {
5960
{
6061
name: "empty PrometheusMetricMap",
6162
data: PrometheusMetricMap{},
62-
wantErr: false, // no errors when metrics are missing
63-
updated: false,
63+
wantErr: true, // errors when metrics are missing
64+
updated: false, // and also not updated...
6465
},
6566
{
6667
name: "single valid metric",
@@ -69,13 +70,13 @@ func TestExtractorExtract(t *testing.T) {
6970
Type: dto.MetricType_GAUGE.Enum(),
7071
Metric: []*dto.Metric{
7172
{
72-
Gauge: &dto.Gauge{Value: float64Ptr(5)},
73+
Gauge: &dto.Gauge{Value: ptr.To(5.0)},
7374
},
7475
},
7576
},
7677
},
77-
wantErr: false,
78-
updated: true,
78+
wantErr: true, // missing metrics can return an error
79+
updated: true, // but should still update
7980
},
8081
}
8182

pkg/epp/datalayer/metrics/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func printDebugMetrics(logger logr.Logger, datastore datalayer.PoolInfo, stalene
9696
freshPods := datastore.PodList(podsWithFreshMetrics(stalenessThreshold))
9797
stalePods := datastore.PodList(podsWithStaleMetrics(stalenessThreshold))
9898

99-
s := fmt.Sprintf("Current Pods and metrics gathered. Fresh metrics: %+v, Stale metrics: %+v", freshPods, stalePods)
100-
logger.V(logutil.VERBOSE).Info(s)
99+
logger.V(logutil.TRACE).Info("Current Pods and metrics gathered",
100+
"Fresh metrics", fmt.Sprintf("%+v", freshPods), "Stale metrics", fmt.Sprintf("%+v", stalePods))
101101
}
102102

103103
func refreshPrometheusMetrics(logger logr.Logger, datastore datalayer.PoolInfo, stalenessThreshold time.Duration) {

pkg/epp/datalayer/metrics/spec.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ func (spec *Spec) labelsMatch(metricLabels []*dto.LabelPair) bool {
149149
func extractValue(metric *dto.Metric) float64 {
150150
if metric == nil {
151151
return 0
152-
} else if gauge := metric.GetGauge(); gauge != nil {
152+
}
153+
if gauge := metric.GetGauge(); gauge != nil {
153154
return gauge.GetValue()
154-
} else if counter := metric.GetCounter(); counter != nil {
155+
}
156+
if counter := metric.GetCounter(); counter != nil {
155157
return counter.GetValue()
156158
}
157159
return 0

pkg/epp/datalayer/metrics/spec_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"github.com/stretchr/testify/require"
2828
"google.golang.org/protobuf/proto"
29+
"k8s.io/utils/ptr"
2930
)
3031

3132
// --- Test Helpers ---
3233

33-
func float64Ptr(v float64) *float64 {
34-
return &v
35-
}
36-
3734
func makeMetric(labels map[string]string, value float64, timestampMs int64) *dto.Metric {
3835
labelPairs := []*dto.LabelPair{}
3936
for k, v := range labels {
@@ -503,14 +500,14 @@ func TestExtractValue(t *testing.T) {
503500
{
504501
name: "gauge metric",
505502
metric: &dto.Metric{
506-
Gauge: &dto.Gauge{Value: float64Ptr(42.5)},
503+
Gauge: &dto.Gauge{Value: ptr.To(42.5)},
507504
},
508505
want: 42.5,
509506
},
510507
{
511508
name: "counter metric",
512509
metric: &dto.Metric{
513-
Counter: &dto.Counter{Value: float64Ptr(99.9)},
510+
Counter: &dto.Counter{Value: ptr.To(99.9)},
514511
},
515512
want: 99.9,
516513
},

0 commit comments

Comments
 (0)