Fix logging: add response_bytes to middleware, cache_key to handler
- Middleware now tracks and logs bytes written via response_bytes - Handler logs cache_key for cache hit debugging - Changed "served encrypted image" to "image served" (only URL is encrypted)
This commit is contained in:
@@ -91,7 +91,8 @@ func (s *Handlers) HandleImageEnc() http.HandlerFunc {
|
|||||||
|
|
||||||
// Log completion
|
// Log completion
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
s.log.Info("served encrypted image",
|
s.log.Info("image served",
|
||||||
|
"cache_key", imgcache.CacheKey(req),
|
||||||
"host", req.SourceHost,
|
"host", req.SourceHost,
|
||||||
"path", req.SourcePath,
|
"path", req.SourcePath,
|
||||||
"format", req.Format,
|
"format", req.Format,
|
||||||
|
|||||||
@@ -58,11 +58,12 @@ func ipFromHostPort(hp string) string {
|
|||||||
|
|
||||||
type loggingResponseWriter struct {
|
type loggingResponseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
statusCode int
|
statusCode int
|
||||||
|
bytesWritten int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
func newLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||||
return &loggingResponseWriter{w, http.StatusOK}
|
return &loggingResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lrw *loggingResponseWriter) WriteHeader(code int) {
|
func (lrw *loggingResponseWriter) WriteHeader(code int) {
|
||||||
@@ -70,6 +71,13 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) {
|
|||||||
lrw.ResponseWriter.WriteHeader(code)
|
lrw.ResponseWriter.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (lrw *loggingResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
n, err := lrw.ResponseWriter.Write(b)
|
||||||
|
lrw.bytesWritten += int64(n)
|
||||||
|
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
// Logging returns a logging middleware.
|
// Logging returns a logging middleware.
|
||||||
func (s *Middleware) Logging() func(http.Handler) http.Handler {
|
func (s *Middleware) Logging() func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
@@ -90,6 +98,7 @@ func (s *Middleware) Logging() func(http.Handler) http.Handler {
|
|||||||
"proto", r.Proto,
|
"proto", r.Proto,
|
||||||
"remoteIP", ipFromHostPort(r.RemoteAddr),
|
"remoteIP", ipFromHostPort(r.RemoteAddr),
|
||||||
"status", lrw.statusCode,
|
"status", lrw.statusCode,
|
||||||
|
"response_bytes", lrw.bytesWritten,
|
||||||
"latency_ms", latency.Milliseconds(),
|
"latency_ms", latency.Milliseconds(),
|
||||||
)
|
)
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user