-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
148 lines (128 loc) · 3.64 KB
/
logging.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bufio"
"log"
"net"
"net/http"
"os"
"text/template"
"time"
"github.com/google/uuid"
)
type incomingTmplValues struct {
TraceID string
Method string
URL string
Protocol string
Accept string
Host string
ContentType string
}
var incomingTmpl = template.Must(template.New("").Parse(`
Incoming Request: {{ .TraceID }}
{{ .Method }} {{ .URL }} {{ .Protocol }}
Accept: {{ .Accept }}
Host: {{ .Host }}
Content-Type: {{ .ContentType }}
`))
func fromRequest(r *http.Request) *incomingTmplValues {
return &incomingTmplValues{
TraceID: r.Header.Get("Traceparent"),
Method: r.Method,
URL: r.URL.String(),
Protocol: r.Proto,
Accept: r.Header.Get("Accept"),
Host: r.Host,
ContentType: r.Header.Get("Content-Type"),
}
}
func logRequest(r *http.Request) {
err := incomingTmpl.Execute(os.Stdout, fromRequest(r))
if err != nil {
log.Printf("Error executing template: %v", err)
return
}
}
var _ http.ResponseWriter = (*ResponseWriterWrapper)(nil)
var _ http.Flusher = (*ResponseWriterWrapper)(nil)
var _ http.Hijacker = (*ResponseWriterWrapper)(nil)
type ResponseWriterWrapper struct {
w *http.ResponseWriter
f *http.Flusher
h *http.Hijacker
statusCode *int
}
func (rww ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return (*rww.h).Hijack()
}
func (rww ResponseWriterWrapper) Flush() {
(*rww.f).Flush()
}
func (rww ResponseWriterWrapper) Header() http.Header {
return (*rww.w).Header()
}
func (rww ResponseWriterWrapper) Write(bytes []byte) (int, error) {
return (*rww.w).Write(bytes)
}
// WriteHeader function overwrites the http.ResponseWriter WriteHeader() function
func (rww ResponseWriterWrapper) WriteHeader(statusCode int) {
*rww.statusCode = statusCode
(*rww.w).WriteHeader(statusCode)
}
// NewResponseWriterWrapper static function creates a wrapper for the http.ResponseWriter
func NewResponseWriterWrapper(w http.ResponseWriter) ResponseWriterWrapper {
var statusCode int = 200
// Every request should implement flusher
flusher, _ := w.(http.Flusher)
// Every request should implement hijacker
hijacker, _ := w.(http.Hijacker)
return ResponseWriterWrapper{
w: &w,
h: &hijacker,
f: &flusher,
statusCode: &statusCode,
}
}
type outgoingTmplValues struct {
TraceID string
Duration string
Protocol string
StatusCode int
ContentType string
}
var outgoingTmpl = template.Must(template.New("").Parse(`
Outgoing Response: {{ .TraceID }}
Duration: {{ .Duration }}
{{ .Protocol }} {{ .StatusCode }}
Content-Type: {{ .ContentType }}
`))
func fromResponse(w *ResponseWriterWrapper, r *http.Request, duration time.Duration) *outgoingTmplValues {
return &outgoingTmplValues{
TraceID: (*w.w).Header().Get("Traceparent"),
Duration: duration.String(), // To seconds
Protocol: r.Proto,
StatusCode: *w.statusCode,
ContentType: (*w.w).Header().Get("Content-Type"),
}
}
func logResponse(w *ResponseWriterWrapper, r *http.Request, duration time.Duration) {
err := outgoingTmpl.Execute(os.Stdout, fromResponse(w, r, duration))
if err != nil {
log.Printf("Error executing template: %v", err)
return
}
}
func HTTPLogging(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Generate a unique trace ID for the request
traceID := uuid.New().String()
// TODO: Actually add open-telemetry compatibility
r.Header.Set("Traceparent", traceID)
logRequest(r)
rww := NewResponseWriterWrapper(w)
start := time.Now()
next(rww, r)
duration := time.Since(start)
logResponse(&rww, r, duration)
})
}