Skip to content

Commit

Permalink
GH #370 Capture remote address in trace info
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 9, 2020
1 parent 29cc550 commit 038920c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ func (r *Request) TraceInfo() TraceInfo {
RetryAttempt: r.RetryAttempt,
}

// Calculate the total time accordingly,
// when connection is reused
if ct.gotConnInfo.Reused {
ti.TotalTime = ct.endTime.Sub(ct.getConn)
} else {
Expand All @@ -607,6 +609,11 @@ func (r *Request) TraceInfo() TraceInfo {
ti.ResponseTime = ct.endTime.Sub(ct.gotFirstResponseByte)
}

// Capture remote address info when connection is non-nil
if ct.gotConnInfo.Conn != nil {
ti.RemoteAddr = ct.gotConnInfo.Conn.RemoteAddr()
}

return ti
}

Expand Down
4 changes: 4 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,8 @@ func TestTraceInfo(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

serverAddr := ts.URL[strings.LastIndex(ts.URL, "/")+1:]

client := dc()
client.SetHostURL(ts.URL).EnableTrace()
for _, u := range []string{"/", "/json", "/long-text", "/long-json"} {
Expand All @@ -1660,6 +1662,7 @@ 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)
}

client.DisableTrace()
Expand All @@ -1677,6 +1680,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)
}

// for sake of hook funcs
Expand Down
6 changes: 5 additions & 1 deletion trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package resty
import (
"context"
"crypto/tls"
"net"
"net/http/httptrace"
"time"
)
Expand Down Expand Up @@ -58,10 +59,13 @@ type TraceInfo struct {
// RetryAttempt is to represent the retry attempt made during a Resty
// request execution flow when using the Resty retry feature.
RetryAttempt int

// RemoteAddr returns the remote network address.
RemoteAddr net.Addr
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// CientTrace struct and its methods
// ClientTrace struct and its methods
//_______________________________________________________________________

// tracer struct maps the `httptrace.ClientTrace` hooks into Fields
Expand Down

0 comments on commit 038920c

Please sign in to comment.