Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace client response #37

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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