mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-05 18:44:58 +03:00
Latency now available with recorder and replacer
This commit is contained in:
parent
51139a5f56
commit
95dce5cdfc
2 changed files with 9 additions and 3 deletions
|
@ -1,6 +1,9 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// responseRecorder is a type of ResponseWriter that captures
|
// responseRecorder is a type of ResponseWriter that captures
|
||||||
// the status code written to it and also the size of the body
|
// the status code written to it and also the size of the body
|
||||||
|
@ -12,6 +15,7 @@ type responseRecorder struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
status int
|
status int
|
||||||
size int
|
size int
|
||||||
|
start time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResponseRecorder makes and returns a new responseRecorder,
|
// NewResponseRecorder makes and returns a new responseRecorder,
|
||||||
|
@ -24,6 +28,7 @@ func NewResponseRecorder(w http.ResponseWriter) *responseRecorder {
|
||||||
return &responseRecorder{
|
return &responseRecorder{
|
||||||
ResponseWriter: w,
|
ResponseWriter: w,
|
||||||
status: http.StatusOK,
|
status: http.StatusOK,
|
||||||
|
start: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,9 @@ func NewReplacer(r *http.Request, rr *responseRecorder) replacer {
|
||||||
"{when}": func() string {
|
"{when}": func() string {
|
||||||
return time.Now().Format(timeFormat)
|
return time.Now().Format(timeFormat)
|
||||||
}(),
|
}(),
|
||||||
"{status}": strconv.Itoa(rr.status),
|
"{status}": strconv.Itoa(rr.status),
|
||||||
"{size}": strconv.Itoa(rr.size),
|
"{size}": strconv.Itoa(rr.size),
|
||||||
|
"{latency}": time.Since(rr.start).String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header placeholders
|
// Header placeholders
|
||||||
|
|
Loading…
Reference in a new issue