Skip to content

Commit

Permalink
xray: Added a const TraceIDHeaderKey with x-amzn-trace-id. (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
raninho authored and luluzhao committed Feb 15, 2019
1 parent 1eb1439 commit 8c8a645
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion xray/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
const RequestIDKey string = "request_id"
const ExtendedRequestIDKey string = "id_2"
const S3ExtendedRequestIDHeaderKey string = "x-amz-id-2"
const TraceIDHeaderKey = "x-amzn-trace-id"

type jsonMap struct {
object interface{}
Expand Down Expand Up @@ -63,7 +64,7 @@ var xRayBeforeValidateHandler = request.NamedHandler{
marshalctx, _ := BeginSubsegment(ctx, "marshal")

r.HTTPRequest = r.HTTPRequest.WithContext(marshalctx)
r.HTTPRequest.Header.Set("x-amzn-trace-id", opseg.DownstreamHeader().String())
r.HTTPRequest.Header.Set(TraceIDHeaderKey, opseg.DownstreamHeader().String())
},
}

Expand Down
2 changes: 1 addition & 1 deletion xray/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (rt *roundtripper) RoundTrip(r *http.Request) (*http.Response, error) {
seg.GetHTTP().GetRequest().Method = r.Method
seg.GetHTTP().GetRequest().URL = r.URL.String()

r.Header.Set("x-amzn-trace-id", seg.DownstreamHeader().String())
r.Header.Set(TraceIDHeaderKey, seg.DownstreamHeader().String())
seg.Unlock()

resp, err = rt.Base.RoundTrip(r)
Expand Down
6 changes: 3 additions & 3 deletions xray/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func HandlerWithContext(ctx context.Context, sn SegmentNamer, h http.Handler) ht
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := sn.Name(r.Host)

traceHeader := header.FromString(r.Header.Get("x-amzn-trace-id"))
traceHeader := header.FromString(r.Header.Get(TraceIDHeaderKey))

r = r.WithContext(ctx)
c, seg := NewSegmentFromHeader(r.Context(), name, traceHeader)
Expand All @@ -110,7 +110,7 @@ func Handler(sn SegmentNamer, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := sn.Name(r.Host)

traceHeader := header.FromString(r.Header.Get("x-amzn-trace-id"))
traceHeader := header.FromString(r.Header.Get(TraceIDHeaderKey))
ctx, seg := NewSegmentFromHeader(r.Context(), name, traceHeader)

r = r.WithContext(ctx)
Expand Down Expand Up @@ -155,7 +155,7 @@ func httpTrace(seg *Segment, h http.Handler, w http.ResponseWriter, r *http.Requ
respHeader.WriteString(strconv.Itoa(btoi(seg.Sampled)))
}

w.Header().Set("x-amzn-trace-id", respHeader.String())
w.Header().Set(TraceIDHeaderKey, respHeader.String())
seg.Unlock()

resp := &responseCapturer{w, 200, 0}
Expand Down
4 changes: 2 additions & 2 deletions xray/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestHandlerWithContextForNonRootHandler(t *testing.T) {
defer ts.Close()

req := httptest.NewRequest("DELETE", ts.URL, strings.NewReader(""))
req.Header.Set("x-amzn-trace-id", "Root=fakeid; Parent=reqid; Sampled=1")
req.Header.Set(TraceIDHeaderKey, "Root=fakeid; Parent=reqid; Sampled=1")

_, err := http.DefaultTransport.RoundTrip(req)
assert.NoError(t, err)
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestNonRootHandler(t *testing.T) {
defer ts.Close()

req := httptest.NewRequest("DELETE", ts.URL, strings.NewReader(""))
req.Header.Set("x-amzn-trace-id", "Root=fakeid; Parent=reqid; Sampled=1")
req.Header.Set(TraceIDHeaderKey, "Root=fakeid; Parent=reqid; Sampled=1")

_, err := http.DefaultTransport.RoundTrip(req)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion xray/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type XRayHeaders struct {
}

func ParseHeadersForTest(h http.Header) XRayHeaders {
traceHeader := header.FromString(h.Get("x-amzn-trace-id"))
traceHeader := header.FromString(h.Get(TraceIDHeaderKey))

return XRayHeaders{
RootTraceID: traceHeader.TraceID,
Expand Down

0 comments on commit 8c8a645

Please sign in to comment.