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

fix: log url only for debugging http requests #3200

Open
wants to merge 1 commit into
base: develop
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
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"os/signal"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/debug"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -114,7 +116,7 @@ var (
}
// Prepare context
if viper.GetBool("DEBUG") {
ctx = utils.WithTraceContext(ctx)
http.DefaultTransport = debug.NewTransport()
fmt.Fprintln(os.Stderr, cmd.Root().Short)
}
cmd.SetContext(ctx)
Expand Down
24 changes: 24 additions & 0 deletions internal/debug/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package debug

import (
"log"
"net/http"
"os"
)

type debugTransport struct {
http.RoundTripper
logger *log.Logger
}

func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.logger.Printf("%s: %s\n", req.Method, req.URL)
return t.RoundTripper.RoundTrip(req)
}

func NewTransport() http.RoundTripper {
return &debugTransport{
http.DefaultTransport,
log.New(os.Stderr, "HTTP ", log.LstdFlags|log.Lmsgprefix),
}
}
56 changes: 0 additions & 56 deletions internal/utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package utils

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/http/httptrace"
"net/textproto"
"sync"

"github.com/go-errors/errors"
Expand Down Expand Up @@ -79,59 +76,6 @@ func ResolveCNAME(ctx context.Context, host string) (string, error) {
return "", errors.Errorf("failed to locate appropriate CNAME record for %s; resolves to %+v", host, serialized)
}

func WithTraceContext(ctx context.Context) context.Context {
trace := &httptrace.ClientTrace{
DNSStart: func(info httptrace.DNSStartInfo) {
log.Printf("DNS Start: %+v\n", info)
},
DNSDone: func(info httptrace.DNSDoneInfo) {
if info.Err != nil {
log.Println("DNS Error:", info.Err)
} else {
log.Printf("DNS Done: %+v\n", info)
}
},
ConnectStart: func(network, addr string) {
log.Println("Connect Start:", network, addr)
},
ConnectDone: func(network, addr string, err error) {
if err != nil {
log.Println("Connect Error:", network, addr, err)
} else {
log.Println("Connect Done:", network, addr)
}
},
TLSHandshakeStart: func() {
log.Println("TLS Start")
},
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
if err != nil {
log.Println("TLS Error:", err)
} else {
log.Printf("TLS Done: %+v\n", cs)
}
},
WroteHeaderField: func(key string, value []string) {
log.Println("Sent Header:", key, value)
},
WroteRequest: func(wr httptrace.WroteRequestInfo) {
if wr.Err != nil {
log.Println("Send Error:", wr.Err)
} else {
log.Println("Send Done")
}
},
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
log.Println("Recv 1xx:", code, header)
return nil
},
GotFirstResponseByte: func() {
log.Println("Recv First Byte")
},
}
return httptrace.WithClientTrace(ctx, trace)
}

type DialContextFunc func(context.Context, string, string) (net.Conn, error)

// Wraps a DialContext with DNS-over-HTTPS as fallback resolver
Expand Down