-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_tracer.go
309 lines (284 loc) · 7.78 KB
/
client_tracer.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package xrayhttp
import (
"context"
"crypto/tls"
"fmt"
"net/http/httptrace"
"sync"
"github.com/shogo82148/aws-xray-yasdk-go/xray"
)
type httpSubsegments struct {
mu sync.Mutex
ctx context.Context
connCtx context.Context
connSeg *xray.Segment
dnsCtx context.Context
dnsSeg *xray.Segment
dialCtx context.Context
dialSeg *xray.Segment
tlsCtx context.Context
tlsSeg *xray.Segment
reqCtx context.Context
reqSeg *xray.Segment
}
func (segs *httpSubsegments) GetConn(hostPort string) {
segs.mu.Lock()
defer segs.mu.Unlock()
segs.connCtx, segs.connSeg = xray.BeginSubsegment(segs.ctx, "connect")
}
func (segs *httpSubsegments) GotConn(info httptrace.GotConnInfo) {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.connCtx != nil {
segs.connSeg.Close()
segs.connCtx, segs.connSeg = nil, nil
}
segs.reqCtx, segs.reqSeg = xray.BeginSubsegment(segs.ctx, "request")
}
func (segs *httpSubsegments) DNSStart(info httptrace.DNSStartInfo) {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.connCtx == nil {
return
}
segs.dnsCtx, segs.dnsSeg = xray.BeginSubsegment(segs.connCtx, "dns")
}
func (segs *httpSubsegments) DNSDone(info httptrace.DNSDoneInfo) {
type dnsDoneInfo struct {
Addresses []string `json:"addresses"`
Coalesced bool `json:"coalesced"`
}
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.dnsCtx == nil {
return
}
addresses := make([]string, 0, len(info.Addrs))
for _, addr := range info.Addrs {
addresses = append(addresses, addr.String())
}
meta := dnsDoneInfo{
Addresses: addresses,
Coalesced: info.Coalesced,
}
segs.dnsSeg.AddMetadataToNamespace("http", "dns", meta)
segs.dnsSeg.AddError(info.Err)
segs.dnsSeg.Close()
segs.dnsCtx, segs.dnsSeg = nil, nil
if info.Err != nil && segs.connCtx != nil {
segs.connSeg.SetFault()
segs.connSeg.Close()
segs.connCtx, segs.connSeg = nil, nil
}
}
func (segs *httpSubsegments) ConnectStart(network, addr string) {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.connCtx == nil {
return
}
segs.dialCtx, segs.dialSeg = xray.BeginSubsegment(segs.connCtx, "dial")
}
func (segs *httpSubsegments) ConnectDone(network, addr string, err error) {
type dialInfo struct {
Network string `json:"network"`
Address string `json:"address"`
}
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.dialCtx == nil {
return
}
segs.dialSeg.AddMetadataToNamespace("http", "dial", dialInfo{
Network: network,
Address: addr,
})
segs.dialSeg.AddError(err)
segs.dialSeg.Close()
segs.dialCtx, segs.dialSeg = nil, nil
if err != nil && segs.connCtx != nil {
segs.connSeg.SetFault()
segs.connSeg.Close()
segs.connCtx, segs.connSeg = nil, nil
}
}
func (segs *httpSubsegments) TLSHandshakeStart() {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.connCtx == nil {
return
}
segs.tlsCtx, segs.tlsSeg = xray.BeginSubsegment(segs.connCtx, "tls")
}
func (segs *httpSubsegments) TLSHandshakeDone(state tls.ConnectionState, err error) {
type tlsInfo struct {
Version string `json:"version,omitempty"`
DidResume bool `json:"did_resume,omitempty"`
NegotiatedProtocol string `json:"negotiated_protocol,omitempty"`
NegotiatedProtocolIsMutual bool `json:"negotiated_protocol_is_mutual,omitempty"`
CipherSuite string `json:"cipher_suite,omitempty"`
}
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.tlsCtx == nil {
return
}
if !segs.tlsSeg.AddError(err) {
segs.tlsSeg.AddMetadataToNamespace("http", "tls", tlsInfo{
Version: tlsVersionName(state.Version),
DidResume: state.DidResume,
NegotiatedProtocol: state.NegotiatedProtocol,
NegotiatedProtocolIsMutual: state.NegotiatedProtocolIsMutual,
CipherSuite: cipherSuiteName(state.CipherSuite),
})
}
segs.tlsSeg.Close()
segs.tlsCtx, segs.tlsSeg = nil, nil
if err != nil && segs.connCtx != nil {
segs.connSeg.SetFault()
segs.connSeg.Close()
segs.connCtx, segs.connSeg = nil, nil
}
}
func tlsVersionName(version uint16) string {
switch version {
case tls.VersionSSL30:
return "ssl3.0"
case tls.VersionTLS10:
return "tls1.0"
case tls.VersionTLS11:
return "tls1.1"
case tls.VersionTLS12:
return "tls1.2"
case 0x0304: // VersionTLS13 is supported from Go 1.13
return "tls1.3"
}
// fallback to hex format
return fmt.Sprintf("0x%04X", version)
}
// naive implementation of tls.CipherSuiteName from Go 1.14
func cipherSuiteName(id uint16) string {
switch id {
case 0x0005:
return "TLS_RSA_WITH_RC4_128_SHA"
case 0x000a:
return "TLS_RSA_WITH_3DES_EDE_CBC_SHA"
case 0x002f:
return "TLS_RSA_WITH_AES_128_CBC_SHA"
case 0x0035:
return "TLS_RSA_WITH_AES_256_CBC_SHA"
case 0x003c:
return "TLS_RSA_WITH_AES_128_CBC_SHA256"
case 0x009c:
return "TLS_RSA_WITH_AES_128_GCM_SHA256"
case 0x009d:
return "TLS_RSA_WITH_AES_256_GCM_SHA384"
case 0xc007:
return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA"
case 0xc009:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"
case 0xc00a:
return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA"
case 0xc011:
return "TLS_ECDHE_RSA_WITH_RC4_128_SHA"
case 0xc012:
return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"
case 0xc013:
return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"
case 0xc014:
return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"
case 0xc023:
return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"
case 0xc027:
return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"
case 0xc02f:
return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
case 0xc02b:
return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"
case 0xc030:
return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"
case 0xc02c:
return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"
case 0xcca8:
return "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"
case 0xcca9:
return "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"
case 0x1301:
return "TLS_AES_128_GCM_SHA256"
case 0x1302:
return "TLS_AES_256_GCM_SHA384"
case 0x1303:
return "TLS_CHACHA20_POLY1305_SHA256"
case 0x5600:
return "TLS_FALLBACK_SCSV"
}
return fmt.Sprintf("0x%04X", id)
}
func (segs *httpSubsegments) WroteRequest(info httptrace.WroteRequestInfo) {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.reqCtx == nil {
return
}
segs.reqSeg.AddError(info.Err)
segs.reqSeg.Close()
segs.reqCtx, segs.reqSeg = nil, nil
}
// Cancel clean up all segments and set fault.
func (segs *httpSubsegments) Cancel() {
segs.mu.Lock()
defer segs.mu.Unlock()
if segs.dnsCtx != nil {
segs.dnsSeg.AddError(context.Canceled)
segs.dnsSeg.Close()
segs.dnsCtx, segs.dnsSeg = nil, nil
}
if segs.dialCtx != nil {
segs.dialSeg.AddError(context.Canceled)
segs.dialSeg.Close()
segs.dialCtx, segs.dialSeg = nil, nil
}
if segs.tlsCtx != nil {
segs.tlsSeg.AddError(context.Canceled)
segs.tlsSeg.Close()
segs.tlsCtx, segs.tlsSeg = nil, nil
}
if segs.connCtx != nil {
segs.connSeg.AddError(context.Canceled)
segs.connSeg.Close()
segs.connCtx, segs.connSeg = nil, nil
}
if segs.reqCtx != nil {
segs.reqSeg.AddError(context.Canceled)
segs.reqSeg.Close()
segs.reqCtx, segs.reqSeg = nil, nil
}
}
type clientTrace struct {
segments *httpSubsegments
httptrace *httptrace.ClientTrace
}
// WithClientTrace returns a new context based on the provided parent ctx.
func WithClientTrace(ctx context.Context) (context.Context, context.CancelFunc) {
if ctx == nil {
panic("ctx must not be nil")
}
segs := &httpSubsegments{
ctx: ctx,
}
trace := &clientTrace{
segments: segs,
httptrace: &httptrace.ClientTrace{
GetConn: segs.GetConn,
GotConn: segs.GotConn,
DNSStart: segs.DNSStart,
DNSDone: segs.DNSDone,
ConnectStart: segs.ConnectStart,
ConnectDone: segs.ConnectDone,
TLSHandshakeStart: segs.TLSHandshakeStart,
TLSHandshakeDone: segs.TLSHandshakeDone,
WroteRequest: segs.WroteRequest,
},
}
return httptrace.WithClientTrace(ctx, trace.httptrace), segs.Cancel
}