技术控

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

[其他] Introducing HTTP Tracing

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

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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.
友荐云推荐




上一篇:你相信存在没有漏洞的代码吗?美国人正在研究
下一篇:Matlab 中文用户谨慎更新 macOS Sierra
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表