Skip to content

Commit

Permalink
enable logging headers added in roundtrip (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyangyu authored Sep 1, 2024
1 parent 1423108 commit 81850d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
8 changes: 1 addition & 7 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,7 @@ func RootCmd(h *internal.Helper) *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if debugMode {
log.InitLogger("DEBUG")
// enable debug for go-openapi, will output http request/response details, but won't output header
// added in roundtrip
err := os.Setenv("SWAGGER_DEBUG", "1")
if err != nil {
return err
}
err = os.Setenv(config.DebugEnv, "1")
err := os.Setenv(config.DebugEnv, "1")
if err != nil {
return err
}
Expand Down
49 changes: 40 additions & 9 deletions internal/service/cloud/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"

"os"
"tidbcloud-cli/internal/config"
Expand Down Expand Up @@ -405,12 +406,6 @@ func NewApiClient(rt http.RoundTripper, apiUrl string, serverlessEndpoint string
exportCfg.HTTPClient = httpclient
exportCfg.Host = serverlessURL.Host

if os.Getenv(config.DebugEnv) == "true" || os.Getenv(config.DebugEnv) == "1" {
branchCfg.Debug = true
exportCfg.Debug = true
iamCfg.Debug = true
}

return branch.NewAPIClient(branchCfg), serverlessClient.New(serverlessTransport, strfmt.Default),
pingchatClient.New(transport, strfmt.Default), brClient.New(backRestoreTransport, strfmt.Default),
serverlessImportClient.New(importTransport, strfmt.Default), export.NewAPIClient(exportCfg),
Expand All @@ -419,13 +414,14 @@ func NewApiClient(rt http.RoundTripper, apiUrl string, serverlessEndpoint string

func NewDigestTransport(publicKey, privateKey string) http.RoundTripper {
return NewTransportWithAgent(&digest.Transport{
Username: publicKey,
Password: privateKey,
Username: publicKey,
Password: privateKey,
Transport: NewDebugTransport(http.DefaultTransport),
}, fmt.Sprintf("%s/%s", config.CliName, version.Version))
}

func NewBearTokenTransport(token string) http.RoundTripper {
return NewTransportWithAgent(NewTransportWithBearToken(http.DefaultTransport, token),
return NewTransportWithAgent(NewTransportWithBearToken(NewDebugTransport(http.DefaultTransport), token),
fmt.Sprintf("%s/%s", config.CliName, version.Version))
}

Expand Down Expand Up @@ -465,6 +461,41 @@ func (bt *BearTokenTransport) RoundTrip(r *http.Request) (*http.Response, error)
return bt.inner.RoundTrip(r)
}

func NewDebugTransport(inner http.RoundTripper) http.RoundTripper {
return &DebugTransport{inner: inner}
}

type DebugTransport struct {
inner http.RoundTripper
}

func (dt *DebugTransport) RoundTrip(r *http.Request) (*http.Response, error) {
debug := os.Getenv(config.DebugEnv) == "true" || os.Getenv(config.DebugEnv) == "1"

if debug {
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
return nil, err
}
fmt.Printf("\n%s", string(dump))
}

resp, err := dt.inner.RoundTrip(r)
if err != nil {
return resp, err
}

if debug {
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
return resp, err
}
fmt.Printf("%s\n", string(dump))
}

return resp, err
}

func parseError(err error, resp *http.Response) error {
defer func() {
if resp != nil {
Expand Down

0 comments on commit 81850d7

Please sign in to comment.