From 60978d6c278d523dd43018a069b917be2b1c9be0 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Fri, 24 Jan 2025 22:16:11 -0800 Subject: [PATCH] feat: add json definition and method to trace info - its preparation for upcoming debug log formatter --- request.go | 2 +- request_test.go | 9 +++++++-- trace.go | 40 +++++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/request.go b/request.go index a1b4ec4f..c91ac0e4 100644 --- a/request.go +++ b/request.go @@ -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 diff --git a/request_test.go b/request_test.go index d988c0a0..d3cbcf43 100644 --- a/request_test.go +++ b/request_test.go @@ -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() @@ -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) } }) @@ -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() diff --git a/trace.go b/trace.go index db72278c..94ee9319 100644 --- a/trace.go +++ b/trace.go @@ -9,7 +9,6 @@ import ( "context" "crypto/tls" "fmt" - "net" "net/http/httptrace" "time" ) @@ -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. @@ -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].