-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
87 lines (80 loc) · 2.33 KB
/
Copy pathlog.go
File metadata and controls
87 lines (80 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"fmt"
"log"
"net"
"net/http"
"strings"
"time"
)
// accessLog wraps a handler with one log line per request:
//
// <client-ip> <method> <uri> <status> <duration> "<user-agent>"
//
// The client IP is the real caller behind a proxy/ingress (see clientIP).
// Status is "-" when the connection was hijacked (e.g. the drop endpoint),
// since no status line is written in that case.
func accessLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Health probes are frequent and uninteresting; don't log them.
if r.URL.Path == healthzPath {
next.ServeHTTP(w, r)
return
}
start := time.Now()
lw := &logWriter{ResponseWriter: w}
next.ServeHTTP(lw, r)
status := "-"
if lw.status != 0 {
status = fmt.Sprintf("%d", lw.status)
}
log.Printf("%s %s %s %s %s %q",
clientIP(r), r.Method, r.URL.RequestURI(), status,
time.Since(start).Round(time.Millisecond), r.UserAgent())
})
}
// clientIP returns the originating client address, preferring proxy headers set
// by an ingress/load balancer over the direct peer (which would otherwise be the
// proxy itself). Order: leftmost X-Forwarded-For, then X-Real-IP, then the
// connection peer with its port stripped.
func clientIP(r *http.Request) string {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
if i := strings.IndexByte(xff, ','); i >= 0 {
xff = xff[:i]
}
if ip := strings.TrimSpace(xff); ip != "" {
return ip
}
}
if xr := strings.TrimSpace(r.Header.Get("X-Real-IP")); xr != "" {
return xr
}
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
return host
}
return r.RemoteAddr
}
// logWriter records the response status code while delegating everything else,
// including hijacking, so it stays transparent to the drop endpoint.
type logWriter struct {
http.ResponseWriter
status int
}
func (l *logWriter) WriteHeader(code int) {
l.status = code
l.ResponseWriter.WriteHeader(code)
}
func (l *logWriter) Write(b []byte) (int, error) {
if l.status == 0 {
l.status = http.StatusOK
}
return l.ResponseWriter.Write(b)
}
func (l *logWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj, ok := l.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("underlying ResponseWriter does not support hijacking")
}
return hj.Hijack()
}