Skip to content

Commit

Permalink
Merge pull request #37 from shogo82148/trace-client-response
Browse files Browse the repository at this point in the history
trace client response
  • Loading branch information
shogo82148 authored Mar 30, 2020
2 parents c097852 + ebd5cf0 commit 08a39f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
59 changes: 59 additions & 0 deletions xrayhttp/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package xrayhttp

import (
"context"
"io"
"net/http"
"net/http/httptrace"
"strconv"
"sync"

"github.com/shogo82148/aws-xray-yasdk-go/xray"
"github.com/shogo82148/aws-xray-yasdk-go/xray/schema"
Expand Down Expand Up @@ -64,12 +68,19 @@ func (rt *roundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
}
seg.SetHTTPRequest(requestInfo)

// set trace hooks
ctx, cancel := WithClientTrace(ctx)
defer cancel()
respTracer := &clientResponseTracer{BaseContext: ctx}
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
GotFirstResponseByte: respTracer.GotFirstResponseByte,
})
req = req.WithContext(ctx)

resp, err := rt.Base.RoundTrip(req)
if err != nil {
seg.AddError(err)
respTracer.Close()
return nil, err
}

Expand All @@ -89,5 +100,53 @@ func (rt *roundtripper) RoundTrip(req *http.Request) (*http.Response, error) {
if resp.StatusCode >= 500 && resp.StatusCode < 600 {
seg.SetFault()
}
if resp.StatusCode == http.StatusSwitchingProtocols {
respTracer.Close()
} else {
respTracer.body = resp.Body
resp.Body = respTracer
}
return resp, err
}

type clientResponseTracer struct {
BaseContext context.Context
mu sync.RWMutex
body io.ReadCloser
ctx context.Context
seg *xray.Segment
}

func (r *clientResponseTracer) GotFirstResponseByte() {
r.mu.Lock()
defer r.mu.Unlock()
if r.ctx != nil {
return
}
r.ctx, r.seg = xray.BeginSubsegment(r.BaseContext, "response")
}

func (r *clientResponseTracer) Read(b []byte) (int, error) {
r.mu.RLock()
body := r.body
r.mu.RUnlock()
if body != nil {
return body.Read(b)
}
return 0, io.EOF
}

func (r *clientResponseTracer) Close() error {
r.mu.Lock()
defer r.mu.Unlock()

var err error
if r.body != nil {
err = r.body.Close()
}
if r.ctx != nil {
r.seg.Close()
r.ctx, r.seg = nil, nil
}
return err
}
5 changes: 5 additions & 0 deletions xrayhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func TestClient(t *testing.T) {
},
},
{Name: "request"},
{Name: "response"},
},
},
},
Expand Down Expand Up @@ -223,6 +224,7 @@ func TestClient_StatusTooManyRequests(t *testing.T) {
},
},
{Name: "request"},
{Name: "response"},
},
},
},
Expand Down Expand Up @@ -324,6 +326,7 @@ func TestClient_StatusInternalServerError(t *testing.T) {
},
},
{Name: "request"},
{Name: "response"},
},
},
},
Expand Down Expand Up @@ -442,6 +445,7 @@ func TestClient_TLS(t *testing.T) {
},
},
{Name: "request"},
{Name: "response"},
},
},
},
Expand Down Expand Up @@ -559,6 +563,7 @@ func TestClient_DNS(t *testing.T) {
},
},
{Name: "request"},
{Name: "response"},
},
},
},
Expand Down

0 comments on commit 08a39f9

Please sign in to comment.