Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qiniu api v7.6.0; add debug info for http.DefaultClient #288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package cmd

import (
"fmt"
"net/http"
"net/http/httptrace"
"net/http/httputil"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/astaxie/beego/logs"
homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -103,8 +107,8 @@ func initConfig() {
if DebugFlag {
logs.SetLevel(logs.LevelDebug)
client.TurnOnDebug()
// master 已合并, v7.5.0 分支没包含次参数,等待 v7.5.1
// client.DeepDebugInfo = DeepDebugInfo
client.DeepDebugInfo = DeepDebugInfo
initHttpDefaultClient()
} else {
logs.SetLevel(logs.LevelInformational)
}
Expand Down Expand Up @@ -159,3 +163,62 @@ func initConfig() {
}
os.Rename(jsonConfigFile, cfgFile)
}

type MyTransport struct {
Transport http.RoundTripper
}

func (t MyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if DebugFlag {
trace := &httptrace.ClientTrace{
// 如果 ConnectStart Network 都没有显示,很可能是 DNS 失败
ConnectStart: func(network, addr string) {
logs.Debug(fmt.Sprintf("ConnectStart Network: %s, Remote ip:%s, URL: %s", network, addr, req.URL))
},
GotConn: func(connInfo httptrace.GotConnInfo) {
remoteAddr := connInfo.Conn.RemoteAddr()
logs.Debug(fmt.Sprintf("GotConn Network: %s, Remote ip:%s, URL: %s", remoteAddr.Network(), remoteAddr.String(), req.URL))
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
bs, bErr := httputil.DumpRequest(req, DeepDebugInfo)
if bErr == nil {
logs.Debug(string(bs))
} else {
logs.Debug(bErr)
}
}

resp, err := t.Transport.RoundTrip(req)

if DebugFlag {
if err != nil {
logs.Debug(err)
} else {
bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo)
if dErr == nil {
logs.Debug(string(bs))
}
}
}
return resp, err
}

func initHttpDefaultClient() {
t0 := http.DefaultTransport
if t0 != nil {
http.DefaultTransport = MyTransport{
Transport: t0,
}
}

t1 := http.DefaultClient.Transport
if t1 != nil {
http.DefaultClient.Transport = MyTransport{
Transport: t1,
}
}
if http.DefaultClient.Timeout == 0 {
http.DefaultClient.Timeout = 180 * time.Second
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/pelletier/go-toml v1.8.0 // indirect
github.com/qiniu/api.v7/v7 v7.5.0
github.com/qiniu/api.v7/v7 v7.6.0
github.com/satori/go.uuid v1.2.0 // indirect
github.com/spf13/afero v1.3.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/qiniu/api.v7/v7 v7.5.0 h1:DY6NrIp6FZ1GP4Roc9hRnO2m+OLzASYNnvz5Mbgw1rk=
github.com/qiniu/api.v7/v7 v7.5.0/go.mod h1:VE5oC5rkE1xul0u1S2N0b2Uxq9/6hZzhyqjgK25XDcM=
github.com/qiniu/api.v7/v7 v7.6.0 h1:396UGG+AWLh80pIhpPNCgEzb04t4S8CGKxqvLkiQeZI=
github.com/qiniu/api.v7/v7 v7.6.0/go.mod h1:zg3DaqU8mVnoQSQmtC/Mr2wXTJIE7fvcup+7sMt58Q0=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down