Skip to content

Commit

Permalink
Merge branch 'main' into christian/gocql-report-cluster-name-and-data…
Browse files Browse the repository at this point in the history
…center
  • Loading branch information
darccio authored Jul 9, 2024
2 parents 960b8ce + 79fd68c commit 1c2d7c2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
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

0 comments on commit 1c2d7c2

Please sign in to comment.