Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rarguelloF committed Oct 14, 2024
1 parent 55426a1 commit 5a0c9bb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions contrib/internal/httptrace/before_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type ServeConfig struct {
SpanOpts []ddtrace.StartSpanOption
}

// BeforeHandle contains functionality that should be executed before a http.Handler runs.
// It returns the "traced" http.ResponseWriter and http.Request, an additional afterHandle function
// that should be executed after the Handler runs, and a handled bool that instructs if the request has been handled
// or not - in case it was handled, the original handler should not run.
func BeforeHandle(cfg *ServeConfig, w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request, func(), bool) {
if cfg == nil {
cfg = new(ServeConfig)
Expand Down
4 changes: 2 additions & 2 deletions contrib/julienschmidt/httprouter/internal/tracing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
const defaultServiceName = "http.router"

type Config struct {
serviceName string
headerTags *internal.LockMap
spanOpts []ddtrace.StartSpanOption
serviceName string
analyticsRate float64
headerTags *internal.LockMap
}

func NewConfig(opts ...Option) *Config {
Expand Down
9 changes: 8 additions & 1 deletion contrib/julienschmidt/httprouter/internal/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ type Param interface {
GetValue() string
}

func BeforeHandle[T any, WT Router](cfg *Config, router T, wrapRouter func(T) WT, w http.ResponseWriter, req *http.Request) (http.ResponseWriter, *http.Request, func(), bool) {
// BeforeHandle is an adapter of httptrace.BeforeHandle for julienschmidt/httprouter types.
func BeforeHandle[T any, WT Router](
cfg *Config,
router T,
wrapRouter func(T) WT,
w http.ResponseWriter,
req *http.Request,
) (http.ResponseWriter, *http.Request, func(), bool) {
wRouter := wrapRouter(router)
// get the resource associated to this request
route := req.URL.Path
Expand Down
13 changes: 11 additions & 2 deletions internal/appsec/emitter/httpsec/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,17 @@ func makeCookies(parsed []*http.Cookie) map[string][]string {
return cookies
}

func BeforeHandle(w http.ResponseWriter, r *http.Request, span ddtrace.Span, pathParams map[string]string, opts *Config) (
http.ResponseWriter, *http.Request, func(), bool) {
// BeforeHandle contains the appsec functionality that should be executed before a http.Handler runs.
// It returns the modified http.ResponseWriter and http.Request, an additional afterHandle function
// that should be executed after the Handler runs, and a handled bool that instructs if the request has been handled
// or not - in case it was handled, the original handler should not run.
func BeforeHandle(
w http.ResponseWriter,
r *http.Request,
span ddtrace.Span,
pathParams map[string]string,
opts *Config,
) (http.ResponseWriter, *http.Request, func(), bool) {
if opts == nil {
opts = defaultWrapHandlerConfig
} else if opts.ResponseHeaderCopier == nil {
Expand Down

0 comments on commit 5a0c9bb

Please sign in to comment.