Skip to content

Commit

Permalink
WIP: instrument net/http.ServeHTTP implementations
Browse files Browse the repository at this point in the history
TODO - description

Fixes #173
  • Loading branch information
nsrip-dd committed Oct 11, 2024
1 parent 0acfd19 commit 71230cd
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 196 deletions.
31 changes: 31 additions & 0 deletions _integration-tests/tests/net_http/net_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ type TestCase struct {
*http.Server
}

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

Check failure on line 31 in _integration-tests/tests/net_http/net_http.go

View workflow job for this annotation

GitHub Actions / Go Linters

unused-receiver: method receiver 'h' is not referenced in method's body, consider removing or renaming it as _ (revive)
defer r.Body.Close()

b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

Check failure on line 37 in _integration-tests/tests/net_http/net_http.go

View workflow job for this annotation

GitHub Actions / Go Linters

unhandled-error: Unhandled error in call to function net/http.ResponseWriter.Write (revive)
return
}

w.WriteHeader(http.StatusOK)
w.Write(b)

Check failure on line 42 in _integration-tests/tests/net_http/net_http.go

View workflow job for this annotation

GitHub Actions / Go Linters

unhandled-error: Unhandled error in call to function net/http.ResponseWriter.Write (revive)
}

func (tc *TestCase) Setup(t *testing.T) {
mux := http.NewServeMux()
tc.Server = &http.Server{
Expand All @@ -35,6 +51,21 @@ func (tc *TestCase) Setup(t *testing.T) {

mux.HandleFunc("/hit", tc.handleHit)
mux.HandleFunc("/", tc.handleRoot)
// TODO: hit these endpoints, check for spans
mux.Handle("/handler", handler{})
mux.HandleFunc("/literal", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

Check failure on line 62 in _integration-tests/tests/net_http/net_http.go

View workflow job for this annotation

GitHub Actions / Go Linters

unhandled-error: Unhandled error in call to function net/http.ResponseWriter.Write (revive)
return
}

w.WriteHeader(http.StatusOK)
w.Write(b)
})

go func() { assert.ErrorIs(t, tc.Server.ListenAndServe(), http.ErrServerClosed) }()
}
Expand Down
18 changes: 18 additions & 0 deletions instrument/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ func resourceNamer(r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
}

// MaybeWrapHandler instruments an http.Handler if it is nil or an *http.ServeMux,
// and otherwise returns the same handler.
// This is intended for instrumenting http.ListenAndServe, etc,
// where we may already be passed an instrumented handler.
// We assume it is already instrumented unless it's a net/http handler.
func MaybeWrapHandler(handler http.Handler) http.Handler {
if handler == nil {
handler = http.DefaultServeMux

Check warning on line 31 in instrument/http.go

View check run for this annotation

Codecov / codecov/patch

instrument/http.go#L29-L31

Added lines #L29 - L31 were not covered by tests
}
switch handler.(type) {
case *http.ServeMux, *http.HandlerFunc:
return WrapHandler(handler)
default:

Check warning on line 36 in instrument/http.go

View check run for this annotation

Codecov / codecov/patch

instrument/http.go#L33-L36

Added lines #L33 - L36 were not covered by tests
// Don't change it, it may have already been instrumented
return handler

Check warning on line 38 in instrument/http.go

View check run for this annotation

Codecov / codecov/patch

instrument/http.go#L38

Added line #L38 was not covered by tests
}
}

func WrapHandler(handler http.Handler) http.Handler {
return httptrace.WrapHandler(handler, "", "", httptrace.WithResourceNamer(resourceNamer))
// TODO: We'll reintroduce this later when we stop hard-coding dd-trace-go as above.
Expand Down
52 changes: 11 additions & 41 deletions internal/injector/builtin/generated.go

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

1 change: 0 additions & 1 deletion internal/injector/builtin/generated_deps.go

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

17 changes: 10 additions & 7 deletions internal/injector/builtin/testdata/server/chiv5.go.snap

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

24 changes: 14 additions & 10 deletions internal/injector/builtin/testdata/server/gorilla.go.snap

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

2 changes: 1 addition & 1 deletion internal/injector/builtin/testdata/server/main.go.snap

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

109 changes: 35 additions & 74 deletions internal/injector/builtin/testdata/server/other_handlers.go.snap

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

Loading

0 comments on commit 71230cd

Please sign in to comment.