-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlation.go
167 lines (140 loc) · 4.41 KB
/
correlation.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
167
package correlation
import (
"context"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/lucsky/cuid"
)
const (
// UUID generate a random UUID as correlation id.
UUID correlationType = iota + 1
// CUID generate a CUID as correlation id.
CUID
// Random generate a pseudo random Int64 as correlation id.
Random
// Custom pass a custom string as correlation id.
Custom
// Time use the elapsed nanoseconds since the Epoch as correlation id.
Time
ctxCorrelationHeaderKey = correlationCtxKey("CorrelationResponseHeader")
correlationIDHeader = "X-Correlation-ID"
)
type correlationCtxKey string
type correlationType int
//Options is a struct for specifying configuration options.
type Options struct {
// CorrelationHeaderName the name of the header to be used as correlation id. Defaults to `X-Correlation-ID`.
CorrelationHeaderName string
// CorrelationIDType the type of correlation id to generate. Defaults to `correlation.UUID`.
CorrelationIDType correlationType
// CorrelationCustomString the value to use when using a custom correlation id. Default is empty.
CorrelationCustomString string
}
// Correlation is a middleware that adds correlation ids to requests. A correlation.Options struct can
// be provided t override the default configuration values.
type Correlation struct {
opt Options
rand rand.Source
}
//New returns a new correlation struct with the provides options.
func New(opt Options) *Correlation {
return &Correlation{
opt: opt,
rand: rand.NewSource(time.Now().UnixNano()),
}
}
//Handler for integration with net/http.
func (c *Correlation) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := c.Process(w, r)
if err != nil {
return
}
h.ServeHTTP(w, r)
})
}
// HandlerForRequestOnly for integration with net/http.
func (c *Correlation) HandlerForRequestOnly(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := c.processRequest(w, r)
if err != nil {
return
}
ctx := context.WithValue(r.Context(), ctxCorrelationHeaderKey, response)
h.ServeHTTP(w, r.WithContext(ctx))
})
}
// HandlerFuncWithNext for integration with github.com/urfave/negroni.
func (c *Correlation) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
err := c.Process(w, r)
if err == nil && next != nil {
next(w, r)
}
}
// HandlerFuncWithNextForRequestOnly for integration with github.com/urfave/negroni.
func (c *Correlation) HandlerFuncWithNextForRequestOnly(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
response, err := c.processRequest(w, r)
if err == nil && next != nil {
ctx := context.WithValue(r.Context(), ctxCorrelationHeaderKey, response)
next(w, r.WithContext(ctx))
}
}
//Process processes the incoming request.
func (c *Correlation) Process(w http.ResponseWriter, r *http.Request) error {
response, err := c.processRequest(w, r)
if response != nil {
for key, values := range response {
for _, value := range values {
w.Header().Set(key, value)
}
}
}
return err
}
func (c *Correlation) processRequest(w http.ResponseWriter, r *http.Request) (http.Header, error) {
response := make(http.Header)
value := func(c *Correlation) string {
switch c.opt.CorrelationIDType {
case UUID:
return uuid.New().String()
case CUID:
return cuid.New()
case Random:
return strconv.FormatInt(c.rand.Int63(), 10)
case Custom:
return c.opt.CorrelationCustomString
case Time:
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
return uuid.New().String()
}
correlationHeader := func(c *Correlation) string {
if len(c.opt.CorrelationHeaderName) > 0 {
return c.opt.CorrelationHeaderName
}
return correlationIDHeader
}
if len(r.Header.Get(correlationHeader(c))) == 0 {
r.Header.Set(correlationHeader(c), value(c))
}
response.Set(correlationHeader(c), r.Header.Get(correlationHeader(c)))
return response, nil
}
// ModifyResponseHeaders modifies the response for integration with net/http/httputil ReverseProxy.
func (c *Correlation) ModifyResponseHeaders(res *http.Response) error {
if res != nil && res.Request != nil {
response := res.Request.Context().Value(ctxCorrelationHeaderKey)
if response != nil {
for h, v := range response.(http.Header) {
if len(v) > 0 {
res.Header.Set(h, strings.Join(v, ","))
}
}
}
}
return nil
}