This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
166 lines (148 loc) · 5.37 KB
/
trace.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// package trace provides convenient functionality to trace HTTP requests.
// Main functionality consists of prepending unique token to all logs related to request.
//
// Basic usage: wrap your top-level http.Handler with trace.Handler and use trace.Log* functions
// instead of log.Print*.
// Use trace.Token to retrieve unique token for request (for example to write it in response body/header).
//
// This library was created to help debugging services that handle multiple concurrent requests and
// be able to extract only relevant logs for it.
package trace
import (
"bufio"
"crypto/md5"
"errors"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/context"
)
const requestTokenKey = "_token"
type statusRecorder struct {
http.ResponseWriter
status int
}
func (sr *statusRecorder) WriteHeader(status int) {
sr.status = status
sr.ResponseWriter.WriteHeader(status)
}
func (sr *statusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := sr.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, errors.New("Hijack not supported")
}
func (sr statusRecorder) getStatus() string {
if sr.status == 0 {
sr.status = http.StatusOK
}
return strconv.Itoa(sr.status) + " " + http.StatusText(sr.status)
}
// Handler wraps h, generating new token for it.
// It also logs request beginning and ending.
// gorilla/context.Clear is called after handler is done.
func Handler(h http.Handler) http.Handler {
return context.ClearHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
token := fmt.Sprintf("%x", md5.Sum([]byte(r.URL.String()+r.RemoteAddr+time.Now().String())))
context.Set(r, requestTokenKey, token)
Logln(r, "new request", r.Method, r.URL)
sr := &statusRecorder{ResponseWriter: rw}
start := time.Now()
h.ServeHTTP(sr, r)
Logln(r, "done, status:", sr.getStatus(), "time:", time.Since(start))
}))
}
// NoLogHandler is like Handler but it doesn't do any logging.
func NoLogHandler(h http.Handler) http.Handler {
return context.ClearHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
token := fmt.Sprintf("%x", md5.Sum([]byte(r.URL.String()+r.RemoteAddr+time.Now().String())))
context.Set(r, requestTokenKey, token)
h.ServeHTTP(rw, r)
}))
}
// NoClearHandler is like Handler but it doesn't clear gorilla/context.
func NoClearHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
token := fmt.Sprintf("%x", md5.Sum([]byte(r.URL.String()+r.RemoteAddr+time.Now().String())))
context.Set(r, requestTokenKey, token)
Logln(r, "new request", r.Method, r.URL)
sr := &statusRecorder{ResponseWriter: rw}
start := time.Now()
h.ServeHTTP(sr, r)
Logln(r, "done, status:", sr.getStatus(), "time:", time.Since(start))
})
}
// NoLogClearHandler is like Handler but it doesn't do any logging and doesn't clear gorilla/context.
func NoLogClearHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
token := fmt.Sprintf("%x", md5.Sum([]byte(r.URL.String()+r.RemoteAddr+time.Now().String())))
context.Set(r, requestTokenKey, token)
h.ServeHTTP(rw, r)
})
}
// KVPHandler is like Handler but logs the token as key-value pair.
// This means that instead of
// [timestamp] [token] [message]
// you will see
// [timestamp] request_id=[token] [message]
//
// This format is easier to deal with using log parsing systems, such as Splunk.
func KVPHandler(h http.Handler) http.Handler {
return context.ClearHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
token := fmt.Sprintf("request_id=%x", md5.Sum([]byte(r.URL.String()+r.RemoteAddr+time.Now().String())))
context.Set(r, requestTokenKey, token)
Logln(r, "new request", r.Method, r.URL)
sr := &statusRecorder{ResponseWriter: rw}
start := time.Now()
h.ServeHTTP(sr, r)
Logln(r, "done, status:", sr.getStatus(), "time:", time.Since(start))
}))
}
// Token returns generated token for request or empty string it's not present.
// The returned token is formatted as a key-value pair, e.g.
// "request_id=token". If you need just the token not in KVP form, use
// TokenPlain.
//
// The reason for this to prepend "request_id=" is to match our logging format
// and make log parsing easier.
func Token(r *http.Request) string {
tok := context.Get(r, requestTokenKey)
if toks, ok := tok.(string); ok {
return toks
}
return ""
}
// TokenPlain returns generated token for request or empty string it's not present.
// In case token is not formatted correctly, TokenPlain panics.
func TokenPlain(r *http.Request) string {
tok := context.Get(r, requestTokenKey)
toks, ok := tok.(string)
if !ok {
return ""
}
parts := strings.Split(toks, "=")
if len(parts) != 2 {
panic("trace: malformed request token: " + toks)
}
return parts[1]
}
// Log forwards vals to log.Print and prepends request token
func Log(r *http.Request, vals ...interface{}) {
tok := Token(r)
log.Print(append([]interface{}{tok}, vals...)...)
}
// Logln forwards vals to log.Println and prepends request token
func Logln(r *http.Request, vals ...interface{}) {
tok := Token(r)
log.Println(append([]interface{}{tok}, vals...)...)
}
// Logf forwards f and vals to log.Printf and prepends request token
func Logf(r *http.Request, f string, vals ...interface{}) {
tok := Token(r)
f = "%s " + f
log.Printf(f, append([]interface{}{tok}, vals...)...)
}