From b40fa61783c6f1a08c49aab15949eba4eee26066 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Thu, 19 Mar 2020 13:48:04 +0100 Subject: [PATCH 1/2] Fix memory leak in metrics Co-authored-by: Ludovic Fernandez --- pkg/metrics/metrics.go | 32 +++++++++--------------------- pkg/metrics/metrics_test.go | 8 +++----- pkg/middlewares/metrics/metrics.go | 4 +--- 3 files changed, 13 insertions(+), 31 deletions(-) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 4e7749d07..8927e3245 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -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 } @@ -251,17 +244,10 @@ func NewMultiHistogram(h ...ScalableHistogram) MultiHistogram { return MultiHistogram(h) } -// StartAt implements ScalableHistogram. -func (h MultiHistogram) StartAt(t time.Time) { +// ObserveFromStart implements ScalableHistogram. +func (h MultiHistogram) ObserveFromStart(start time.Time) { for _, histogram := range h { - histogram.StartAt(t) - } -} - -// ObserveDuration implements ScalableHistogram. -func (h MultiHistogram) ObserveDuration() { - for _, histogram := range h { - histogram.ObserveDuration() + histogram.ObserveFromStart(start) } } diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 221193a42..522be234f 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -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) @@ -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 diff --git a/pkg/middlewares/metrics/metrics.go b/pkg/middlewares/metrics/metrics.go index 53ce890fa..1e55467b6 100644 --- a/pkg/middlewares/metrics/metrics.go +++ b/pkg/middlewares/metrics/metrics.go @@ -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 { From f4fb758629c8aa266d96341e839ace6429b5a508 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 19 Mar 2020 15:46:04 +0100 Subject: [PATCH 2/2] Prepare release v2.1.8 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d8444d61..95a2c395b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [v2.1.8](https://github.com/containous/traefik/tree/v2.1.8) (2020-03-19) +[All Commits](https://github.com/containous/traefik/compare/v2.1.7...v2.1.8) + +**Bug fixes:** +- **[middleware,metrics]** Fix memory leak in metrics ([#6522](https://github.com/containous/traefik/pull/6522) by [juliens](https://github.com/juliens)) + ## [v2.1.7](https://github.com/containous/traefik/tree/v2.1.7) (2020-03-18) [All Commits](https://github.com/containous/traefik/compare/v2.1.6...v2.1.7)