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

Print the response's body on demand for debugging purposes #276

Merged
merged 12 commits into from
Jan 24, 2025
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ linters-settings:
arguments:
- fmt.Printf
- fmt.Println
- fmt.Fprint
shackra marked this conversation as resolved.
Show resolved Hide resolved
- fmt.Fprintf
- fmt.Fprintln
- os.Stderr.Sync
Expand Down
40 changes: 20 additions & 20 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions pkg/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ const maxDepth = 8

var dontFixCycles, _ = strconv.ParseBool(os.Getenv("BATON_DONT_FIX_CYCLES"))

var (
ErrSyncNotComplete = fmt.Errorf("sync exited without finishing")
)
var ErrSyncNotComplete = fmt.Errorf("sync exited without finishing")

type Syncer interface {
Sync(context.Context) error
Expand Down Expand Up @@ -971,8 +969,7 @@ func (s *syncer) SyncAssets(ctx context.Context) error {
return nil
}

// SyncGrantExpansion
// TODO(morgabra) Docs.
// SyncGrantExpansion documentation pending.
func (s *syncer) SyncGrantExpansion(ctx context.Context) error {
l := ctxzap.Extract(ctx)
entitlementGraph := s.state.EntitlementGraph(ctx)
Expand Down
52 changes: 52 additions & 0 deletions pkg/uhttp/body_print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uhttp

// Implements a debugging facility for request responses. This changes
// the behavior of `BaseHttpClient` with an unexported flag.
//
// IMPORTANT: This feature is intended for development and debugging purposes only.
// Do not enable in production as it may expose sensitive information in logs.
//
// Usage:
// client := uhttp.NewBaseHttpClient(
// httpClient,
// uhttp.WithPrintBody(true), // Enable response body printing
// )

import (
"errors"
"fmt"
"io"
"os"
)

type printReader struct {
reader io.Reader
}

func (pr *printReader) Read(p []byte) (int, error) {
n, err := pr.reader.Read(p)
if n > 0 {
_, merr := fmt.Fprint(os.Stdout, string(p[:n]))
if merr != nil {
return -1, errors.Join(err, merr)
}
}

return n, err
}
shackra marked this conversation as resolved.
Show resolved Hide resolved

func wrapPrintBody(body io.Reader) io.Reader {
return &printReader{reader: body}
}

type printBodyOption struct {
debugPrintBody bool
}

func (o printBodyOption) Apply(c *BaseHttpClient) {
c.debugPrintBody = o.debugPrintBody
}

func WithPrintBody(shouldPrint bool) WrapperOption {
return printBodyOption{debugPrintBody: shouldPrint}
}
13 changes: 9 additions & 4 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ type (
NewRequest(ctx context.Context, method string, url *url.URL, options ...RequestOption) (*http.Request, error)
}
BaseHttpClient struct {
HttpClient *http.Client
rateLimiter uRateLimit.Limiter
baseHttpCache icache
HttpClient *http.Client
rateLimiter uRateLimit.Limiter
baseHttpCache icache
debugPrintBody bool
}

DoOption func(resp *WrapperResponse) error
Expand Down Expand Up @@ -341,7 +342,11 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo
}

// Replace resp.Body with a no-op closer so nobody has to worry about closing the reader.
resp.Body = io.NopCloser(bytes.NewBuffer(body))
if c.debugPrintBody {
resp.Body = io.NopCloser(wrapPrintBody(bytes.NewBuffer(body)))
} else {
resp.Body = io.NopCloser(bytes.NewBuffer(body))
}
shackra marked this conversation as resolved.
Show resolved Hide resolved

wresp := WrapperResponse{
Header: resp.Header,
Expand Down
Loading