add metrics for backend_retries_total

This commit is contained in:
Marco Jantke 2017-04-18 08:22:06 +02:00 committed by Timo Reimann
parent 7874ffd506
commit e007bb7546
8 changed files with 530 additions and 125 deletions

View file

@ -1,22 +1,29 @@
package middlewares
import (
"github.com/go-kit/kit/metrics"
"net/http"
"strconv"
"time"
"github.com/go-kit/kit/metrics"
)
// Metrics is an Interface that must be satisfied by any system that
// wants to expose and monitor metrics
// wants to expose and monitor Metrics.
type Metrics interface {
getReqsCounter() metrics.Counter
getLatencyHistogram() metrics.Histogram
handler() http.Handler
getReqDurationHistogram() metrics.Histogram
RetryMetrics
}
// RetryMetrics must be satisfied by any system that wants to collect and
// expose retry specific Metrics.
type RetryMetrics interface {
getRetryCounter() metrics.Counter
}
// MetricsWrapper is a Negroni compatible Handler which relies on a
// given Metrics implementation to expose and monitor Traefik metrics
// given Metrics implementation to expose and monitor Traefik Metrics.
type MetricsWrapper struct {
Impl Metrics
}
@ -35,17 +42,25 @@ func (m *MetricsWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Request, next
start := time.Now()
prw := &responseRecorder{rw, http.StatusOK}
next(prw, r)
labels := []string{"code", strconv.Itoa(prw.StatusCode()), "method", r.Method}
labels := []string{"code", strconv.Itoa(prw.statusCode), "method", r.Method}
m.Impl.getReqsCounter().With(labels...).Add(1)
m.Impl.getLatencyHistogram().Observe(float64(time.Since(start).Seconds()))
m.Impl.getReqDurationHistogram().Observe(float64(time.Since(start).Seconds()))
}
func (rw *responseRecorder) StatusCode() int {
return rw.statusCode
// MetricsRetryListener is an implementation of the RetryListener interface to
// record Metrics about retry attempts.
type MetricsRetryListener struct {
retryMetrics RetryMetrics
}
// Handler is the chance for the Metrics implementation
// to expose its metrics on a server endpoint
func (m *MetricsWrapper) Handler() http.Handler {
return m.Impl.handler()
// Retried tracks the retry in the Metrics implementation.
func (m *MetricsRetryListener) Retried(attempt int) {
if m.retryMetrics != nil {
m.retryMetrics.getRetryCounter().Add(1)
}
}
// NewMetricsRetryListener instantiates a MetricsRetryListener with the given RetryMetrics.
func NewMetricsRetryListener(retryMetrics RetryMetrics) RetryListener {
return &MetricsRetryListener{retryMetrics: retryMetrics}
}