Skip to content

Commit

Permalink
contrib/net/http: implement rwUnwrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF committed Jul 8, 2024
1 parent 891aad4 commit 3f70895
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
45 changes: 45 additions & 0 deletions contrib/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package http

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

"gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/namingschematest"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
Expand All @@ -21,6 +23,7 @@ import (
"gopkg.in/DataDog/dd-trace-go.v1/internal/normalizer"

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

func TestWithHeaderTags(t *testing.T) {
Expand Down Expand Up @@ -523,6 +526,48 @@ func TestServerNamingSchema(t *testing.T) {
namingschematest.NewHTTPServerTest(genSpans, "http.router")(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) {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *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"))
})

h := WrapHandler(fn, "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)
}

func router(muxOpts ...Option) http.Handler {
defaultOpts := []Option{
WithServiceName("my-service"),
Expand Down
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.

0 comments on commit 3f70895

Please sign in to comment.