亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Go 中使用 httptrace 跟蹤 http.Client

如何在 Go 中使用 httptrace 跟蹤 http.Client

Go
繁星coding 2022-10-10 15:33:00
根據這個文檔,我們可以http.Client用httptrace這種方式追蹤 t := &transport{}    req, _ := http.NewRequest("GET", "https://google.com", nil)    trace := &httptrace.ClientTrace{        GotConn: t.GotConn,    }    req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))    client := &http.Client{Transport: t}對于google API 客戶端,這里是一個包裝代碼func NewWithClient(jsonKey []byte, cli *http.Client) (*Client, error) {    if cli == nil {        return nil, fmt.Errorf("client is nil")    }    ctx := context.WithValue(context.Background(), oauth2.HTTPClient, cli)    conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)    if err != nil {        return nil, err    }    service, err := androidpublisher.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))    if err != nil {        return nil, err    }    return &Client{service}, err}我們要應用到做 HTTP 跟蹤httptrace的http.Client參數。NewWithClient我們嘗試過的type TraceTransport struct {}var traceTransport = &TraceTransport{}var trace = &httptrace.ClientTrace{    GotConn: traceTransport.GotConn,}func (t *TraceTransport) RoundTrip(req *http.Request) (*http.Response, error) {    return http.DefaultTransport.RoundTrip(req)}func (t *TraceTransport) GotConn(info httptrace.GotConnInfo) {    fmt.Printf("Connection reused for %v \n", info.Reused)}type ClientWrapper struct {    defaultClient *http.Client}var clientWrapperTrace = &httptrace.ClientTrace{GotConn: traceTransport.GotConn}func (c *ClientWrapper) Do(req *http.Request) (*http.Response, error) {    req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientWrapperTrace))    return c.defaultClient.Do(req)}但是,追蹤失敗http.Client,沒有輸出GotConn。有人可以幫助我們找出上述代碼的問題嗎?http去谷歌API安卓發布者
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

  1. 來自的請求無法google/oauth2被 追蹤httptrace。您ClientWrapper傳遞的 withcontext.WithValue將在這里被忽略,并且 oauth2 有它自己的 http.Client,它只是使用from context.Value的Transport方法。*http.Client

  2. 來自 androidpublisher 的請求可以通過 httptrace 跟蹤,如下所示:

ctx := httptrace.WithClientTrace(context.Background(), clientWrapperTrace)r, err := c.VerifyProduct(ctx, packageName, productID, token)
  1. 如果您只想計算請求,我認為覆蓋http.Client.Transport是一種簡單的方法。

type TraceTransport struct {

}


func (t *TraceTransport) RoundTrip(req *http.Request) (*http.Response, error) {

    fmt.Printf("RoundTrip hook %v\n", req.URL)

    return http.DefaultTransport.RoundTrip(req)

}


func NewClientTrace(jsonKey []byte) (*Client, error) {

    cli := &http.Client{Transport: &TraceTransport{}}

    ctx := context.WithValue(context.Background(), oauth2.HTTPClient, cli)


    // ...

    service, err := androidpublisher.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))

    // ....

}


查看完整回答
反對 回復 2022-10-10
  • 1 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號