-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_tracing.go
139 lines (114 loc) · 4.05 KB
/
request_tracing.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
package httptrace
import (
"io"
"net/http"
"context"
"github.com/m4rw3r/uuid"
"os"
)
type ctxRequestIDKeyType string // This avoids key collisions with clients
const (
ctxRequestIDKey ctxRequestIDKeyType = "request-id"
headerRequestIDKey string = "X-Request-ID"
headerUniqueIDKey string = "X-Unique-ID"
)
var userAgent string
// init reads the 'SERVICE_NAME' environment variable so that if it is not set manually, we have something meaningful
func init() {
userAgent = os.Getenv(`SERVICE_NAME`)
}
/* We need a getter for the RequestID in this package, because context.Context.Value is also considering the type when
fetching values, and since the request-id is in a const, it is tied to this package. */
// GetRequestID gets the RequestID from a context.context
func GetRequestID(ctx context.Context) string {
if ctx.Value(ctxRequestIDKey) == nil {
return ""
}
return ctx.Value(ctxRequestIDKey).(string)
}
// ContextWithRequestID lets us generate a new context that contains a request ID. We use this for services which
// are not entirely REST-ful but still want to propagate a request ID.
func ContextWithRequestID(requestID string) context.Context {
return context.WithValue(context.Background(), ctxRequestIDKey, requestID)
}
func SetGlobalUserAgent(ua string) {
userAgent = ua
}
func TracingMiddleware(h http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
requestWithContext := r.WithContext(context.WithValue(r.Context(), ctxRequestIDKey, getRequestID(r)))
defer func() {
if requestID := requestWithContext.Context().Value(ctxRequestIDKey); requestID != nil {
if requestIDStr, ok := requestID.(string); ok {
setRequestID(w, headerRequestIDKey, requestIDStr)
setRequestID(w, headerUniqueIDKey, requestIDStr)
}
}
}()
h(w, requestWithContext)
},
)
}
func getRequestID(req *http.Request) string {
requestID := req.Header.Get(headerRequestIDKey)
if requestID == "" {
uuidValue, _ := uuid.V4()
requestID = uuidValue.String()
}
return requestID
}
func setRequestID(w http.ResponseWriter, headerName string, headerValue string) {
w.Header().Set(headerName, headerValue)
}
// HTTPClient an client implementing this interface can be used by the tracer HTTP client
type HTTPClient interface {
Do(req *http.Request) (resp *http.Response, err error)
}
type TracingHTTPClient struct {
HTTPClient
}
func (c *TracingHTTPClient) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
if userAgent != "" {
req.Header.Set("User-Agent", userAgent)
}
if requestID := ctx.Value(ctxRequestIDKey); requestID != nil {
if requestIDStr, ok := requestID.(string); ok {
req.Header.Set(headerRequestIDKey, requestIDStr)
}
}
return c.HTTPClient.Do(req)
}
func (c *TracingHTTPClient) Get(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return c.Do(ctx, req)
}
func (c *TracingHTTPClient) Post(ctx context.Context, url string, bodyType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", bodyType)
return c.Do(ctx, req)
}
func (c *TracingHTTPClient) Put(ctx context.Context, url string, bodyType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", bodyType)
return c.Do(ctx, req)
}
var DefaultTracingHTTPClient = TracingHTTPClient{http.DefaultClient}
func Get(ctx context.Context, url string) (*http.Response, error) {
return DefaultTracingHTTPClient.Get(ctx, url)
}
func Post(ctx context.Context, url string, bodyType string, body io.Reader) (*http.Response, error) {
return DefaultTracingHTTPClient.Post(ctx, url, bodyType, body)
}
func Put(ctx context.Context, url string, bodyType string, body io.Reader) (*http.Response, error) {
return DefaultTracingHTTPClient.Put(ctx, url, bodyType, body)
}