]> go.fuhry.dev Git - runtime.git/commitdiff
utils/log: record body size, request duration, initialize status to 200
authorDan Fuhry <dan@fuhry.com>
Tue, 31 Dec 2024 01:34:19 +0000 (20:34 -0500)
committerDan Fuhry <dan@fuhry.com>
Tue, 31 Dec 2024 01:34:19 +0000 (20:34 -0500)
utils/log/http.go

index de9330aefcc14bce02e4d935eed3f12e233267f0..fe8a2ff062f8dc2c4e81d6970c629fb52ab06ee1 100644 (file)
@@ -7,6 +7,7 @@ import (
        "net/http"
        "net/textproto"
        "strings"
+       "time"
 )
 
 const (
@@ -16,17 +17,20 @@ const (
        kPath          = "path"
        kUserAgent     = "user_agent"
        kStatusCode    = "status"
+       kTimeTaken     = "duration_ms"
+       kBytesWritten  = "response_size"
 )
 
 type statusRecorder struct {
        http.ResponseWriter
        http.Hijacker
 
-       Status int
+       Status       int
+       BytesWritten uint
 }
 
 type LoggingMiddleware struct {
-       Logger    Logger
+       Logger    coreLogger
        ExtraFunc func(map[string]any)
 
        extraRequestHeaders  []string
@@ -68,12 +72,15 @@ func (lm *LoggingMiddleware) HandlerFunc() http.HandlerFunc {
 func (lm *LoggingMiddleware) handle(w http.ResponseWriter, r *http.Request) {
        ws := &statusRecorder{
                ResponseWriter: w,
+               Status:         http.StatusOK,
        }
        if h, ok := w.(http.Hijacker); ok {
                ws.Hijacker = h
        }
 
+       startTime := time.Now().UnixMilli()
        lm.h.ServeHTTP(ws, r)
+       respTime := time.Now().UnixMilli() - startTime
 
        entry := map[string]any{
                kRemoteAddress: r.RemoteAddr,
@@ -81,15 +88,17 @@ func (lm *LoggingMiddleware) handle(w http.ResponseWriter, r *http.Request) {
                kMethod:        r.Method,
                kPath:          r.URL.Path,
                kStatusCode:    ws.Status,
+               kTimeTaken:     respTime,
+               kBytesWritten:  ws.BytesWritten,
        }
 
        for _, h := range lm.extraRequestHeaders {
-               hKey := strings.ReplaceAll("-", "_", h)
+               hKey := strings.ReplaceAll(h, "-", "_")
                entry[hKey] = r.Header.Get(h)
        }
        for _, h := range lm.extraResponseHeaders {
-               hKey := strings.ReplaceAll("-", "_", h)
-               entry[hKey] = r.Header.Get(h)
+               hKey := strings.ReplaceAll(h, "-", "_")
+               entry[hKey] = w.Header().Get(h)
        }
        if lm.ExtraFunc != nil {
                lm.ExtraFunc(entry)
@@ -107,6 +116,14 @@ func (r *statusRecorder) WriteHeader(status int) {
 }
 
 func (r *statusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
-       r.Status = http.StatusSwitchingProtocols
+       if r.Status == 0 {
+               r.Status = http.StatusSwitchingProtocols
+       }
        return r.Hijacker.Hijack()
 }
+
+func (r *statusRecorder) Write(buf []byte) (int, error) {
+       n, err := r.ResponseWriter.Write(buf)
+       r.BytesWritten += uint(n)
+       return n, err
+}