Skip to content

Commit

Permalink
Merge branch 'main' into evan.li/flush-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
lievan committed Jan 19, 2023
2 parents aa4942a + 2dd2f38 commit 8d5f2b0
Show file tree
Hide file tree
Showing 11 changed files with 1,120 additions and 362 deletions.
28 changes: 8 additions & 20 deletions contrib/gin-gonic/gin/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,27 @@
package gin

import (
"net/http"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo/instrumentation"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo/instrumentation/httpsec"

"github.com/gin-gonic/gin"
)

// useAppSec executes the AppSec logic related to the operation start and
// returns the function to be executed upon finishing the operation
func useAppSec(c *gin.Context, span tracer.Span) func() {
instrumentation.SetAppSecEnabledTags(span)

func useAppSec(c *gin.Context, span tracer.Span) {
var params map[string]string
if l := len(c.Params); l > 0 {
params = make(map[string]string, l)
for _, p := range c.Params {
params[p.Key] = p.Value
}
}

req := c.Request
ipTags, clientIP := httpsec.ClientIPTags(req.Header, req.RemoteAddr)
instrumentation.SetStringTags(span, ipTags)

args := httpsec.MakeHandlerOperationArgs(req, clientIP, params)
ctx, op := httpsec.StartOperation(req.Context(), args)
c.Request = req.WithContext(ctx)

return func() {
events := op.Finish(httpsec.HandlerOperationRes{Status: c.Writer.Status()})
instrumentation.SetTags(span, op.Tags())
if len(events) > 0 {
httpsec.SetSecurityEventTags(span, events, args.Headers, c.Writer.Header())
}
}
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Request = r
c.Next()
})
httpsec.WrapHandler(h, span, params).ServeHTTP(c.Writer, c.Request)
}
Loading

0 comments on commit 8d5f2b0

Please sign in to comment.