Skip to content

Commit

Permalink
bugfix: preserve optional interfaces on http.ResponseWriter (#91)
Browse files Browse the repository at this point in the history
* bugfix: preserves optional interfaces on http.ResponseWriter

* add test for preserving optional interfaces

* removes support for go 1.7 and 1.8
  • Loading branch information
chrisradek authored and luluzhao committed Mar 14, 2019
1 parent 9bde012 commit 6818260
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 407 deletions.
20 changes: 4 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ go_import_path: github.com/aws/aws-xray-sdk-go
sudo: required

go:
- "1.7"
- "1.8"
- "1.9"
- "1.10"
- "1.11"
Expand All @@ -25,28 +23,20 @@ env:

before_install:
- echo $TRAVIS_GO_VERSION
- if [ $TRAVIS_GO_VERSION == "1.7" ] ||
[ $TRAVIS_GO_VERSION == "1.8" ]; then
export DEP_MANAGEMENT_TOOL="GLIDE";
elif [ $TRAVIS_GO_VERSION == "1.9" ] ||
- if [ $TRAVIS_GO_VERSION == "1.9" ] ||
[ $TRAVIS_GO_VERSION == "1.10" ]; then
export DEP_MANAGEMENT_TOOL="DEP";
else
export DEP_MANAGEMENT_TOOL="MODULE";
fi
- echo $DEP_MANAGEMENT_TOOL
- if [ $DEP_MANAGEMENT_TOOL == "GLIDE" ]; then
go get -v github.com/Masterminds/glide;
cd $GOPATH/src/github.com/Masterminds/glide && git checkout 67790b3dbede72cfdc54aa53be4706322c9499e0 && go install && cd -;
elif [ $DEP_MANAGEMENT_TOOL == "DEP" ]; then
- if [ $DEP_MANAGEMENT_TOOL == "DEP" ]; then
curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep;
chmod +x $GOPATH/bin/dep;
fi

install:
- if [ $DEP_MANAGEMENT_TOOL == "GLIDE" ]; then
glide install;
elif [ $DEP_MANAGEMENT_TOOL == "DEP" ]; then
- if [ $DEP_MANAGEMENT_TOOL == "DEP" ]; then
dep ensure;
else
go get;
Expand All @@ -57,6 +47,4 @@ script:

matrix:
allow_failures:
- go: tip
- go: "1.7"
- go: "1.8"
- go: tip
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Installing

The AWS X-Ray SDK for Go is compatible with Go 1.7 and above.
The AWS X-Ray SDK for Go is compatible with Go 1.9 and above.

Install the SDK using the following command (The SDK's non-testing dependencies will be installed):
Use `go get` to retrieve the SDK to add it to your `GOPATH` workspace, or your project's Go module dependencies (Go 1.11 and up):
Expand Down
29 changes: 7 additions & 22 deletions xray/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,21 @@ func httpTrace(seg *Segment, h http.Handler, w http.ResponseWriter, r *http.Requ
w.Header().Set(TraceIDHeaderKey, respHeader.String())
seg.Unlock()

resp := &responseCapturer{w, 200, 0}
capturer := &responseCapturer{w, 200, 0}
resp := capturer.wrappedResponseWriter()
h.ServeHTTP(resp, r)

seg.Lock()
seg.GetHTTP().GetResponse().Status = resp.status
seg.GetHTTP().GetResponse().ContentLength, _ = strconv.Atoi(resp.Header().Get("Content-Length"))
seg.GetHTTP().GetResponse().Status = capturer.status
seg.GetHTTP().GetResponse().ContentLength, _ = strconv.Atoi(capturer.Header().Get("Content-Length"))

if resp.status >= 400 && resp.status < 500 {
if capturer.status >= 400 && capturer.status < 500 {
seg.Error = true
}
if resp.status == 429 {
if capturer.status == 429 {
seg.Throttle = true
}
if resp.status >= 500 && resp.status < 600 {
if capturer.status >= 500 && capturer.status < 600 {
seg.Fault = true
}
seg.Unlock()
Expand All @@ -190,22 +191,6 @@ func clientIP(r *http.Request) (string, bool) {
return ip, false
}

type responseCapturer struct {
http.ResponseWriter
status int
length int
}

func (w *responseCapturer) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}

func (w *responseCapturer) Write(data []byte) (int, error) {
w.length += len(data)
return w.ResponseWriter.Write(data)
}

func btoi(b bool) int {
if b {
return 1
Expand Down
31 changes: 30 additions & 1 deletion xray/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
package xray

import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"context"
"github.com/stretchr/testify/assert"
"os"
"time"

"github.com/stretchr/testify/assert"
)

func TestNewFixedSegmentName(t *testing.T) {
Expand Down Expand Up @@ -155,3 +157,30 @@ func TestNonRootHandler(t *testing.T) {
assert.Equal(t, "reqid", s.ParentID)
assert.Equal(t, true, s.Sampled)
}

func TestXRayHandlerPreservesOptionalInterfaces(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, isCloseNotifier := w.(http.CloseNotifier)
_, isFlusher := w.(http.Flusher)
_, isHijacker := w.(http.Hijacker)
_, isPusher := w.(http.Pusher)
_, isReaderFrom := w.(io.ReaderFrom)

assert.True(t, isCloseNotifier)
assert.True(t, isFlusher)
assert.True(t, isHijacker)
assert.True(t, isReaderFrom)
// Pusher is only available when using http/2, so should not be present
assert.False(t, isPusher)

w.WriteHeader(202)
})

ts := httptest.NewServer(Handler(NewFixedSegmentNamer("test"), handler))
defer ts.Close()

req := httptest.NewRequest("GET", ts.URL, strings.NewReader(""))

_, err := http.DefaultTransport.RoundTrip(req)
assert.NoError(t, err)
}
184 changes: 0 additions & 184 deletions xray/httptrace_go17.go

This file was deleted.

Loading

0 comments on commit 6818260

Please sign in to comment.