Skip to content

Commit

Permalink
contrib/gorilla/mux: simplify AppSecParams struct to PathParams strin…
Browse files Browse the repository at this point in the history
…g map
  • Loading branch information
Hellzy committed Jan 12, 2022
1 parent 8ab0f33 commit 05a98ab
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
5 changes: 1 addition & 4 deletions contrib/gorilla/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"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/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo/instrumentation/httpsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -125,9 +124,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
FinishOpts: r.config.finishOpts,
SpanOpts: spanopts,
QueryParams: r.config.queryParams,
AppSecParams: httpsec.AppSecParams{
PathParams: match.Vars,
},
PathParams: match.Vars,
})
}

Expand Down
4 changes: 2 additions & 2 deletions contrib/internal/httputil/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type TraceConfig struct {
QueryParams bool // specifies that request query parameters should be appended to http.url tag
FinishOpts []ddtrace.FinishOption // span finish options to be applied
SpanOpts []ddtrace.StartSpanOption // additional span options to be applied
AppSecParams httpsec.AppSecParams // extra parameters that AppSec may need while wrapping the request handler
PathParams map[string]string // path parameters needed for AppSec checks
}

// TraceAndServe will apply tracing to the given http.Handler using the passed tracer under the given service and resource.
Expand Down Expand Up @@ -56,7 +56,7 @@ func TraceAndServe(h http.Handler, cfg *TraceConfig) {
defer span.Finish(cfg.FinishOpts...)

if appsec.Enabled() {
h = httpsec.WrapHandler(h, span, cfg.AppSecParams)
h = httpsec.WrapHandler(h, span, cfg.PathParams)
}
h.ServeHTTP(wrapResponseWriter(cfg.ResponseWriter, span), cfg.Request.WithContext(ctx))
}
Expand Down
14 changes: 4 additions & 10 deletions internal/appsec/dyngo/instrumentation/httpsec/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,15 @@ type (
// Status corresponds to the address `server.response.status`.
Status int
}

// AppSecParams is the extra parameters that need to be passed over to the AppSec WrapHandler (i.e params
// not retrievable from the http.Request, such as path parameters)
AppSecParams struct {
PathParams map[string]string
}
)

// WrapHandler wraps the given HTTP handler with the abstract HTTP operation defined by HandlerOperationArgs and
// HandlerOperationRes.
func WrapHandler(handler http.Handler, span ddtrace.Span, params AppSecParams) http.Handler {
func WrapHandler(handler http.Handler, span ddtrace.Span, pathParams map[string]string) http.Handler {
SetAppSecTags(span)

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
args := MakeHandlerOperationArgs(r, params.PathParams)
args := MakeHandlerOperationArgs(r, pathParams)
op := StartOperation(
args,
nil,
Expand Down Expand Up @@ -84,7 +78,7 @@ func WrapHandler(handler http.Handler, span ddtrace.Span, params AppSecParams) h
// MakeHandlerOperationArgs creates the HandlerOperationArgs out of a standard
// http.Request along with the given current span. It returns an empty structure
// when appsec is disabled.
func MakeHandlerOperationArgs(r *http.Request, params map[string]string) HandlerOperationArgs {
func MakeHandlerOperationArgs(r *http.Request, pathParams map[string]string) HandlerOperationArgs {
headers := make(http.Header, len(r.Header))
for k, v := range r.Header {
k := strings.ToLower(k)
Expand Down Expand Up @@ -112,7 +106,7 @@ func MakeHandlerOperationArgs(r *http.Request, params map[string]string) Handler
// TODO(Julio-Guerra): avoid actively parsing the query string and move to a lazy monitoring of this value with
// the dynamic instrumentation of the Query() method.
Query: r.URL.Query(),
PathParams: params,
PathParams: pathParams,
}
}

Expand Down

0 comments on commit 05a98ab

Please sign in to comment.