Skip to content

Commit

Permalink
feat: add json definition and method to trace info
Browse files Browse the repository at this point in the history
- its preparation for upcoming debug log formatter
  • Loading branch information
jeevatkm committed Jan 25, 2025
1 parent 236685e commit 60978d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ func (r *Request) TraceInfo() TraceInfo {

// Capture remote address info when connection is non-nil
if ct.gotConnInfo.Conn != nil {
ti.RemoteAddr = ct.gotConnInfo.Conn.RemoteAddr()
ti.RemoteAddr = ct.gotConnInfo.Conn.RemoteAddr().String()
}

return ti
Expand Down
9 changes: 7 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,9 @@ func TestTraceInfo(t *testing.T) {
assertEqual(t, true, tr.TotalTime >= 0)
assertEqual(t, true, tr.TotalTime < time.Hour)
assertEqual(t, true, tr.TotalTime == resp.Time())
assertEqual(t, tr.RemoteAddr.String(), serverAddr)
assertEqual(t, tr.RemoteAddr, serverAddr)

assertNotNil(t, tr.Clone())
}

client.DisableTrace()
Expand All @@ -1823,7 +1825,7 @@ func TestTraceInfo(t *testing.T) {
assertEqual(t, true, tr.ResponseTime >= 0)
assertEqual(t, true, tr.TotalTime >= 0)
assertEqual(t, true, tr.TotalTime == resp.Time())
assertEqual(t, tr.RemoteAddr.String(), serverAddr)
assertEqual(t, tr.RemoteAddr, serverAddr)
}

})
Expand All @@ -1837,6 +1839,9 @@ func TestTraceInfo(t *testing.T) {
resp, err := c.R().EnableTrace().EnableDebug().Get(u)
assertNil(t, err)
assertNotNil(t, resp)

jsonStr := resp.Request.TraceInfo().JSON()
assertEqual(t, true, strings.Contains(jsonStr, serverAddr))
}

logContent := logBuf.String()
Expand Down
40 changes: 27 additions & 13 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http/httptrace"
"time"
)
Expand All @@ -19,45 +18,45 @@ import (
type TraceInfo struct {
// DNSLookup is the duration that transport took to perform
// DNS lookup.
DNSLookup time.Duration
DNSLookup time.Duration `json:"dns_lookup_time"`

// ConnTime is the duration it took to obtain a successful connection.
ConnTime time.Duration
ConnTime time.Duration `json:"connection_time"`

// TCPConnTime is the duration it took to obtain the TCP connection.
TCPConnTime time.Duration
TCPConnTime time.Duration `json:"tcp_connection_time"`

// TLSHandshake is the duration of the TLS handshake.
TLSHandshake time.Duration
TLSHandshake time.Duration `json:"tls_handshake_time"`

// ServerTime is the server's duration for responding to the first byte.
ServerTime time.Duration
ServerTime time.Duration `json:"server_time"`

// ResponseTime is the duration since the first response byte from the server to
// request completion.
ResponseTime time.Duration
ResponseTime time.Duration `json:"response_time"`

// TotalTime is the duration of the total time request taken end-to-end.
TotalTime time.Duration
TotalTime time.Duration `json:"total_time"`

// IsConnReused is whether this connection has been previously
// used for another HTTP request.
IsConnReused bool
IsConnReused bool `json:"is_connection_reused"`

// IsConnWasIdle is whether this connection was obtained from an
// idle pool.
IsConnWasIdle bool
IsConnWasIdle bool `json:"is_connection_was_idle"`

// ConnIdleTime is the duration how long the connection that was previously
// idle, if IsConnWasIdle is true.
ConnIdleTime time.Duration
ConnIdleTime time.Duration `json:"connection_idle_time"`

// RequestAttempt is to represent the request attempt made during a Resty
// request execution flow, including retry count.
RequestAttempt int
RequestAttempt int `json:"request_attempt"`

// RemoteAddr returns the remote network address.
RemoteAddr net.Addr
RemoteAddr string `json:"remote_address"`
}

// String method returns string representation of request trace information.
Expand All @@ -80,6 +79,21 @@ func (ti TraceInfo) String() string {
ti.RemoteAddr)
}

// JSON method returns the JSON string of request trace information
func (ti TraceInfo) JSON() string {
buf := acquireBuffer()
defer releaseBuffer(buf)
_ = encodeJSON(buf, ti)
return buf.String()
}

// Clone method returns the clone copy of [TraceInfo]
func (ti TraceInfo) Clone() *TraceInfo {
ti2 := new(TraceInfo)
*ti2 = ti
return ti2
}

// clientTrace struct maps the [httptrace.ClientTrace] hooks into Fields
// with the same naming for easy understanding. Plus additional insights
// [Request].
Expand Down

0 comments on commit 60978d6

Please sign in to comment.