-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathnet_http_httptrace.htm
360 lines (213 loc) · 16.4 KB
/
net_http_httptrace.htm
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../assets/site.css" rel="stylesheet">
<title>net/http/httptrace</title>
<meta name="twitter:title" content="Package httptrace">
<meta property="og:title" content="Package httptrace">
<meta name="description" content="Package httptrace provides mechanisms to trace the events within HTTP client requests.">
<meta name="twitter:description" content="Package httptrace provides mechanisms to trace the events within HTTP client requests.">
<meta property="og:description" content="Package httptrace provides mechanisms to trace the events within HTTP client requests.">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@golang">
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package httptrace</h2>
<p><code>import "net/http/httptrace"</code>
<p>
Package httptrace provides mechanisms to trace the events within
HTTP client requests.</p>
<div class="panel-group">
<div class="panel panel-default" id="example-package">
<div class="panel-heading"><a class="accordion-toggle" data-toggle="collapse" href="#ex-package">Example</a></div>
<div id="ex-package" class="panel-collapse collapse"><div class="panel-body">
<p>Code:<span class="pull-right"><a href="?play=package">play</a> </span>
<pre>
req, _ := http.NewRequest("GET", "http://example.com", nil)
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %+v\n", connInfo)
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf("DNS Info: %+v\n", dnsInfo)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
_, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Fatal(err)
}
</pre>
</div></div>
</div>
</div>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<ul class="list-unstyled">
<li><a href="#WithClientTrace">func WithClientTrace(ctx context.Context, trace *ClientTrace) context.Context</a></li>
<li><a href="#ClientTrace">type ClientTrace</a></li>
<ul>
<li><a href="#ContextClientTrace">func ContextClientTrace(ctx context.Context) *ClientTrace</a></li>
</ul>
<li><a href="#DNSDoneInfo">type DNSDoneInfo</a></li>
<li><a href="#DNSStartInfo">type DNSStartInfo</a></li>
<li><a href="#GotConnInfo">type GotConnInfo</a></li>
<li><a href="#WroteRequestInfo">type WroteRequestInfo</a></li>
</ul>
<h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-examples">¶</a></h4>
<ul class="list-unstyled">
<li><a href="#example-package" onclick="$('#ex-package').addClass('in').removeClass('collapse').height('auto')">package</a></li>
</ul>
<h4 id="pkg-files">
<a href="https://github.com/golang/go/blob/master/src/net/http/httptrace/">Package Files</a>
<a class="permalink" href="#pkg-files">¶</a>
</h4>
<p><a href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go">trace.go</a> </p>
<h3 id="WithClientTrace" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L33">WithClientTrace</a> <a class="permalink" href="#WithClientTrace">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=WithClientTrace&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L33">❖</a><pre>func WithClientTrace(ctx <a href="/context">context</a>.<a href="/context#Context">Context</a>, trace *<a href="#ClientTrace">ClientTrace</a>) <a href="/context">context</a>.<a href="/context#Context">Context</a></pre></div><p>
WithClientTrace returns a new context based on the provided parent
ctx. HTTP client requests made with the returned context will use
the provided trace hooks, in addition to any previous hooks
registered with ctx. Any hooks defined in the provided trace will
be called first.
</p>
<h3 id="ClientTrace" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L79">ClientTrace</a> <a class="permalink" href="#ClientTrace">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=ClientTrace&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L79">❖</a><pre>type ClientTrace struct {
<span class="com">// GetConn is called before a connection is created or
// retrieved from an idle pool. The hostPort is the
// "host:port" of the target or proxy. GetConn is called even
// if there's already an idle cached connection available.</span>
<span id="ClientTrace.GetConn">GetConn</span> func(hostPort <a href="/builtin#string">string</a>)
<span class="com">// GotConn is called after a successful connection is
// obtained. There is no hook for failure to obtain a
// connection; instead, use the error from
// Transport.RoundTrip.</span>
<span id="ClientTrace.GotConn">GotConn</span> func(<a href="#GotConnInfo">GotConnInfo</a>)
<span class="com">// PutIdleConn is called when the connection is returned to
// the idle pool. If err is nil, the connection was
// successfully returned to the idle pool. If err is non-nil,
// it describes why not. PutIdleConn is not called if
// connection reuse is disabled via Transport.DisableKeepAlives.
// PutIdleConn is called before the caller's Response.Body.Close
// call returns.
// For HTTP/2, this hook is not currently used.</span>
<span id="ClientTrace.PutIdleConn">PutIdleConn</span> func(err <a href="/builtin#error">error</a>)
<span class="com">// GotFirstResponseByte is called when the first byte of the response
// headers is available.</span>
<span id="ClientTrace.GotFirstResponseByte">GotFirstResponseByte</span> func()
<span class="com">// Got100Continue is called if the server replies with a "100
// Continue" response.</span>
<span id="ClientTrace.Got100Continue">Got100Continue</span> func()
<span class="com">// DNSStart is called when a DNS lookup begins.</span>
<span id="ClientTrace.DNSStart">DNSStart</span> func(<a href="#DNSStartInfo">DNSStartInfo</a>)
<span class="com">// DNSDone is called when a DNS lookup ends.</span>
<span id="ClientTrace.DNSDone">DNSDone</span> func(<a href="#DNSDoneInfo">DNSDoneInfo</a>)
<span class="com">// ConnectStart is called when a new connection's Dial begins.
// If net.Dialer.DualStack (IPv6 "Happy Eyeballs") support is
// enabled, this may be called multiple times.</span>
<span id="ClientTrace.ConnectStart">ConnectStart</span> func(network, addr <a href="/builtin#string">string</a>)
<span class="com">// ConnectDone is called when a new connection's Dial
// completes. The provided err indicates whether the
// connection completedly successfully.
// If net.Dialer.DualStack ("Happy Eyeballs") support is
// enabled, this may be called multiple times.</span>
<span id="ClientTrace.ConnectDone">ConnectDone</span> func(network, addr <a href="/builtin#string">string</a>, err <a href="/builtin#error">error</a>)
<span class="com">// TLSHandshakeStart is called when the TLS handshake is started. When
// connecting to a HTTPS site via a HTTP proxy, the handshake happens after
// the CONNECT request is processed by the proxy.</span>
<span id="ClientTrace.TLSHandshakeStart">TLSHandshakeStart</span> func()
<span class="com">// TLSHandshakeDone is called after the TLS handshake with either the
// successful handshake's connection state, or a non-nil error on handshake
// failure.</span>
<span id="ClientTrace.TLSHandshakeDone">TLSHandshakeDone</span> func(<a href="/crypto/tls">tls</a>.<a href="/crypto/tls#ConnectionState">ConnectionState</a>, <a href="/builtin#error">error</a>)
<span class="com">// WroteHeaders is called after the Transport has written
// the request headers.</span>
<span id="ClientTrace.WroteHeaders">WroteHeaders</span> func()
<span class="com">// Wait100Continue is called if the Request specified
// "Expected: 100-continue" and the Transport has written the
// request headers but is waiting for "100 Continue" from the
// server before writing the request body.</span>
<span id="ClientTrace.Wait100Continue">Wait100Continue</span> func()
<span class="com">// WroteRequest is called with the result of writing the
// request and any body. It may be called multiple times
// in the case of retried requests.</span>
<span id="ClientTrace.WroteRequest">WroteRequest</span> func(<a href="#WroteRequestInfo">WroteRequestInfo</a>)
}</pre></div><p>
ClientTrace is a set of hooks to run at various stages of an outgoing
HTTP request. Any particular hook may be nil. Functions may be
called concurrently from different goroutines and some may be called
after the request has completed or failed.
</p>
<p>
ClientTrace currently traces a single HTTP request & response
during a single round trip and has no hooks that span a series
of redirected requests.
</p>
<p>
See <a href="https://blog.golang.org/http-tracing">https://blog.golang.org/http-tracing</a> for more.
</p>
<h4 id="ContextClientTrace" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L23">ContextClientTrace</a> <a class="permalink" href="#ContextClientTrace">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=ContextClientTrace&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L23">❖</a><pre>func ContextClientTrace(ctx <a href="/context">context</a>.<a href="/context#Context">Context</a>) *<a href="#ClientTrace">ClientTrace</a></pre></div><p>
ContextClientTrace returns the ClientTrace associated with the
provided context. If none, it returns nil.
</p>
<h3 id="DNSDoneInfo" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L204">DNSDoneInfo</a> <a class="permalink" href="#DNSDoneInfo">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=DNSDoneInfo&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L204">❖</a><pre>type DNSDoneInfo struct {
<span class="com">// Addrs are the IPv4 and/or IPv6 addresses found in the DNS
// lookup. The contents of the slice should not be mutated.</span>
<span id="DNSDoneInfo.Addrs">Addrs</span> []<a href="/net">net</a>.<a href="/net#IPAddr">IPAddr</a>
<span class="com">// Err is any error that occurred during the DNS lookup.</span>
<span id="DNSDoneInfo.Err">Err</span> <a href="/builtin#error">error</a>
<span class="com">// Coalesced is whether the Addrs were shared with another
// caller who was doing the same DNS lookup concurrently.</span>
<span id="DNSDoneInfo.Coalesced">Coalesced</span> <a href="/builtin#bool">bool</a>
}</pre></div><p>
DNSDoneInfo contains information about the results of a DNS lookup.
</p>
<h3 id="DNSStartInfo" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L199">DNSStartInfo</a> <a class="permalink" href="#DNSStartInfo">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=DNSStartInfo&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L199">❖</a><pre>type DNSStartInfo struct {
<span id="DNSStartInfo.Host">Host</span> <a href="/builtin#string">string</a>
}</pre></div><p>
DNSStartInfo contains information about a DNS request.
</p>
<h3 id="GotConnInfo" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L226">GotConnInfo</a> <a class="permalink" href="#GotConnInfo">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=GotConnInfo&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L226">❖</a><pre>type GotConnInfo struct {
<span class="com">// Conn is the connection that was obtained. It is owned by
// the http.Transport and should not be read, written or
// closed by users of ClientTrace.</span>
<span id="GotConnInfo.Conn">Conn</span> <a href="/net">net</a>.<a href="/net#Conn">Conn</a>
<span class="com">// Reused is whether this connection has been previously
// used for another HTTP request.</span>
<span id="GotConnInfo.Reused">Reused</span> <a href="/builtin#bool">bool</a>
<span class="com">// WasIdle is whether this connection was obtained from an
// idle pool.</span>
<span id="GotConnInfo.WasIdle">WasIdle</span> <a href="/builtin#bool">bool</a>
<span class="com">// IdleTime reports how long the connection was previously
// idle, if WasIdle is true.</span>
<span id="GotConnInfo.IdleTime">IdleTime</span> <a href="/time">time</a>.<a href="/time#Duration">Duration</a>
}</pre></div><p>
GotConnInfo is the argument to the ClientTrace.GotConn function and
contains information about the obtained connection.
</p>
<h3 id="WroteRequestInfo" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L156">WroteRequestInfo</a> <a class="permalink" href="#WroteRequestInfo">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=WroteRequestInfo&pkg=net%2Fhttp%2Fhttptrace&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/net/http/httptrace/trace.go#L156">❖</a><pre>type WroteRequestInfo struct {
<span class="com">// Err is any error encountered while writing the Request.</span>
<span id="WroteRequestInfo.Err">Err</span> <a href="/builtin#error">error</a>
}</pre></div><p>
WroteRequestInfo contains information provided to the WroteRequest
hook.
</p>
<div id="x-footer" class="clearfix">
<div class="container">
<a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
<span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
<span class="pull-right"><a href="#">Back to top</a></span>
</div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>
</body>
</html>