Skip to content

Instantly share code, notes, and snippets.

@amsokol
Created September 16, 2018 07:39
Show Gist options
  • Save amsokol/ed20b0e79cb02c41945575071f64ef94 to your computer and use it in GitHub Desktop.
Save amsokol/ed20b0e79cb02c41945575071f64ef94 to your computer and use it in GitHub Desktop.
package middleware
import (
"net/http"
"strings"
"time"
"go.uber.org/zap"
)
// AddLogger logs request/response pair
func AddLogger(logger *zap.Logger, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// We do not want to be spammed by Kubernetes health check.
// Do not log Kubernetes health check.
// You can change this behavior as you wish.
if r.Header.Get("X-Liveness-Probe") == "Healthz" {
h.ServeHTTP(w, r)
return
}
id := GetReqID(ctx)
// Prepare fields to log
var scheme string
if r.TLS != nil {
scheme = "https"
} else {
scheme = "http"
}
proto := r.Proto
method := r.Method
remoteAddr := r.RemoteAddr
userAgent := r.UserAgent()
uri := strings.Join([]string{scheme, "://", r.Host, r.RequestURI}, "")
// Log HTTP request
logger.Debug("request started",
zap.String("request-id", id),
zap.String("http-scheme", scheme),
zap.String("http-proto", proto),
zap.String("http-method", method),
zap.String("remote-addr", remoteAddr),
zap.String("user-agent", userAgent),
zap.String("uri", uri),
)
t1 := time.Now()
h.ServeHTTP(w, r)
// Log HTTP response
logger.Debug("request completed",
zap.String("request-id", id),
zap.String("http-scheme", scheme),
zap.String("http-proto", proto),
zap.String("http-method", method),
zap.String("remote-addr", remoteAddr),
zap.String("user-agent", userAgent),
zap.String("uri", uri),
zap.Float64("elapsed-ms", float64(time.Since(t1).Nanoseconds())/1000000.0),
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment