Skip to content

Commit ce74050

Browse files
authored
fix: log url only for debugging http requests (#3200)
fix: log url only for debugging http
1 parent 4b6489e commit ce74050

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"net/http"
78
"net/url"
89
"os"
910
"os/signal"
@@ -15,6 +16,7 @@ import (
1516
"github.com/spf13/afero"
1617
"github.com/spf13/cobra"
1718
"github.com/spf13/viper"
19+
"github.com/supabase/cli/internal/debug"
1820
"github.com/supabase/cli/internal/utils"
1921
"github.com/supabase/cli/internal/utils/flags"
2022
"golang.org/x/mod/semver"
@@ -114,7 +116,7 @@ var (
114116
}
115117
// Prepare context
116118
if viper.GetBool("DEBUG") {
117-
ctx = utils.WithTraceContext(ctx)
119+
http.DefaultTransport = debug.NewTransport()
118120
fmt.Fprintln(os.Stderr, cmd.Root().Short)
119121
}
120122
cmd.SetContext(ctx)

internal/debug/http.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package debug
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"os"
7+
)
8+
9+
type debugTransport struct {
10+
http.RoundTripper
11+
logger *log.Logger
12+
}
13+
14+
func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
15+
t.logger.Printf("%s: %s\n", req.Method, req.URL)
16+
return t.RoundTripper.RoundTrip(req)
17+
}
18+
19+
func NewTransport() http.RoundTripper {
20+
return &debugTransport{
21+
http.DefaultTransport,
22+
log.New(os.Stderr, "HTTP ", log.LstdFlags|log.Lmsgprefix),
23+
}
24+
}

internal/utils/api.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package utils
22

33
import (
44
"context"
5-
"crypto/tls"
65
"encoding/json"
76
"fmt"
87
"log"
98
"net"
109
"net/http"
11-
"net/http/httptrace"
12-
"net/textproto"
1310
"sync"
1411

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

82-
func WithTraceContext(ctx context.Context) context.Context {
83-
trace := &httptrace.ClientTrace{
84-
DNSStart: func(info httptrace.DNSStartInfo) {
85-
log.Printf("DNS Start: %+v\n", info)
86-
},
87-
DNSDone: func(info httptrace.DNSDoneInfo) {
88-
if info.Err != nil {
89-
log.Println("DNS Error:", info.Err)
90-
} else {
91-
log.Printf("DNS Done: %+v\n", info)
92-
}
93-
},
94-
ConnectStart: func(network, addr string) {
95-
log.Println("Connect Start:", network, addr)
96-
},
97-
ConnectDone: func(network, addr string, err error) {
98-
if err != nil {
99-
log.Println("Connect Error:", network, addr, err)
100-
} else {
101-
log.Println("Connect Done:", network, addr)
102-
}
103-
},
104-
TLSHandshakeStart: func() {
105-
log.Println("TLS Start")
106-
},
107-
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
108-
if err != nil {
109-
log.Println("TLS Error:", err)
110-
} else {
111-
log.Printf("TLS Done: %+v\n", cs)
112-
}
113-
},
114-
WroteHeaderField: func(key string, value []string) {
115-
log.Println("Sent Header:", key, value)
116-
},
117-
WroteRequest: func(wr httptrace.WroteRequestInfo) {
118-
if wr.Err != nil {
119-
log.Println("Send Error:", wr.Err)
120-
} else {
121-
log.Println("Send Done")
122-
}
123-
},
124-
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
125-
log.Println("Recv 1xx:", code, header)
126-
return nil
127-
},
128-
GotFirstResponseByte: func() {
129-
log.Println("Recv First Byte")
130-
},
131-
}
132-
return httptrace.WithClientTrace(ctx, trace)
133-
}
134-
13579
type DialContextFunc func(context.Context, string, string) (net.Conn, error)
13680

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

0 commit comments

Comments
 (0)