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 memory leak in metrics #6522

Merged
merged 2 commits into from
Mar 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 9 additions & 23 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,29 @@ func (r *standardRegistry) ServiceServerUpGauge() metrics.Gauge {
// used when producing observations without explicitly setting the observed value.
type ScalableHistogram interface {
With(labelValues ...string) ScalableHistogram
StartAt(t time.Time)
Observe(v float64)
ObserveDuration()
ObserveFromStart(start time.Time)
}

// HistogramWithScale is a histogram that will convert its observed value to the specified unit.
type HistogramWithScale struct {
histogram metrics.Histogram
unit time.Duration
start time.Time
}

// With implements ScalableHistogram.
func (s *HistogramWithScale) With(labelValues ...string) ScalableHistogram {
s.histogram = s.histogram.With(labelValues...)
return s
h, _ := NewHistogramWithScale(s.histogram.With(labelValues...), s.unit)
return h
}

// StartAt implements ScalableHistogram.
func (s *HistogramWithScale) StartAt(t time.Time) {
s.start = t
}

// ObserveDuration implements ScalableHistogram.
func (s *HistogramWithScale) ObserveDuration() {
// ObserveFromStart implements ScalableHistogram.
func (s *HistogramWithScale) ObserveFromStart(start time.Time) {
if s.unit <= 0 {
return
}

d := float64(time.Since(s.start).Nanoseconds()) / float64(s.unit)
d := float64(time.Since(start).Nanoseconds()) / float64(s.unit)
if d < 0 {
d = 0
}
Expand Down Expand Up @@ -251,17 +244,10 @@ func NewMultiHistogram(h ...ScalableHistogram) MultiHistogram {
return MultiHistogram(h)
}

// StartAt implements ScalableHistogram.
func (h MultiHistogram) StartAt(t time.Time) {
for _, histogram := range h {
histogram.StartAt(t)
}
}

// ObserveDuration implements ScalableHistogram.
func (h MultiHistogram) ObserveDuration() {
// ObserveFromStart implements ScalableHistogram.
func (h MultiHistogram) ObserveFromStart(start time.Time) {
for _, histogram := range h {
histogram.ObserveDuration()
histogram.ObserveFromStart(start)
}
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func TestScalableHistogram(t *testing.T) {

ticker := time.NewTicker(500 * time.Millisecond)
<-ticker.C
sh.StartAt(time.Now())
start := time.Now()
<-ticker.C
sh.ObserveDuration()
sh.ObserveFromStart(start)

var b bytes.Buffer
h.Print(&b)
Expand Down Expand Up @@ -99,9 +99,7 @@ func (c *histogramMock) With(labelValues ...string) ScalableHistogram {

func (c *histogramMock) Start() {}

func (c *histogramMock) StartAt(t time.Time) {}

func (c *histogramMock) ObserveDuration() {}
func (c *histogramMock) ObserveFromStart(t time.Time) {}

func (c *histogramMock) Observe(v float64) {
c.lastHistogramValue = v
Expand Down
4 changes: 1 addition & 3 deletions pkg/middlewares/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ func (m *metricsMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request)
labels = append(labels, "code", strconv.Itoa(recorder.getCode()))

histograms := m.reqDurationHistogram.With(labels...)
histograms.StartAt(start)
histograms.ObserveFromStart(start)

m.reqsCounter.With(labels...).Add(1)

histograms.ObserveDuration()
}

func getRequestProtocol(req *http.Request) string {
Expand Down