chore(ota): add trace logging to HTTP requests

This commit is contained in:
Siyuan 2025-11-18 14:26:45 +00:00
parent 88becfeed1
commit fc8cfadb9f
1 changed files with 15 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package ota
import ( import (
"context" "context"
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -58,6 +59,7 @@ func (s *State) getUpdateURL(params UpdateParams) (string, error, bool) {
} }
// newHTTPRequestWithTrace creates a new HTTP request with a trace logger // newHTTPRequestWithTrace creates a new HTTP request with a trace logger
// TODO: use OTEL instead of doing this manually
func (s *State) newHTTPRequestWithTrace(ctx context.Context, method, url string, body io.Reader, logger func() *zerolog.Event) (*http.Request, error) { func (s *State) newHTTPRequestWithTrace(ctx context.Context, method, url string, body io.Reader, logger func() *zerolog.Event) (*http.Request, error) {
localCtx := ctx localCtx := ctx
if s.l.GetLevel() <= zerolog.TraceLevel { if s.l.GetLevel() <= zerolog.TraceLevel {
@ -67,16 +69,23 @@ func (s *State) newHTTPRequestWithTrace(ctx context.Context, method, url string,
l := func() *zerolog.Event { return logger().Str("url", url).Str("method", method) } l := func() *zerolog.Event { return logger().Str("url", url).Str("method", method) }
localCtx = httptrace.WithClientTrace(localCtx, &httptrace.ClientTrace{ localCtx = httptrace.WithClientTrace(localCtx, &httptrace.ClientTrace{
GetConn: func(hostPort string) { l().Str("hostPort", hostPort).Msg("starting to create conn") }, GetConn: func(hostPort string) { l().Str("hostPort", hostPort).Msg("[conn] starting to create conn") },
DNSStart: func(info httptrace.DNSStartInfo) { l().Interface("info", info).Msg("starting to look up dns") }, GotConn: func(info httptrace.GotConnInfo) { l().Interface("info", info).Msg("[conn] connection established") },
DNSDone: func(info httptrace.DNSDoneInfo) { l().Interface("info", info).Msg("done looking up dns") }, PutIdleConn: func(err error) { l().Err(err).Msg("[conn] connection returned to idle pool") },
GotFirstResponseByte: func() { l().Msg("[resp] first response byte received") },
Got100Continue: func() { l().Msg("[resp] 100 continue received") },
DNSStart: func(info httptrace.DNSStartInfo) { l().Interface("info", info).Msg("[dns] starting to look up dns") },
DNSDone: func(info httptrace.DNSDoneInfo) { l().Interface("info", info).Msg("[dns] done looking up dns") },
ConnectStart: func(network, addr string) { ConnectStart: func(network, addr string) {
l().Str("network", network).Str("addr", addr).Msg("starting tcp connection") l().Str("network", network).Str("addr", addr).Msg("[tcp] starting tcp connection")
}, },
ConnectDone: func(network, addr string, err error) { ConnectDone: func(network, addr string, err error) {
l().Str("network", network).Str("addr", addr).Err(err).Msg("tcp connection created") l().Str("network", network).Str("addr", addr).Err(err).Msg("[tcp] tcp connection created")
},
TLSHandshakeStart: func() { l().Msg("[tls] handshake started") },
TLSHandshakeDone: func(state tls.ConnectionState, err error) {
l().Interface("state", state).Err(err).Msg("[tls] handshake done")
}, },
GotConn: func(info httptrace.GotConnInfo) { l().Interface("info", info).Msg("connection established") },
}) })
} }