Skip to content

Commit 8a29e30

Browse files
authored
Fix TraceInfo on failed connections (#342)
Fixes #338 * Fix TraceInfo on failed connections * Only calculate TCPConnTime, ConnTime, and ResponseTime for successful connections * Sequentially sort clientTrace struct members * Sort clientTrace struct members based upon the normally trace hook execution sequence
1 parent e7fae0d commit 8a29e30

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

Diff for: request.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Reque
324324

325325
// SetMultipartFormData method allows simple form data to be attached to the request as `multipart:form-data`
326326
func (r *Request) SetMultipartFormData(data map[string]string) *Request {
327-
328327
for k, v := range data {
329328
r = r.SetMultipartField(k, "", "", strings.NewReader(v))
330329
}
@@ -575,18 +574,32 @@ func (r *Request) TraceInfo() TraceInfo {
575574
return TraceInfo{}
576575
}
577576

578-
return TraceInfo{
577+
ti := TraceInfo{
579578
DNSLookup: ct.dnsDone.Sub(ct.dnsStart),
580-
ConnTime: ct.gotConn.Sub(ct.getConn),
581-
TCPConnTime: ct.connectDone.Sub(ct.dnsDone),
582579
TLSHandshake: ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart),
583580
ServerTime: ct.gotFirstResponseByte.Sub(ct.gotConn),
584-
ResponseTime: ct.endTime.Sub(ct.gotFirstResponseByte),
585581
TotalTime: ct.endTime.Sub(ct.dnsStart),
586582
IsConnReused: ct.gotConnInfo.Reused,
587583
IsConnWasIdle: ct.gotConnInfo.WasIdle,
588584
ConnIdleTime: ct.gotConnInfo.IdleTime,
589585
}
586+
587+
// Only calcuate on successful connections
588+
if !ct.connectDone.IsZero() {
589+
ti.TCPConnTime = ct.connectDone.Sub(ct.dnsDone)
590+
}
591+
592+
// Only calcuate on successful connections
593+
if !ct.gotConn.IsZero() {
594+
ti.ConnTime = ct.gotConn.Sub(ct.getConn)
595+
}
596+
597+
// Only calcuate on successful connections
598+
if !ct.gotFirstResponseByte.IsZero() {
599+
ti.ResponseTime = ct.endTime.Sub(ct.gotFirstResponseByte)
600+
}
601+
602+
return ti
590603
}
591604

592605
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

Diff for: request_test.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,6 @@ func TestReportMethodSupportsPayload(t *testing.T) {
14761476

14771477
assertError(t, err)
14781478
assertEqual(t, http.StatusOK, resp.StatusCode())
1479-
14801479
}
14811480

14821481
func TestRequestQueryStringOrder(t *testing.T) {
@@ -1648,6 +1647,25 @@ func TestTraceInfoWithoutEnableTrace(t *testing.T) {
16481647
}
16491648
}
16501649

1650+
func TestTraceInfoOnTimeout(t *testing.T) {
1651+
client := dc()
1652+
client.SetHostURL("http://resty-nowhere.local").EnableTrace()
1653+
1654+
resp, err := client.R().Get("/")
1655+
assertNotNil(t, err)
1656+
assertNotNil(t, resp)
1657+
1658+
tr := resp.Request.TraceInfo()
1659+
assertEqual(t, true, tr.DNSLookup >= 0)
1660+
assertEqual(t, true, tr.ConnTime == 0)
1661+
assertEqual(t, true, tr.TLSHandshake == 0)
1662+
assertEqual(t, true, tr.TCPConnTime == 0)
1663+
assertEqual(t, true, tr.ServerTime == 0)
1664+
assertEqual(t, true, tr.ResponseTime == 0)
1665+
assertEqual(t, true, tr.TotalTime > 0)
1666+
assertEqual(t, true, tr.TotalTime == resp.Time())
1667+
}
1668+
16511669
func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
16521670
ts := createFilePostServer(t)
16531671
defer ts.Close()
@@ -1683,7 +1701,8 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
16831701
SetFormData(map[string]string{
16841702
"first_name": "Alex",
16851703
"last_name": strings.Repeat("C", int(debugBodySizeLimit)),
1686-
"zip_code": "00001"}).
1704+
"zip_code": "00001",
1705+
}).
16871706
SetBasicAuth("myuser", "mypass").
16881707
Post(formTs.URL + "/profile")
16891708
assertNil(t, err)
@@ -1696,7 +1715,8 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
16961715
SetFormData(map[string]string{
16971716
"first_name": "Alex",
16981717
"last_name": "C",
1699-
"zip_code": "00001"}).
1718+
"zip_code": "00001",
1719+
}).
17001720
SetBasicAuth("myuser", "mypass").
17011721
Post(formTs.URL + "/profile")
17021722
assertNil(t, err)

Diff for: trace.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ type TraceInfo struct {
6565
// Request.
6666
type clientTrace struct {
6767
getConn time.Time
68-
gotConn time.Time
69-
connectDone time.Time
70-
gotFirstResponseByte time.Time
7168
dnsStart time.Time
7269
dnsDone time.Time
70+
connectDone time.Time
7371
tlsHandshakeStart time.Time
7472
tlsHandshakeDone time.Time
73+
gotConn time.Time
74+
gotFirstResponseByte time.Time
7575
endTime time.Time
7676
gotConnInfo httptrace.GotConnInfo
7777
}

0 commit comments

Comments
 (0)