summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/metrics.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-01-25 15:34:35 -0500
committerGitHub <noreply@github.com>2022-01-25 15:34:35 -0500
commit741b0502ee7cb0ecbd6dd0517c4f186729f52718 (patch)
treea8dbe6a5bbb0aa10dc9ab9c2f5111e017e559358 /modules/caddyhttp/metrics.go
parent44e5e9e43f3583f04613bbbb1996e9b5a13a60ac (diff)
parent7ca5921a87c819f9848ccd7ec786aab0f896be72 (diff)
Merge pull request #4545 from hairyhenderson/metrics-restrict-http-methods
metrics: Enforce smaller set of method labels
Diffstat (limited to 'modules/caddyhttp/metrics.go')
-rw-r--r--modules/caddyhttp/metrics.go16
1 files changed, 4 insertions, 12 deletions
diff --git a/modules/caddyhttp/metrics.go b/modules/caddyhttp/metrics.go
index 3e5d639..458c22a 100644
--- a/modules/caddyhttp/metrics.go
+++ b/modules/caddyhttp/metrics.go
@@ -3,11 +3,10 @@ package caddyhttp
import (
"context"
"net/http"
- "strconv"
- "strings"
"sync"
"time"
+ "github.com/caddyserver/caddy/v2/internal/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
@@ -109,7 +108,7 @@ func newMetricsInstrumentedHandler(handler string, mh MiddlewareHandler) *metric
func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
server := serverNameFromContext(r.Context())
labels := prometheus.Labels{"server": server, "handler": h.handler}
- method := strings.ToUpper(r.Method)
+ method := metrics.SanitizeMethod(r.Method)
// the "code" value is set later, but initialized here to eliminate the possibility
// of a panic
statusLabels := prometheus.Labels{"server": server, "handler": h.handler, "method": method, "code": ""}
@@ -124,7 +123,7 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
// being called when the headers are written.
// Effectively the same behaviour as promhttp.InstrumentHandlerTimeToWriteHeader.
writeHeaderRecorder := ShouldBufferFunc(func(status int, header http.Header) bool {
- statusLabels["code"] = sanitizeCode(status)
+ statusLabels["code"] = metrics.SanitizeCode(status)
ttfb := time.Since(start).Seconds()
httpMetrics.responseDuration.With(statusLabels).Observe(ttfb)
return false
@@ -143,7 +142,7 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
if statusLabels["code"] == "" {
// we still sanitize it, even though it's likely to be 0. A 200 is
// returned on fallthrough so we want to reflect that.
- statusLabels["code"] = sanitizeCode(wrec.Status())
+ statusLabels["code"] = metrics.SanitizeCode(wrec.Status())
}
httpMetrics.requestDuration.With(statusLabels).Observe(dur)
@@ -153,13 +152,6 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
return nil
}
-func sanitizeCode(code int) string {
- if code == 0 {
- return "200"
- }
- return strconv.Itoa(code)
-}
-
// taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298
func computeApproximateRequestSize(r *http.Request) int {
s := 0