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

WIP: instrument net/http.ServeHTTP implementations #175

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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 @@
*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 @@

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 @@
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
Loading