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 {