-
Notifications
You must be signed in to change notification settings - Fork 14
/
corerequestcontext.go
146 lines (113 loc) · 4.2 KB
/
corerequestcontext.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
package common
import (
"context"
"net/http"
"github.com/anz-bank/sysl-go/common/internal"
"github.com/sirupsen/logrus"
)
func GetLogEntryFromContext(ctx context.Context) *logrus.Entry {
return getCoreContext(ctx).entry
}
func GetLoggerFromContext(ctx context.Context) *logrus.Logger {
return getCoreContext(ctx).logger
}
func NewLoggingRoundTripper(name string, base http.RoundTripper) http.RoundTripper {
// temporary pass-through to get the real round tripper from the request context
return &tempRoundtripper{name, base}
}
type coreRequestContext struct {
logger *logrus.Logger
entry *logrus.Entry
}
type reqHeaderContext struct {
header http.Header
}
type respHeaderAndStatusContext struct {
header http.Header
status int
}
// LoggerToContext create a new context containing the logger
func LoggerToContext(ctx context.Context, logger *logrus.Logger, entry *logrus.Entry) context.Context {
return context.WithValue(ctx, coreRequestContextKey{}, &coreRequestContext{logger, entry})
}
// RequestHeaderToContext create a new context containing the request header
func RequestHeaderToContext(ctx context.Context, header http.Header) context.Context {
return context.WithValue(ctx, reqHeaderContextKey{}, &reqHeaderContext{header})
}
// RequestHeaderFromContext retrieve the request header from the context
func RequestHeaderFromContext(ctx context.Context) http.Header {
reqHeader := getReqHeaderContext(ctx)
if reqHeader == nil {
return nil
}
return reqHeader.header
}
// RespHeaderAndStatusToContext create a new context containing the response header and status
func RespHeaderAndStatusToContext(ctx context.Context, header http.Header, status int) context.Context {
return context.WithValue(ctx, respHeaderAndStatusContextKey{}, &respHeaderAndStatusContext{header, status})
}
// RespHeaderAndStatusFromContext retrieve response header and status from the context
func RespHeaderAndStatusFromContext(ctx context.Context) (header http.Header, status int) {
respHeaderAndStatus := getRespHeaderAndStatusContext(ctx)
if respHeaderAndStatus == nil {
return nil, http.StatusOK
}
header = respHeaderAndStatus.header
status = respHeaderAndStatus.status
return
}
func UpdateResponseStatus(ctx context.Context, status int) error {
respHeaderAndStatus := getRespHeaderAndStatusContext(ctx)
if respHeaderAndStatus == nil {
return CreateError(ctx, InternalError, "response status not in context", nil)
}
respHeaderAndStatus.status = status
return nil
}
func CoreRequestContextMiddleware(logger *logrus.Logger) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
entry := logger.WithField(traceIDLogField, GetTraceIDFromContext(ctx))
ctx = LoggerToContext(r.Context(), logger, entry)
ctx = internal.AddResponseBodyMonitorToContext(ctx)
defer internal.CheckForUnclosedResponses(ctx, entry)
reqLogger, entry := internal.NewRequestLogger(entry, r)
w = reqLogger.ResponseWriter(w)
defer reqLogger.FlushLog()
ctx = LoggerToContext(ctx, logger, entry)
r = r.WithContext(ctx)
tl := internal.NewRequestTimer(w, r)
w = tl.RespWrapper
defer tl.Log(entry)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}
type coreRequestContextKey struct{}
func getCoreContext(ctx context.Context) *coreRequestContext {
return ctx.Value(coreRequestContextKey{}).(*coreRequestContext)
}
type reqHeaderContextKey struct{}
type respHeaderAndStatusContextKey struct{}
func getReqHeaderContext(ctx context.Context) *reqHeaderContext {
if ctx.Value(reqHeaderContextKey{}) == nil {
return nil
}
return ctx.Value(reqHeaderContextKey{}).(*reqHeaderContext)
}
func getRespHeaderAndStatusContext(ctx context.Context) *respHeaderAndStatusContext {
if ctx.Value(respHeaderAndStatusContextKey{}) == nil {
return nil
}
return ctx.Value(respHeaderAndStatusContextKey{}).(*respHeaderAndStatusContext)
}
type tempRoundtripper struct {
name string
base http.RoundTripper
}
func (t *tempRoundtripper) RoundTrip(r *http.Request) (*http.Response, error) {
logentry := GetLogEntryFromContext(r.Context()).WithField("Downsteam", t.name)
return internal.NewLoggingRoundTripper(logentry, t.base).RoundTrip(r)
}