-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoutgoing.go
171 lines (148 loc) · 3.97 KB
/
outgoing.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
168
169
170
171
package apitoolkit
import (
"bytes"
"context"
"io"
"net/http"
"github.com/google/uuid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
type roundTripper struct {
base http.RoundTripper
ctx context.Context
cfg *roundTripperConfig
}
func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
defer func() {
if err != nil {
ReportError(rt.ctx, err)
}
}()
tracer := otel.GetTracerProvider().Tracer("")
_, span := tracer.Start(rt.ctx, "apitoolkit-http-span", trace.WithSpanKind(trace.SpanKindClient))
// Capture the request body
reqBodyBytes := []byte{}
if req.Body != nil {
reqBodyBytes, _ = io.ReadAll(req.Body)
req.Body = io.NopCloser(bytes.NewBuffer(reqBodyBytes))
}
// Add a header to all outgoing requests "X-APITOOLKIT-TRACE-PARENT-ID"
res, err = rt.base.RoundTrip(req)
var errorList []ATError
if err != nil {
// Add the error for the given request payload
errorList = append(errorList, BuildError(err))
}
var payload Payload
var parentMsgIDPtr *uuid.UUID
parentMsgID, ok := rt.ctx.Value(CurrentRequestMessageID).(uuid.UUID)
if ok {
parentMsgIDPtr = &parentMsgID
}
// Capture the response body
conf := roundTripperConfigToConfig(rt.cfg)
if res != nil {
respBodyBytes, _ := io.ReadAll(res.Body)
res.Body = io.NopCloser(bytes.NewBuffer(respBodyBytes))
payload = BuildPayload(
GoOutgoing,
req, res.StatusCode, reqBodyBytes,
respBodyBytes, res.Header, nil,
req.URL.Path,
rt.cfg.RedactHeaders, rt.cfg.RedactRequestBody, rt.cfg.RedactResponseBody,
errorList,
uuid.Nil,
parentMsgIDPtr,
conf,
)
CreateSpan(payload, conf, span)
} else {
payload = BuildPayload(
GoOutgoing,
req, 503, reqBodyBytes,
nil, nil, nil,
req.URL.Path,
rt.cfg.RedactHeaders, rt.cfg.RedactRequestBody, rt.cfg.RedactResponseBody,
errorList,
uuid.Nil,
parentMsgIDPtr,
conf,
)
CreateSpan(payload, conf, span)
}
return res, err
}
func HTTPClient(ctx context.Context, opts ...RoundTripperOption) *http.Client {
// Run the roundTripperConfig to extract out a httpClient Transport
cfg := new(roundTripperConfig)
for _, opt := range opts {
opt(cfg)
}
httpClientV := *http.DefaultClient
httpClient := &httpClientV
if cfg.HTTPClient != nil {
// Use httpClient supplied by user.
v := *cfg.HTTPClient
httpClient = &v
}
httpClient.Transport = WrapRoundTripper(
ctx, httpClient.Transport,
opts...,
)
return httpClient
}
type roundTripperConfig struct {
HTTPClient *http.Client
RedactHeaders []string
RedactRequestBody []string
RedactResponseBody []string
}
type RoundTripperOption func(*roundTripperConfig)
// WithHTTPClient allows you supply your own custom http client
func WithHTTPClient(httpClient *http.Client) RoundTripperOption {
return func(rc *roundTripperConfig) {
rc.HTTPClient = httpClient
}
}
func WithRedactHeaders(headers ...string) RoundTripperOption {
return func(rc *roundTripperConfig) {
rc.RedactHeaders = headers
}
}
func WithRedactRequestBody(fields ...string) RoundTripperOption {
return func(rc *roundTripperConfig) {
rc.RedactRequestBody = fields
}
}
func WithRedactResponseBody(fields ...string) RoundTripperOption {
return func(rc *roundTripperConfig) {
rc.RedactResponseBody = fields
}
}
// WrapRoundTripper returns a new RoundTripper which traces all requests sent
// over the transport.
func WrapRoundTripper(ctx context.Context, rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper {
cfg := new(roundTripperConfig)
for _, opt := range opts {
opt(cfg)
}
// If no rt is passed in, then use the default standard library transport
if rt == nil {
rt = http.DefaultTransport
}
return &roundTripper{
base: rt,
ctx: ctx,
cfg: cfg,
}
}
func roundTripperConfigToConfig(cfg *roundTripperConfig) Config {
return Config{
RedactHeaders: cfg.RedactHeaders,
RedactRequestBody: cfg.RedactRequestBody,
RedactResponseBody: cfg.RedactResponseBody,
CaptureRequestBody: true,
CaptureResponseBody: true,
}
}