From 5a0c9bba7ea7daaf02a0b5ee4e7027d5d4b876f4 Mon Sep 17 00:00:00 2001 From: Rodrigo Arguello Date: Mon, 14 Oct 2024 13:36:31 +0200 Subject: [PATCH] review comments --- contrib/internal/httptrace/before_handle.go | 4 ++++ .../httprouter/internal/tracing/config.go | 4 ++-- .../httprouter/internal/tracing/tracing.go | 9 ++++++++- internal/appsec/emitter/httpsec/http.go | 13 +++++++++++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/contrib/internal/httptrace/before_handle.go b/contrib/internal/httptrace/before_handle.go index 12f954f32e..afd40726ed 100644 --- a/contrib/internal/httptrace/before_handle.go +++ b/contrib/internal/httptrace/before_handle.go @@ -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) diff --git a/contrib/julienschmidt/httprouter/internal/tracing/config.go b/contrib/julienschmidt/httprouter/internal/tracing/config.go index 533ec2a7cd..07ee86a765 100644 --- a/contrib/julienschmidt/httprouter/internal/tracing/config.go +++ b/contrib/julienschmidt/httprouter/internal/tracing/config.go @@ -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 { diff --git a/contrib/julienschmidt/httprouter/internal/tracing/tracing.go b/contrib/julienschmidt/httprouter/internal/tracing/tracing.go index 47c1211c62..26d46b1e79 100644 --- a/contrib/julienschmidt/httprouter/internal/tracing/tracing.go +++ b/contrib/julienschmidt/httprouter/internal/tracing/tracing.go @@ -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 diff --git a/internal/appsec/emitter/httpsec/http.go b/internal/appsec/emitter/httpsec/http.go index 7ed885b12d..9f81cdc20e 100644 --- a/internal/appsec/emitter/httpsec/http.go +++ b/internal/appsec/emitter/httpsec/http.go @@ -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 {