Skip to content
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

fix inference extension not correctly scrape pod metrics #366

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkg/ext-proc/backend/vllm/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *PodMetricsClientImpl) FetchMetrics(

// Currently the metrics endpoint is hard-coded, which works with vLLM.
// TODO(https://github.com/kubernetes-sigs/gateway-api-inference-extension/issues/16): Consume this from InferencePool config.
url := fmt.Sprintf("http://%s/metrics", existing.Address)
url := existing.BuildScrapeEndpoint()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just pass in the port number to the function and have the caller get the port number from the pool directly instead of adding the port to every pod?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes but I did like this for considerations as:

  • I think during the lifetime of a pod, the scrape port or path should not change, and I think it is reasonable to store this info in PodMetrics.
  • If we pass the port to FetchMetrics, we may call datastore.PoolGet() to get the targetPort, this will try to acuire the pool lock every 50 milliseconds, I think this may affect the performance (but not tested).

I can rewrite the code if you think this considerations are not reasonable. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, thanks for looking into it! btw #363 will merge first, so we will need to rebase this PR unfortunately since the ext-proc/backend/types.go will move to ext-proc/datastore/types.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, thanks for you comments!

Copy link
Contributor

Choose a reason for hiding this comment

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

The PR merged now, pls rebase when you get a chance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

Copy link
Contributor

Choose a reason for hiding this comment

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

btw the lock is a read lock, so it shouldn't add any overhead, but this is fine too.

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
loggerDefault.Error(err, "Failed create HTTP request", "method", http.MethodGet, "url", url)
Expand Down
10 changes: 5 additions & 5 deletions pkg/ext-proc/controller/pod_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
)

var (
basePod1 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod1"}, Address: "address-1"}}
basePod2 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod2"}, Address: "address-2"}}
basePod3 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod3"}, Address: "address-3"}}
basePod11 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod1"}, Address: "address-11"}}
basePod1 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod1"}, Address: "address-1", ScrapePath: "/metrics", ScrapePort: 8000}}
basePod2 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod2"}, Address: "address-2", ScrapePath: "/metrics", ScrapePort: 8000}}
basePod3 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod3"}, Address: "address-3", ScrapePath: "/metrics", ScrapePort: 8000}}
basePod11 = &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: types.NamespacedName{Name: "pod1"}, Address: "address-11", ScrapePath: "/metrics", ScrapePort: 8000}}
)

func TestUpdateDatastore_PodReconciler(t *testing.T) {
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestUpdateDatastore_PodReconciler(t *testing.T) {
func populateMap(pods ...*datastore.PodMetrics) *sync.Map {
newMap := &sync.Map{}
for _, pod := range pods {
newMap.Store(pod.NamespacedName, &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: pod.NamespacedName, Address: pod.Address}})
newMap.Store(pod.NamespacedName, &datastore.PodMetrics{Pod: datastore.Pod{NamespacedName: pod.NamespacedName, Address: pod.Address, ScrapePort: pod.ScrapePort, ScrapePath: pod.ScrapePath}})
}
return newMap
}
5 changes: 4 additions & 1 deletion pkg/ext-proc/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,16 @@ func (ds *datastore) PodDelete(namespacedName types.NamespacedName) {
}

func (ds *datastore) PodUpdateOrAddIfNotExist(pod *corev1.Pod) bool {
pool, _ := ds.PoolGet()
new := &PodMetrics{
Pod: Pod{
NamespacedName: types.NamespacedName{
Name: pod.Name,
Namespace: pod.Namespace,
},
Address: pod.Status.PodIP,
Address: pod.Status.PodIP,
ScrapePath: "/metrics",
ScrapePort: pool.Spec.TargetPortNumber,
},
Metrics: Metrics{
ActiveModels: make(map[string]int),
Expand Down
10 changes: 10 additions & 0 deletions pkg/ext-proc/datastore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
type Pod struct {
NamespacedName types.NamespacedName
Address string

// metrics scrape options
ScrapePort int32
ScrapePath string
}

type Metrics struct {
Expand Down Expand Up @@ -41,6 +45,8 @@ func (pm *PodMetrics) Clone() *PodMetrics {
Pod: Pod{
NamespacedName: pm.NamespacedName,
Address: pm.Address,
ScrapePort: pm.ScrapePort,
ScrapePath: pm.ScrapePath,
},
Metrics: Metrics{
ActiveModels: cm,
Expand All @@ -52,3 +58,7 @@ func (pm *PodMetrics) Clone() *PodMetrics {
}
return clone
}

func (pm *PodMetrics) BuildScrapeEndpoint() string {
return fmt.Sprintf("http://%s:%d%s", pm.Address, pm.ScrapePort, pm.ScrapePath)
}