Skip to content

Commit

Permalink
perf: reduce metrics labels (#1784)
Browse files Browse the repository at this point in the history
Previously every new url will create a metrics entry with url as label. It will keep increasing as pod/ip creating and will not be released which will exhaust the memory. Aggregate the metrics by url prefix.

(cherry picked from commit 42812b9)
  • Loading branch information
oilbeater committed Aug 5, 2022
1 parent d7a9f5e commit d7fd379
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pkg/controller/client_go_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package controller
import (
"context"
"net/url"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -154,7 +155,12 @@ type latencyAdapter struct {
}

func (l *latencyAdapter) Observe(_ context.Context, verb string, u url.URL, latency time.Duration) {
l.metric.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
url := u.String()
last := strings.LastIndex(url, "/")
if last != -1 {
url = url[:last]
}
l.metric.WithLabelValues(verb, url).Observe(latency.Seconds())
}

type resultAdapter struct {
Expand Down
8 changes: 7 additions & 1 deletion pkg/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"context"
"net/url"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -170,7 +171,12 @@ type latencyAdapter struct {
}

func (l *latencyAdapter) Observe(_ context.Context, verb string, u url.URL, latency time.Duration) {
l.metric.WithLabelValues(verb, u.String()).Observe(latency.Seconds())
url := u.String()
last := strings.LastIndex(url, "/")
if last != -1 {
url = url[:last]
}
l.metric.WithLabelValues(verb, url).Observe(latency.Seconds())
}

type resultAdapter struct {
Expand Down

0 comments on commit d7fd379

Please sign in to comment.