forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler_profile_tracing_test.go
More file actions
247 lines (211 loc) · 8.68 KB
/
Copy pathscheduler_profile_tracing_test.go
File metadata and controls
247 lines (211 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduling
import (
"context"
"testing"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
k8stypes "k8s.io/apimachinery/pkg/types"
fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer"
fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin"
fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling"
)
// setupSpanRecorder installs an in-memory span recorder as the global tracer
// provider and returns it, restoring the previous provider on cleanup.
func setupSpanRecorder(t *testing.T) *tracetest.SpanRecorder {
t.Helper()
recorder := tracetest.NewSpanRecorder()
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(recorder))
origTP := otel.GetTracerProvider()
otel.SetTracerProvider(tp)
t.Cleanup(func() { otel.SetTracerProvider(origTP) })
return recorder
}
func findSpans(spans []sdktrace.ReadOnlySpan, name string) []sdktrace.ReadOnlySpan {
var out []sdktrace.ReadOnlySpan
for _, s := range spans {
if s.Name() == name {
out = append(out, s)
}
}
return out
}
func spanAttributes(span sdktrace.ReadOnlySpan) map[attribute.Key]attribute.Value {
attrs := make(map[attribute.Key]attribute.Value)
for _, kv := range span.Attributes() {
attrs[kv.Key] = kv.Value
}
return attrs
}
func newTestEndpoints(names ...string) []fwksched.Endpoint {
endpoints := make([]fwksched.Endpoint, len(names))
for i, name := range names {
endpoints[i] = fwksched.NewEndpoint(
&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: name}}, nil, nil)
}
return endpoints
}
func TestRunFilterPluginsSingleSpan(t *testing.T) {
recorder := setupSpanRecorder(t)
filter := &testPlugin{
typedName: fwkplugin.TypedName{Type: "test-filter", Name: "instance-a"},
FilterRes: []k8stypes.NamespacedName{{Name: "pod1"}, {Name: "pod2"}},
}
profile := NewSchedulerProfile().WithFilters(filter)
endpoints := newTestEndpoints("pod1", "pod2", "pod3")
ctx, root := otel.Tracer("test").Start(context.Background(), "root")
result := profile.runFilterPlugins(ctx, &fwksched.InferenceRequest{TargetModel: "m1", RequestID: "r1"}, endpoints)
root.End()
if len(result) != 2 {
t.Fatalf("runFilterPlugins returned %d endpoints, want 2", len(result))
}
spans := findSpans(recorder.Ended(), "filter_endpoints")
if len(spans) != 1 {
t.Fatalf("got %d filter_endpoints spans, want 1", len(spans))
}
span := spans[0]
if span.SpanKind() != trace.SpanKindInternal {
t.Errorf("span kind = %v, want Internal", span.SpanKind())
}
if span.Parent().SpanID() != root.SpanContext().SpanID() {
t.Errorf("parent span ID = %v, want root %v", span.Parent().SpanID(), root.SpanContext().SpanID())
}
attrs := spanAttributes(span)
if got := attrs["llm_d.epp.filter.candidate_endpoints"].AsInt64(); got != 3 {
t.Errorf("candidate_endpoints = %d, want 3", got)
}
if got := attrs["llm_d.epp.filter.filtered_endpoints"].AsInt64(); got != 2 {
t.Errorf("filtered_endpoints = %d, want 2", got)
}
if got := attrs["gen_ai.request.model"].AsString(); got != "m1" {
t.Errorf("gen_ai.request.model = %q, want %q", got, "m1")
}
if got := attrs["gen_ai.request.id"].AsString(); got != "r1" {
t.Errorf("gen_ai.request.id = %q, want %q", got, "r1")
}
}
// A filter chain emits one span over the whole stage: candidate is the input to
// the first filter, filtered is the output of the last filter that ran.
func TestRunFilterPluginsChainEmitsOneSpan(t *testing.T) {
recorder := setupSpanRecorder(t)
filterA := &testPlugin{
typedName: fwkplugin.TypedName{Type: "filter-a", Name: "a"},
FilterRes: []k8stypes.NamespacedName{{Name: "pod1"}, {Name: "pod2"}},
}
filterB := &testPlugin{
typedName: fwkplugin.TypedName{Type: "filter-b", Name: "b"},
FilterRes: []k8stypes.NamespacedName{{Name: "pod1"}},
}
profile := NewSchedulerProfile().WithFilters(filterA, filterB)
endpoints := newTestEndpoints("pod1", "pod2", "pod3")
ctx, root := otel.Tracer("test").Start(context.Background(), "root")
result := profile.runFilterPlugins(ctx, &fwksched.InferenceRequest{}, endpoints)
root.End()
if len(result) != 1 {
t.Fatalf("runFilterPlugins returned %d endpoints, want 1", len(result))
}
spans := findSpans(recorder.Ended(), "filter_endpoints")
if len(spans) != 1 {
t.Fatalf("got %d filter_endpoints spans, want 1 for the whole chain", len(spans))
}
attrs := spanAttributes(spans[0])
if got := attrs["llm_d.epp.filter.candidate_endpoints"].AsInt64(); got != 3 {
t.Errorf("candidate_endpoints = %d, want 3", got)
}
if got := attrs["llm_d.epp.filter.filtered_endpoints"].AsInt64(); got != 1 {
t.Errorf("filtered_endpoints = %d, want 1", got)
}
}
func TestRunFilterPluginsDrainBreakStillEndsSpan(t *testing.T) {
recorder := setupSpanRecorder(t)
drain := &testPlugin{
typedName: fwkplugin.TypedName{Type: "drain-filter", Name: "drain"},
FilterRes: []k8stypes.NamespacedName{},
}
never := &testPlugin{
typedName: fwkplugin.TypedName{Type: "never-filter", Name: "never"},
FilterRes: []k8stypes.NamespacedName{{Name: "pod1"}},
}
profile := NewSchedulerProfile().WithFilters(drain, never)
endpoints := newTestEndpoints("pod1", "pod2")
ctx, root := otel.Tracer("test").Start(context.Background(), "root")
result := profile.runFilterPlugins(ctx, &fwksched.InferenceRequest{}, endpoints)
root.End()
if len(result) != 0 {
t.Fatalf("runFilterPlugins returned %d endpoints, want 0", len(result))
}
spans := findSpans(recorder.Ended(), "filter_endpoints")
if len(spans) != 1 {
t.Fatalf("got %d filter_endpoints spans, want 1 (span must end on drain)", len(spans))
}
if got := spanAttributes(spans[0])["llm_d.epp.filter.filtered_endpoints"].AsInt64(); got != 0 {
t.Errorf("filtered_endpoints = %d, want 0", got)
}
if never.FilterCallCount != 0 {
t.Errorf("second filter ran %d times after drain, want 0", never.FilterCallCount)
}
}
// childSpanFilter starts a child span from the context it is given, so a test
// can assert the delegate runs inside the filter span.
type childSpanFilter struct{ typedName fwkplugin.TypedName }
func (f *childSpanFilter) TypedName() fwkplugin.TypedName { return f.typedName }
func (f *childSpanFilter) Filter(ctx context.Context, _ *fwksched.InferenceRequest, endpoints []fwksched.Endpoint) []fwksched.Endpoint {
_, span := otel.Tracer("test").Start(ctx, "inner_filter_span")
span.End()
return endpoints
}
func TestRunFilterPluginsNestsDelegateSpan(t *testing.T) {
recorder := setupSpanRecorder(t)
filter := &childSpanFilter{typedName: fwkplugin.TypedName{Type: "child", Name: "c"}}
profile := NewSchedulerProfile().WithFilters(filter)
endpoints := newTestEndpoints("pod1")
ctx, root := otel.Tracer("test").Start(context.Background(), "root")
profile.runFilterPlugins(ctx, &fwksched.InferenceRequest{}, endpoints)
root.End()
outer := findSpans(recorder.Ended(), "filter_endpoints")
inner := findSpans(recorder.Ended(), "inner_filter_span")
if len(outer) != 1 || len(inner) != 1 {
t.Fatalf("got %d filter_endpoints and %d inner spans, want 1 each", len(outer), len(inner))
}
if inner[0].Parent().SpanID() != outer[0].SpanContext().SpanID() {
t.Errorf("inner span parent = %v, want filter_endpoints span %v",
inner[0].Parent().SpanID(), outer[0].SpanContext().SpanID())
}
}
func TestRunFilterPluginsOmitsEmptyGenAI(t *testing.T) {
recorder := setupSpanRecorder(t)
filter := &testPlugin{
typedName: fwkplugin.TypedName{Type: "f", Name: "n"},
FilterRes: []k8stypes.NamespacedName{{Name: "pod1"}},
}
profile := NewSchedulerProfile().WithFilters(filter)
endpoints := newTestEndpoints("pod1")
ctx, root := otel.Tracer("test").Start(context.Background(), "root")
profile.runFilterPlugins(ctx, &fwksched.InferenceRequest{}, endpoints)
root.End()
spans := findSpans(recorder.Ended(), "filter_endpoints")
if len(spans) != 1 {
t.Fatalf("got %d filter_endpoints spans, want 1", len(spans))
}
attrs := spanAttributes(spans[0])
if _, ok := attrs["gen_ai.request.model"]; ok {
t.Error("gen_ai.request.model set for empty TargetModel")
}
if _, ok := attrs["gen_ai.request.id"]; ok {
t.Error("gen_ai.request.id set for empty RequestID")
}
}