File tree 2 files changed +21
-6
lines changed 2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -1251,13 +1251,16 @@ func (r *Request) TraceInfo() TraceInfo {
1251
1251
RequestAttempt : r .Attempt ,
1252
1252
}
1253
1253
1254
- // Calculate the total time accordingly,
1255
- // when connection is reused
1256
- if ct .gotConnInfo .Reused {
1257
- ti .TotalTime = ct .endTime .Sub (ct .getConn )
1258
- } else {
1259
- ti .TotalTime = ct .endTime .Sub (ct .dnsStart )
1254
+ // Calculate the total time accordingly when connection is reused,
1255
+ // and DNS start and get conn time may be zero if the request is invalid.
1256
+ // See issue #1016.
1257
+ requestStartTime := r .Time
1258
+ if ct .gotConnInfo .Reused && ! ct .getConn .IsZero () {
1259
+ requestStartTime = ct .getConn
1260
+ } else if ! ct .dnsStart .IsZero () {
1261
+ requestStartTime = ct .dnsStart
1260
1262
}
1263
+ ti .TotalTime = ct .endTime .Sub (requestStartTime )
1261
1264
1262
1265
// Only calculate on successful connections
1263
1266
if ! ct .connectDone .IsZero () {
Original file line number Diff line number Diff line change @@ -1830,6 +1830,18 @@ func TestTraceInfo(t *testing.T) {
1830
1830
1831
1831
})
1832
1832
1833
+ t .Run ("enable trace on invalid request, issue #1016" , func (t * testing.T ) {
1834
+ resp , err := client .R ().EnableTrace ().Get ("unknown://url.com" )
1835
+ assertNotNil (t , err )
1836
+ tr := resp .Request .TraceInfo ()
1837
+ assertEqual (t , true , tr .DNSLookup == 0 )
1838
+ assertEqual (t , true , tr .ConnTime == 0 )
1839
+ assertEqual (t , true , tr .TLSHandshake == 0 )
1840
+ assertEqual (t , true , tr .ServerTime == 0 )
1841
+ assertEqual (t , true , tr .ResponseTime == 0 )
1842
+ assertEqual (t , true , tr .TotalTime > 0 && tr .TotalTime < time .Second )
1843
+ })
1844
+
1833
1845
t .Run ("enable trace and debug on request" , func (t * testing.T ) {
1834
1846
c , logBuf := dcldb ()
1835
1847
c .SetBaseURL (ts .URL )
You can’t perform that action at this time.
0 commit comments