请选择 进入手机版 | 继续访问电脑版

技术控

    今日:45| 主题:61300
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Introducing HTTP Tracing

[复制链接]
半疯半颠半痴傻 发表于 2016-10-5 06:23:40
111 0
Introduction

  In Go 1.7 we introduced HTTP tracing, a facility to gather fine-grained information throughout the lifecycle of an HTTP client request. Support for HTTP tracing is provided by the          net/http/httptrace    package. The collected information can be used for debugging latency issues, service monitoring, writing adaptive systems, and more.  
  HTTP events

  The    httptracepackage provides a number of hooks to gather information during an HTTP round trip about a variety of events. These events include:  
  
       
  • Connection creation   
  • Connection reuse   
  • DNS lookups   
  • Writing the request to the wire   
  • Reading the response  
  Tracing events

  You can enable HTTP tracing by putting an          *httptrace.ClientTrace    containing hook functions into a request's          context.Context    . Various          http.RoundTripper    implementations report the internal events by looking for context's    *httptrace.ClientTraceand calling the relevant hook functions.  
  The tracing is scoped to the request's context and users should put a    *httptrace.ClientTraceto the request context before they start a request.  
  1.     req, _ := http.NewRequest("GET", "http://example.com", nil)
  2.     trace := &httptrace.ClientTrace{
  3.         DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
  4.             fmt.Printf("DNS Info: %+v\n", dnsInfo)
  5.         },
  6.         GotConn: func(connInfo httptrace.GotConnInfo) {
  7.             fmt.Printf("Got Conn: %+v\n", connInfo)
  8.         },
  9.     }
  10.     req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  11.     if _, err := http.DefaultTransport.RoundTrip(req); err != nil {
  12.         log.Fatal(err)
  13.     }
复制代码
During a round trip,    http.DefaultTransportwill invoke each hook as an event happens. The program above will print the DNS information as soon as the DNS lookup is complete. It will similarly print connection information when a connection is established to the request's host.  
  Tracing with http.Client

  The tracing mechanism is designed to trace the events in the lifecycle of a single    http.Transport.RoundTrip. However, a client may make multiple round trips to complete an HTTP request. For example, in the case of a URL redirection, the registered hooks will be called as many times as the client follows HTTP redirects, making multiple requests. Users are responsible for recognizing such events at the    http.Clientlevel. The program below identifies the current request by using an    http.RoundTripperwrapper.  
  1. package main
  2. import (
  3.     "fmt"
  4.     "log"
  5.     "net/http"
  6.     "net/http/httptrace"
  7. )
  8. // transport is an http.RoundTripper that keeps track of the in-flight
  9. // request and implements hooks to report HTTP tracing events.
  10. type transport struct {
  11.     current *http.Request
  12. }
  13. // RoundTrip wraps http.DefaultTransport.RoundTrip to keep track
  14. // of the current request.
  15. func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
  16.     t.current = req
  17.     return http.DefaultTransport.RoundTrip(req)
  18. }
  19. // GotConn prints whether the connection has been used previously
  20. // for the current request.
  21. func (t *transport) GotConn(info httptrace.GotConnInfo) {
  22.     fmt.Printf("Connection reused for %v? %v\n", t.current.URL, info.Reused)
  23. }
  24. func main() {
  25.     t := &transport{}
  26.     req, _ := http.NewRequest("GET", "https://google.com", nil)
  27.     trace := &httptrace.ClientTrace{
  28.         GotConn: t.GotConn,
  29.     }
  30.     req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
  31.     client := &http.Client{Transport: t}
  32.     if _, err := client.Do(req); err != nil {
  33.         log.Fatal(err)
  34.     }
  35. }
复制代码
The program will follow the redirect of google.com to www.google.com and will output:
  1. Connection reused for https://google.com? false
  2. Connection reused for https://www.google.com/? false
复制代码
The Transport in the    net/httppackage supports tracing of both HTTP/1 and HTTP/2 requests.  
  If you are an author of a custom    http.RoundTripperimplementation, you can support tracing by checking the request context for an    *httptest.ClientTraceand invoking the relevant hooks as the events occur.  
  Conclusion

  HTTP tracing is a valuable addition to Go for those who are interested in debugging HTTP request latency and writing tools for network debugging for outbound traffic. By enabling this new facility, we hope to see HTTP debugging, benchmarking and visualization tools from the community — such as    httpstat.
我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表