Skip to content

Commit

Permalink
example/aws/request/httptrace: Fix example with timeouts and retries (#…
Browse files Browse the repository at this point in the history
…3499)

The tracing example incorrectly copied the HTTP trace between retry
attempts, instead of replacing it. This update corrects this behavior so
that each request attempt has its own http trace. This also fixes
potential output of negative values.
  • Loading branch information
jasdel committed Aug 21, 2020
1 parent bd83217 commit f4e1349
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions example/aws/request/httptrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
for scanner.Scan() {
trace, err := publishMessage(ctx, svc, topicARN, scanner.Text())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to publish message, %v", err)
fmt.Fprintf(os.Stderr, "failed to publish message, %v\n", err)
}
log.Println(trace)

Expand All @@ -77,7 +77,7 @@ func publishMessage(ctx context.Context, svc *sns.SNS, topic, msg string) (*Requ
Message: &msg,
}, trace.TraceRequest)
if err != nil {
return nil, err
return trace, err
}

return trace, nil
Expand Down
9 changes: 7 additions & 2 deletions example/aws/request/httptrace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ func (t *RequestTrace) TraceRequest(r *request.Request) {
attempt.SignDone = time.Now()
})

// Ensure that the http trace added to the request always uses the original
// context instead of each following attempt's context to prevent conflict
// with previous http traces used.
origContext := r.Context()

// Send
r.Handlers.Send.PushFront(func(rr *request.Request) {
attempt.SendStart = time.Now()
attempt.HTTPTrace = NewHTTPTrace(rr.Context())
attempt.HTTPTrace = NewHTTPTrace(origContext)
rr.SetContext(attempt.HTTPTrace)
})
r.Handlers.Send.PushBack(func(rr *request.Request) {
Expand Down Expand Up @@ -398,7 +403,7 @@ func (t *HTTPTrace) wroteRequest(info httptrace.WroteRequestInfo) {
}

func safeTimeDelta(start, end time.Time) time.Duration {
if start.IsZero() || end.IsZero() {
if start.IsZero() || end.IsZero() || start.After(end) {
return 0
}

Expand Down

0 comments on commit f4e1349

Please sign in to comment.