/
httptrace.go
257 lines (231 loc) · 8.5 KB
/
httptrace.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
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
// +build go1.8
package xray
import (
"context"
"crypto/tls"
"errors"
"net/http/httptrace"
"sync"
)
// HTTPSubsegments is a set of context in different HTTP operation.
// Note: from ClientTrace godoc
// Functions may be called concurrently from different goroutines
//
// HTTPSubsegments must operate as though all functions on it can be called in
// different goroutines and must protect against races
type HTTPSubsegments struct {
opCtx context.Context
connCtx context.Context
dnsCtx context.Context
connectCtx context.Context
tlsCtx context.Context
reqCtx context.Context
responseCtx context.Context
mu sync.Mutex
}
// NewHTTPSubsegments creates a new HTTPSubsegments to use in
// httptrace.ClientTrace functions
func NewHTTPSubsegments(opCtx context.Context) *HTTPSubsegments {
return &HTTPSubsegments{opCtx: opCtx}
}
// GetConn begins a connect subsegment if the HTTP operation
// subsegment is still in progress.
func (xt *HTTPSubsegments) GetConn(hostPort string) {
xt.mu.Lock()
defer xt.mu.Unlock()
if GetSegment(xt.opCtx).safeInProgress() {
xt.connCtx, _ = BeginSubsegment(xt.opCtx, "connect")
}
}
// DNSStart begins a dns subsegment if the HTTP operation
// subsegment is still in progress.
func (xt *HTTPSubsegments) DNSStart(info httptrace.DNSStartInfo) {
xt.mu.Lock()
defer xt.mu.Unlock()
if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil {
xt.dnsCtx, _ = BeginSubsegment(xt.connCtx, "dns")
}
}
// DNSDone closes the dns subsegment if the HTTP operation
// subsegment is still in progress, passing the error value
// (if any). Information about the address values looked up,
// and whether or not the call was coalesced is added as
// metadata to the dns subsegment.
func (xt *HTTPSubsegments) DNSDone(info httptrace.DNSDoneInfo) {
xt.mu.Lock()
defer xt.mu.Unlock()
if xt.dnsCtx != nil && GetSegment(xt.opCtx).safeInProgress() {
metadata := make(map[string]interface{})
metadata["addresses"] = info.Addrs
metadata["coalesced"] = info.Coalesced
AddMetadataToNamespace(xt.dnsCtx, "http", "dns", metadata)
GetSegment(xt.dnsCtx).Close(info.Err)
}
}
// ConnectStart begins a dial subsegment if the HTTP operation
// subsegment is still in progress.
func (xt *HTTPSubsegments) ConnectStart(network, addr string) {
xt.mu.Lock()
defer xt.mu.Unlock()
if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil {
xt.connectCtx, _ = BeginSubsegment(xt.connCtx, "dial")
}
}
// ConnectDone closes the dial subsegment if the HTTP operation
// subsegment is still in progress, passing the error value
// (if any). Information about the network over which the dial
// was made is added as metadata to the subsegment.
func (xt *HTTPSubsegments) ConnectDone(network, addr string, err error) {
xt.mu.Lock()
defer xt.mu.Unlock()
if xt.connectCtx != nil && GetSegment(xt.opCtx).safeInProgress() {
metadata := make(map[string]interface{})
metadata["network"] = network
AddMetadataToNamespace(xt.connectCtx, "http", "connect", metadata)
GetSegment(xt.connectCtx).Close(err)
}
}
// TLSHandshakeStart begins a tls subsegment if the HTTP operation
// subsegment is still in progress.
func (xt *HTTPSubsegments) TLSHandshakeStart() {
xt.mu.Lock()
defer xt.mu.Unlock()
if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil {
xt.tlsCtx, _ = BeginSubsegment(xt.connCtx, "tls")
}
}
// TLSHandshakeDone closes the tls subsegment if the HTTP
// operation subsegment is still in progress, passing the
// error value(if any). Information about the tls connection
// is added as metadata to the subsegment.
func (xt *HTTPSubsegments) TLSHandshakeDone(connState tls.ConnectionState, err error) {
xt.mu.Lock()
defer xt.mu.Unlock()
if xt.tlsCtx != nil && GetSegment(xt.opCtx).safeInProgress() {
metadata := make(map[string]interface{})
metadata["did_resume"] = connState.DidResume
metadata["negotiated_protocol"] = connState.NegotiatedProtocol
metadata["negotiated_protocol_is_mutual"] = connState.NegotiatedProtocolIsMutual
metadata["cipher_suite"] = connState.CipherSuite
AddMetadataToNamespace(xt.tlsCtx, "http", "tls", metadata)
GetSegment(xt.tlsCtx).Close(err)
}
}
// GotConn closes the connect subsegment if the HTTP operation
// subsegment is still in progress, passing the error value
// (if any). Information about the connection is added as
// metadata to the subsegment. If the connection is marked as reused,
// the connect subsegment is deleted.
func (xt *HTTPSubsegments) GotConn(info *httptrace.GotConnInfo, err error) {
xt.mu.Lock()
defer xt.mu.Unlock()
if xt.connCtx != nil && GetSegment(xt.opCtx).safeInProgress() { // GetConn may not have been called (client_test.TestBadRoundTrip)
if info != nil {
if info.Reused {
GetSegment(xt.opCtx).RemoveSubsegment(GetSegment(xt.connCtx))
// Remove the connCtx context since it is no longer needed.
xt.connCtx = nil
} else {
metadata := make(map[string]interface{})
metadata["reused"] = info.Reused
metadata["was_idle"] = info.WasIdle
if info.WasIdle {
metadata["idle_time"] = info.IdleTime
}
AddMetadataToNamespace(xt.connCtx, "http", "connection", metadata)
GetSegment(xt.connCtx).Close(err)
}
} else if xt.connCtx != nil && GetSegment(xt.connCtx).safeInProgress() {
GetSegment(xt.connCtx).Close(err)
}
if err == nil {
xt.reqCtx, _ = BeginSubsegment(xt.opCtx, "request")
}
}
}
// WroteRequest closes the request subsegment if the HTTP operation
// subsegment is still in progress, passing the error value
// (if any). The response subsegment is then begun.
func (xt *HTTPSubsegments) WroteRequest(info httptrace.WroteRequestInfo) {
xt.mu.Lock()
defer xt.mu.Unlock()
if xt.reqCtx != nil && GetSegment(xt.opCtx).safeInProgress() {
GetSegment(xt.reqCtx).Close(info.Err)
resCtx, _ := BeginSubsegment(xt.opCtx, "response")
xt.responseCtx = resCtx
}
// In case the GotConn http trace handler wasn't called,
// we close the connection subsegment since a connection
// had to have been acquired before attempting to write
// the request.
if xt.connCtx != nil && GetSegment(xt.connCtx).safeInProgress() {
GetSegment(xt.connCtx).Close(nil)
}
}
// GotFirstResponseByte closes the response subsegment if the HTTP
// operation subsegment is still in progress.
func (xt *HTTPSubsegments) GotFirstResponseByte() {
xt.mu.Lock()
defer xt.mu.Unlock()
resCtx := xt.responseCtx
if resCtx != nil && GetSegment(xt.opCtx).safeInProgress() {
GetSegment(resCtx).Close(nil)
}
}
// ClientTrace is a set of pointers of HTTPSubsegments and ClientTrace.
type ClientTrace struct {
subsegments *HTTPSubsegments
httpTrace *httptrace.ClientTrace
}
// NewClientTrace returns an instance of xray.ClientTrace, a wrapper
// around httptrace.ClientTrace. The ClientTrace implementation will
// generate subsegments for connection time, DNS lookup time, TLS
// handshake time, and provides additional information about the HTTP round trip
func NewClientTrace(opCtx context.Context) (ct *ClientTrace, err error) {
if opCtx == nil {
return nil, errors.New("opCtx must be non-nil")
}
segs := NewHTTPSubsegments(opCtx)
return &ClientTrace{
subsegments: segs,
httpTrace: &httptrace.ClientTrace{
GetConn: func(hostPort string) {
segs.GetConn(hostPort)
},
DNSStart: func(info httptrace.DNSStartInfo) {
segs.DNSStart(info)
},
DNSDone: func(info httptrace.DNSDoneInfo) {
segs.DNSDone(info)
},
ConnectStart: func(network, addr string) {
segs.ConnectStart(network, addr)
},
ConnectDone: func(network, addr string, err error) {
segs.ConnectDone(network, addr, err)
},
TLSHandshakeStart: func() {
segs.TLSHandshakeStart()
},
TLSHandshakeDone: func(connState tls.ConnectionState, err error) {
segs.TLSHandshakeDone(connState, err)
},
GotConn: func(info httptrace.GotConnInfo) {
segs.GotConn(&info, nil)
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
segs.WroteRequest(info)
},
GotFirstResponseByte: func() {
segs.GotFirstResponseByte()
},
},
}, nil
}