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

contrib/net/http: implement rwUnwrapper #2775

Merged
merged 5 commits into from
Jul 8, 2024
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
5 changes: 5 additions & 0 deletions contrib/net/http/make_responsewriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions contrib/net/http/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ func (w *responseWriter) WriteHeader(status int) {
w.ResponseWriter.WriteHeader(status)
w.status = status
}

// Unwrap returns the underlying wrapped http.ResponseWriter.
func (w *responseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}
5 changes: 5 additions & 0 deletions contrib/net/http/trace_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions contrib/net/http/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"

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

func TestTraceAndServe(t *testing.T) {
Expand Down Expand Up @@ -357,6 +359,48 @@ func TestTraceAndServeHost(t *testing.T) {
})
}

// TestUnwrap tests the implementation of the rwUnwrapper interface, which is used internally
// by the standard library: https://github.com/golang/go/blob/6d89b38ed86e0bfa0ddaba08dc4071e6bb300eea/src/net/http/responsecontroller.go#L42-L44
// See also: https://github.com/DataDog/dd-trace-go/issues/2674
func TestUnwrap(t *testing.T) {
h := WrapHandler(deadlineHandler, "service-name", "resource-name")
srv := httptest.NewServer(h)
defer srv.Close()

resp, err := srv.Client().Get(srv.URL + "/")
require.NoError(t, err)
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
respText := string(b)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "OK", respText)
}

var deadlineHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
rc := http.NewResponseController(w)

// Use the SetReadDeadline and SetWriteDeadline methods, which are not explicitly implemented in the trace_gen.go
// generated file. Before the Unwrap change, these methods returned a "feature not supported" error.

err := rc.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = rc.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})

type noopHandler struct{}

func (noopHandler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {}
Expand Down
Loading