@marcel91200 / caddy Mirror · read-only / commits / 3e3bb002
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 19:15 UTC

reverseproxy: Add `_ms` placeholders for proxy durations (#4666)

* reverseproxy: Add `_ms` placeholders for proxy durations * Add http.request.duration_ms Also add comments, and change duration_sec to duration_ms * Add response.duration_ms for consistency * Add missing godoc comment Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>

Francis Lavoie committed Apr 11, 2022 at 15:04 UTC 3e3bb00265f8fa0dd8ff3d33ce901776fd689f32
4 files changed +11
modules/caddyhttp/app.go
+1
@@ -49,6 +49,7 @@ func init() {
49 // `{http.request.body}` | The request body (⚠️ inefficient; use only for debugging)
50 // `{http.request.cookie.*}` | HTTP request cookie
51 // `{http.request.duration}` | Time up to now spent handling the request (after decoding headers from client)
52 +// `{http.request.duration_ms}` | Same as 'duration', but in milliseconds.
53 // `{http.request.uuid}` | The request unique identifier
54 // `{http.request.header.*}` | Specific request header field
55 // `{http.request.host.labels.*}` | Request host labels (0-based from right); e.g. for foo.example.com: 0=com, 1=example, 2=foo
modules/caddyhttp/replacer.go
+3
@@ -148,6 +148,9 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
148 case "http.request.duration":
149 start := GetVar(req.Context(), "start_time").(time.Time)
150 return time.Since(start), true
151 + case "http.request.duration_ms":
152 + start := GetVar(req.Context(), "start_time").(time.Time)
153 + return time.Since(start).Seconds() * 1e3, true // multiply seconds to preserve decimal (see #4666)
154 case "http.request.uuid":
155 id := GetVar(req.Context(), "uuid").(*requestID)
156 return id.String(), true
modules/caddyhttp/reverseproxy/reverseproxy.go
+6
@@ -58,8 +58,11 @@ func init() {
58 // `{http.reverse_proxy.upstream.max_requests}` | The maximum approximate number of requests allowed to the upstream
59 // `{http.reverse_proxy.upstream.fails}` | The number of recent failed requests to the upstream
60 // `{http.reverse_proxy.upstream.latency}` | How long it took the proxy upstream to write the response header.
61 +// `{http.reverse_proxy.upstream.latency_ms}` | Same as 'latency', but in milliseconds.
62 // `{http.reverse_proxy.upstream.duration}` | Time spent proxying to the upstream, including writing response body to client.
63 +// `{http.reverse_proxy.upstream.duration_ms}` | Same as 'upstream.duration', but in milliseconds.
64 // `{http.reverse_proxy.duration}` | Total time spent proxying, including selecting an upstream, retries, and writing response.
65 +// `{http.reverse_proxy.duration_ms}` | Same as 'duration', but in milliseconds.
66 type Handler struct {
67 // Configures the method of transport for the proxy. A transport
68 // is what performs the actual "round trip" to the backend.
@@ -400,6 +403,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
403 defer func() {
404 // total proxying duration, including time spent on LB and retries
405 repl.Set("http.reverse_proxy.duration", time.Since(start))
406 + repl.Set("http.reverse_proxy.duration_ms", time.Since(start).Seconds()*1e3) // multiply seconds to preserve decimal (see #4666)
407 }()
408
409 // in the proxy loop, each iteration is an attempt to proxy the request,
@@ -724,6 +728,7 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, repl *
728
729 // duration until upstream wrote response headers (roundtrip duration)
730 repl.Set("http.reverse_proxy.upstream.latency", duration)
731 + repl.Set("http.reverse_proxy.upstream.latency_ms", duration.Seconds()*1e3) // multiply seconds to preserve decimal (see #4666)
732
733 // update circuit breaker on current conditions
734 if di.Upstream.cb != nil {
@@ -909,6 +914,7 @@ func (h Handler) finalizeResponse(
914
915 // total duration spent proxying, including writing response body
916 repl.Set("http.reverse_proxy.upstream.duration", time.Since(start))
917 + repl.Set("http.reverse_proxy.upstream.duration_ms", time.Since(start).Seconds()*1e3)
918
919 if len(res.Trailer) == announcedTrailers {
920 copyHeader(rw.Header(), res.Trailer)
modules/caddyhttp/server.go
+1
@@ -190,6 +190,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
190 repl.Set("http.response.status", wrec.Status())
191 repl.Set("http.response.size", wrec.Size())
192 repl.Set("http.response.duration", duration)
193 + repl.Set("http.response.duration_ms", duration.Seconds()*1e3) // multiply seconds to preserve decimal (see #4666)
194
195 logger := accLog
196 if s.Logs != nil {