Repository: go-resty/resty
Pull request #1018 (2025-05-11 02:01:14)

backport: potential wrong total time in trace info from PR #1017
This PR backports PR #1017 to v2.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/go-resty/resty/pull/1018?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.71%. Comparing base [(`39c56b9`)](https://app.codecov.io/gh/go-resty/resty/commit/39c56b9ac6804b4a7060f17085c90df258b4e1d4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty) to head [(`794d9ac`)](https://app.codecov.io/gh/go-resty/resty/commit/794d9ac68ffac98009663c8beaf3f7b76ecb83e0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##               v2    #1018   +/-   ##
=======================================
  Coverage   95.70%   95.71%           
=======================================
  Files          14       14           
  Lines        2211     2216    +5     
=======================================
+ Hits         2116     2121    +5     
  Misses         71       71           
  Partials       24       24           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/go-resty/resty/pull/1018?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-resty).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/request.go b/request.go
index 6c6f925..956c956 100644
--- a/request.go
+++ b/request.go
@@ -907,13 +907,16 @@ func (r *Request) TraceInfo() TraceInfo {
 		RequestAttempt: r.Attempt,
 	}
 
-	// Calculate the total time accordingly,
-	// when connection is reused
-	if ct.gotConnInfo.Reused {
-		ti.TotalTime = ct.endTime.Sub(ct.getConn)
-	} else {
-		ti.TotalTime = ct.endTime.Sub(ct.dnsStart)
+	// Calculate the total time accordingly when connection is reused,
+	// and DNS start and get conn time may be zero if the request is invalid.
+	// See issue #1016.
+	requestStartTime := r.Time
+	if ct.gotConnInfo.Reused && !ct.getConn.IsZero() {
+		requestStartTime = ct.getConn
+	} else if !ct.dnsStart.IsZero() {
+		requestStartTime = ct.dnsStart
 	}
+	ti.TotalTime = ct.endTime.Sub(requestStartTime)
 
 	// Only calculate on successful connections
 	if !ct.connectDone.IsZero() {


Test patch:
diff --git a/request_test.go b/request_test.go
index 0d61e66..43825f1 100644
--- a/request_test.go
+++ b/request_test.go
@@ -2103,6 +2103,19 @@ func TestTraceInfoWithoutEnableTrace(t *testing.T) {
 	}
 }
 
+func TestTraceInfoWithInvalidRequest(t *testing.T) {
+	client := dc()
+	resp, err := client.R().EnableTrace().Get("unknown://url.com")
+	assertNotNil(t, err)
+	tr := resp.Request.TraceInfo()
+	assertEqual(t, true, tr.DNSLookup == 0)
+	assertEqual(t, true, tr.ConnTime == 0)
+	assertEqual(t, true, tr.TLSHandshake == 0)
+	assertEqual(t, true, tr.ServerTime == 0)
+	assertEqual(t, true, tr.ResponseTime == 0)
+	assertEqual(t, true, tr.TotalTime > 0 && tr.TotalTime < time.Second)
+}
+
 func TestTraceInfoOnTimeout(t *testing.T) {
 	client := dc()
 	client.SetHostURL("http://resty-nowhere.local").EnableTrace()