Skip to content

Commit 2e3e58b

Browse files
authored
feat: add json definition and method to trace info (#964)
- its preparation for upcoming debug log formatter
1 parent 236685e commit 2e3e58b

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

request.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ func (r *Request) TraceInfo() TraceInfo {
12761276

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

12821282
return ti

request_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,9 @@ func TestTraceInfo(t *testing.T) {
18031803
assertEqual(t, true, tr.TotalTime >= 0)
18041804
assertEqual(t, true, tr.TotalTime < time.Hour)
18051805
assertEqual(t, true, tr.TotalTime == resp.Time())
1806-
assertEqual(t, tr.RemoteAddr.String(), serverAddr)
1806+
assertEqual(t, tr.RemoteAddr, serverAddr)
1807+
1808+
assertNotNil(t, tr.Clone())
18071809
}
18081810

18091811
client.DisableTrace()
@@ -1823,7 +1825,7 @@ func TestTraceInfo(t *testing.T) {
18231825
assertEqual(t, true, tr.ResponseTime >= 0)
18241826
assertEqual(t, true, tr.TotalTime >= 0)
18251827
assertEqual(t, true, tr.TotalTime == resp.Time())
1826-
assertEqual(t, tr.RemoteAddr.String(), serverAddr)
1828+
assertEqual(t, tr.RemoteAddr, serverAddr)
18271829
}
18281830

18291831
})
@@ -1837,6 +1839,9 @@ func TestTraceInfo(t *testing.T) {
18371839
resp, err := c.R().EnableTrace().EnableDebug().Get(u)
18381840
assertNil(t, err)
18391841
assertNotNil(t, resp)
1842+
1843+
jsonStr := resp.Request.TraceInfo().JSON()
1844+
assertEqual(t, true, strings.Contains(jsonStr, serverAddr))
18401845
}
18411846

18421847
logContent := logBuf.String()

trace.go

+27-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"crypto/tls"
1111
"fmt"
12-
"net"
1312
"net/http/httptrace"
1413
"time"
1514
)
@@ -19,45 +18,45 @@ import (
1918
type TraceInfo struct {
2019
// DNSLookup is the duration that transport took to perform
2120
// DNS lookup.
22-
DNSLookup time.Duration
21+
DNSLookup time.Duration `json:"dns_lookup_time"`
2322

2423
// ConnTime is the duration it took to obtain a successful connection.
25-
ConnTime time.Duration
24+
ConnTime time.Duration `json:"connection_time"`
2625

2726
// TCPConnTime is the duration it took to obtain the TCP connection.
28-
TCPConnTime time.Duration
27+
TCPConnTime time.Duration `json:"tcp_connection_time"`
2928

3029
// TLSHandshake is the duration of the TLS handshake.
31-
TLSHandshake time.Duration
30+
TLSHandshake time.Duration `json:"tls_handshake_time"`
3231

3332
// ServerTime is the server's duration for responding to the first byte.
34-
ServerTime time.Duration
33+
ServerTime time.Duration `json:"server_time"`
3534

3635
// ResponseTime is the duration since the first response byte from the server to
3736
// request completion.
38-
ResponseTime time.Duration
37+
ResponseTime time.Duration `json:"response_time"`
3938

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

4342
// IsConnReused is whether this connection has been previously
4443
// used for another HTTP request.
45-
IsConnReused bool
44+
IsConnReused bool `json:"is_connection_reused"`
4645

4746
// IsConnWasIdle is whether this connection was obtained from an
4847
// idle pool.
49-
IsConnWasIdle bool
48+
IsConnWasIdle bool `json:"is_connection_was_idle"`
5049

5150
// ConnIdleTime is the duration how long the connection that was previously
5251
// idle, if IsConnWasIdle is true.
53-
ConnIdleTime time.Duration
52+
ConnIdleTime time.Duration `json:"connection_idle_time"`
5453

5554
// RequestAttempt is to represent the request attempt made during a Resty
5655
// request execution flow, including retry count.
57-
RequestAttempt int
56+
RequestAttempt int `json:"request_attempt"`
5857

5958
// RemoteAddr returns the remote network address.
60-
RemoteAddr net.Addr
59+
RemoteAddr string `json:"remote_address"`
6160
}
6261

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

82+
// JSON method returns the JSON string of request trace information
83+
func (ti TraceInfo) JSON() string {
84+
buf := acquireBuffer()
85+
defer releaseBuffer(buf)
86+
_ = encodeJSON(buf, ti)
87+
return buf.String()
88+
}
89+
90+
// Clone method returns the clone copy of [TraceInfo]
91+
func (ti TraceInfo) Clone() *TraceInfo {
92+
ti2 := new(TraceInfo)
93+
*ti2 = ti
94+
return ti2
95+
}
96+
8397
// clientTrace struct maps the [httptrace.ClientTrace] hooks into Fields
8498
// with the same naming for easy understanding. Plus additional insights
8599
// [Request].

0 commit comments

Comments
 (0)