forked from golang/gddo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_handler.go
59 lines (47 loc) · 1.31 KB
/
http_handler.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
package log
import (
"encoding/hex"
"math/rand"
"net/http"
"time"
"github.com/inconshreveable/log15"
)
const (
gaeRequestIDHeader = "X-AppEngine-Request-Log-Id"
)
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
type httpContextHandler struct {
log log15.Logger
next http.Handler
onAppEngine bool
}
// NewHTTPContextHandler adds a context logger based on the given logger to
// each request. After a request passes through this handler,
// Error(req.Context(), "foo") will log to that logger and add useful context
// to each log entry.
func NewHTTPContextHandler(h http.Handler, l log15.Logger, onAppEngine bool) http.Handler {
if l == nil {
l = log15.Root()
}
return &httpContextHandler{
log: l,
next: h,
onAppEngine: onAppEngine,
}
}
func (h *httpContextHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// We will accept an App Engine Request Header. If there isn't one, we will
// fallback to 16 random bytes (hex encoded).
reqID := r.Header.Get(gaeRequestIDHeader)
if !h.onAppEngine || reqID == "" {
buf := make([]byte, 16)
random.Read(buf)
reqID = hex.EncodeToString(buf)
}
requestLogger := h.log.New(log15.Ctx{
"request_id": reqID,
})
r = r.WithContext(NewContext(ctx, requestLogger))
h.next.ServeHTTP(w, r)
}