From 651d993d9cc0ba35e9352fbca5d06ab409e85220 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Fri, 8 Sep 2017 11:22:03 +0200 Subject: [PATCH] prometheus, HTTP method and utf8 --- middlewares/metrics.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/middlewares/metrics.go b/middlewares/metrics.go index c76c7f0ad..bd91f63d9 100644 --- a/middlewares/metrics.go +++ b/middlewares/metrics.go @@ -4,7 +4,9 @@ import ( "net/http" "strconv" "time" + "unicode/utf8" + "github.com/containous/traefik/log" "github.com/containous/traefik/metrics" gokitmetrics "github.com/go-kit/kit/metrics" ) @@ -17,7 +19,7 @@ type MetricsWrapper struct { } // NewMetricsWrapper return a MetricsWrapper struct with -// a given Metrics implementation e.g Prometheuss +// a given Metrics implementation func NewMetricsWrapper(registry metrics.Registry, service string) *MetricsWrapper { var metricsWrapper = MetricsWrapper{ registry: registry, @@ -32,7 +34,7 @@ func (m *MetricsWrapper) ServeHTTP(rw http.ResponseWriter, r *http.Request, next prw := &responseRecorder{rw, http.StatusOK} next(prw, r) - reqLabels := []string{"service", m.serviceName, "code", strconv.Itoa(prw.statusCode), "method", r.Method} + reqLabels := []string{"service", m.serviceName, "code", strconv.Itoa(prw.statusCode), "method", getMethod(r)} m.registry.ReqsCounter().With(reqLabels...).Add(1) reqDurationLabels := []string{"service", m.serviceName, "code", strconv.Itoa(prw.statusCode)} @@ -48,6 +50,14 @@ func NewMetricsRetryListener(retryMetrics retryMetrics, backendName string) Retr return &MetricsRetryListener{retryMetrics: retryMetrics, backendName: backendName} } +func getMethod(r *http.Request) string { + if !utf8.ValidString(r.Method) { + log.Warnf("Invalid HTTP method encoding: %s", r.Method) + return "NON_UTF8_HTTP_METHOD" + } + return r.Method +} + // MetricsRetryListener is an implementation of the RetryListener interface to // record RequestMetrics about retry attempts. type MetricsRetryListener struct {