Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b9d128

Browse files
authored
cli: add --debug-http flag (#7192)
This makes it easier to help debug client issues.
1 parentf94ac55 commit2b9d128

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎cli/root.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
364364
Value:clibase.BoolOf(&r.verbose),
365365
Group:globalGroup,
366366
},
367+
{
368+
Flag:"debug-http",
369+
Description:"Debug codersdk HTTP requests.",
370+
Value:clibase.BoolOf(&r.debugHTTP),
371+
Group:globalGroup,
372+
Hidden:true,
373+
},
367374
{
368375
Flag:config.FlagName,
369376
Env:"CODER_CONFIG_DIR",
@@ -412,6 +419,7 @@ type RootCmd struct {
412419
forceTTYbool
413420
noOpenbool
414421
verbosebool
422+
debugHTTPbool
415423

416424
noVersionCheckbool
417425
noFeatureWarningbool
@@ -464,6 +472,11 @@ func (r *RootCmd) InitClient(client *codersdk.Client) clibase.MiddlewareFunc {
464472

465473
client.SetSessionToken(r.token)
466474

475+
ifr.debugHTTP {
476+
client.PlainLogger=os.Stderr
477+
client.LogBodies=true
478+
}
479+
467480
// We send these requests in parallel to minimize latency.
468481
var (
469482
versionErr=make(chanerror)

‎codersdk/client.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"mime"
1111
"net"
1212
"net/http"
13+
"net/http/httputil"
1314
"net/url"
1415
"strings"
1516
"sync"
@@ -90,6 +91,10 @@ type Client struct {
9091
// LogBodies can be enabled to print request and response bodies to the logger.
9192
LogBodiesbool
9293

94+
// PlainLogger may be set to log HTTP traffic in a human-readable form.
95+
// It uses the LogBodies option.
96+
PlainLogger io.Writer
97+
9398
// Trace can be enabled to propagate tracing spans to the Coder API.
9499
// This is useful for tracking a request end-to-end.
95100
Tracebool
@@ -109,6 +114,16 @@ func (c *Client) SetSessionToken(token string) {
109114
c.sessionToken=token
110115
}
111116

117+
funcprefixLines(prefix,s []byte) []byte {
118+
ss:=bytes.NewBuffer(make([]byte,0,len(s)*2))
119+
for_,line:=rangebytes.Split(s, []byte("\n")) {
120+
_,_=ss.Write(prefix)
121+
_,_=ss.Write(line)
122+
_=ss.WriteByte('\n')
123+
}
124+
returnss.Bytes()
125+
}
126+
112127
// Request performs a HTTP request with the body provided. The caller is
113128
// responsible for closing the response body.
114129
func (c*Client)Request(ctx context.Context,method,pathstring,bodyinterface{},opts...RequestOption) (*http.Response,error) {
@@ -155,6 +170,15 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
155170
returnnil,xerrors.Errorf("create request: %w",err)
156171
}
157172

173+
ifc.PlainLogger!=nil {
174+
out,err:=httputil.DumpRequest(req,c.LogBodies)
175+
iferr!=nil {
176+
returnnil,xerrors.Errorf("dump request: %w",err)
177+
}
178+
out=prefixLines([]byte("http --> "),out)
179+
_,_=c.PlainLogger.Write(out)
180+
}
181+
158182
tokenHeader:=c.SessionTokenHeader
159183
iftokenHeader=="" {
160184
tokenHeader=SessionTokenHeader
@@ -192,6 +216,15 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
192216
returnnil,err
193217
}
194218

219+
ifc.PlainLogger!=nil {
220+
out,err:=httputil.DumpResponse(resp,c.LogBodies)
221+
iferr!=nil {
222+
returnnil,xerrors.Errorf("dump response: %w",err)
223+
}
224+
out=prefixLines([]byte("http <-- "),out)
225+
_,_=c.PlainLogger.Write(out)
226+
}
227+
195228
span.SetAttributes(httpconv.ClientResponse(resp)...)
196229
span.SetStatus(httpconv.ClientStatus(resp.StatusCode))
197230

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp