Skip to content

Commit

Permalink
Merge pull request #922 from k2io/develop
Browse files Browse the repository at this point in the history
Changes Required by CSEC Agent
  • Loading branch information
nr-swilloughby authored Jul 2, 2024
2 parents 668533d + 96eecf0 commit 3ed8bf7
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 79 deletions.
24 changes: 12 additions & 12 deletions v3/integrations/nrecho-v3/nrecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func handlerName(router interface{}) string {
}
}

func transactionName(c echo.Context) string {
func transactionName(c echo.Context) (string, string) {
ptr := handlerPointer(c.Handler())
if ptr == handlerPointer(echo.NotFoundHandler) {
return "NotFoundHandler"
return "NotFoundHandler", ""
}
if ptr == handlerPointer(echo.MethodNotAllowedHandler) {
return "MethodNotAllowedHandler"
return "MethodNotAllowedHandler", ""
}
return c.Request().Method + " " + c.Path()
return c.Request().Method + " " + c.Path(), c.Path()
}

// Middleware creates Echo middleware that instruments requests.
Expand All @@ -77,9 +77,10 @@ func Middleware(app *newrelic.Application) func(echo.HandlerFunc) echo.HandlerFu
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
rw := c.Response().Writer
txn := app.StartTransaction(transactionName(c))
tName, route := transactionName(c)
txn := app.StartTransaction(tName)
defer txn.End()

txn.SetCsecAttributes(newrelic.AttributeCsecRoute, route)
txn.SetWebRequestHTTP(c.Request())

c.Response().Writer = txn.SetWebResponse(rw)
Expand Down Expand Up @@ -112,14 +113,13 @@ func Middleware(app *newrelic.Application) func(echo.HandlerFunc) echo.HandlerFu
// which is used to detect application URL mapping(api-endpoints) for provable security.
// In this version of the integration, this wrapper is only necessary if you are using the New Relic security agent integration [https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrsecurityagent],
// but it may be enhanced to provide additional functionality in future releases.
// e := echo.New()
// ....
// ....
// ....
//
// nrecho.WrapRouter(e)
// e := echo.New()
// ....
// ....
// ....
//

// nrecho.WrapRouter(e)
func WrapRouter(engine *echo.Echo) {
if engine != nil && newrelic.IsSecurityAgentPresent() {
router := engine.Routes()
Expand Down
24 changes: 12 additions & 12 deletions v3/integrations/nrecho-v4/nrecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func handlerPointer(handler echo.HandlerFunc) uintptr {
return reflect.ValueOf(handler).Pointer()
}

func transactionName(c echo.Context) string {
func transactionName(c echo.Context) (string, string) {
ptr := handlerPointer(c.Handler())
if ptr == handlerPointer(echo.NotFoundHandler) {
return "NotFoundHandler"
return "NotFoundHandler", ""
}
if ptr == handlerPointer(echo.MethodNotAllowedHandler) {
return "MethodNotAllowedHandler"
return "MethodNotAllowedHandler", ""
}
return c.Request().Method + " " + c.Path()
return c.Request().Method + " " + c.Path(), c.Path()
}

// Skipper defines a function to skip middleware. Returning true skips processing
Expand Down Expand Up @@ -100,9 +100,10 @@ func Middleware(app *newrelic.Application, opts ...ConfigOption) func(echo.Handl
}

rw := c.Response().Writer
txn := config.App.StartTransaction(transactionName(c))
tname, path := transactionName(c)
txn := config.App.StartTransaction(tname)
defer txn.End()

txn.SetCsecAttributes(newrelic.AttributeCsecRoute, path)
txn.SetWebRequestHTTP(c.Request())

c.Response().Writer = txn.SetWebResponse(rw)
Expand Down Expand Up @@ -135,14 +136,13 @@ func Middleware(app *newrelic.Application, opts ...ConfigOption) func(echo.Handl
// which is used to detect application URL mapping(api-endpoints) for provable security.
// In this version of the integration, this wrapper is only necessary if you are using the New Relic security agent integration [https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrsecurityagent],
// but it may be enhanced to provide additional functionality in future releases.
// e := echo.New()
// ....
// ....
// ....
//
// nrecho.WrapRouter(e)
// e := echo.New()
// ....
// ....
// ....
//

// nrecho.WrapRouter(e)
func WrapRouter(engine *echo.Echo) {
if engine != nil && newrelic.IsSecurityAgentPresent() {
router := engine.Routes()
Expand Down
2 changes: 2 additions & 0 deletions v3/integrations/nrfasthttp/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ func WrapHandle(app *newrelic.Application, pattern string, handler fasthttp.Requ
fasthttpadaptor.ConvertRequest(ctx, r, true)
resp := fasthttpWrapperResponse{ctx: ctx}

txn.SetCsecAttributes(newrelic.AttributeCsecRoute, pattern)
txn.SetWebResponse(resp)
txn.SetWebRequestHTTP(r)

handler(ctx)
if newrelic.IsSecurityAgentPresent() {
newrelic.GetSecurityAgentInterface().SendEvent("INBOUND_WRITE", resp.Body(), resp.Header())
newrelic.GetSecurityAgentInterface().SendEvent("INBOUND_RESPONSE_CODE", ctx.Response.StatusCode())
}
}
}
12 changes: 6 additions & 6 deletions v3/integrations/nrgin/nrgin.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ func MiddlewareHandlerTxnNames(app *newrelic.Application) gin.HandlerFunc {
// which is used to detect application URL mapping(api-endpoints) for provable security.
// In this version of the integration, this wrapper is only necessary if you are using the New Relic security agent integration [https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrsecurityagent],
// but it may be enhanced to provide additional functionality in future releases.
// router := gin.Default()
// ....
// ....
// ....
//
// nrgin.WrapRouter(router)
// router := gin.Default()
// ....
// ....
// ....
//

// nrgin.WrapRouter(router)
func WrapRouter(engine *gin.Engine) {
if engine != nil && newrelic.IsSecurityAgentPresent() {
router := engine.Routes()
Expand All @@ -171,6 +170,7 @@ func middleware(app *newrelic.Application, useNewNames bool) gin.HandlerFunc {

w := &headerResponseWriter{w: c.Writer}
txn := app.StartTransaction(name, newrelic.WithFunctionLocation(c.Handler()))
txn.SetCsecAttributes(newrelic.AttributeCsecRoute, c.FullPath())
txn.SetWebRequestHTTP(c.Request)
defer txn.End()

Expand Down
24 changes: 18 additions & 6 deletions v3/integrations/nrgorilla/nrgorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func routeName(r *http.Request) string {
return r.Method + " " + n
}

func handlerName(r *http.Request) string {
route := mux.CurrentRoute(r)
if nil == route {
return r.RequestURI
}
if n, _ := route.GetPathTemplate(); n != "" {
return n
} else {
return r.RequestURI
}
}

// InstrumentRoutes instruments requests through the provided mux.Router. Use
// this after the routes have been added to the router.
//
Expand Down Expand Up @@ -104,6 +116,7 @@ func Middleware(app *newrelic.Application) mux.MiddlewareFunc {
name := routeName(r)
txn := app.StartTransaction(name)
defer txn.End()
txn.SetCsecAttributes(newrelic.AttributeCsecRoute, handlerName(r))
txn.SetWebRequestHTTP(r)
w = txn.SetWebResponse(w)
r = newrelic.RequestWithTransactionContext(r, txn)
Expand All @@ -116,14 +129,13 @@ func Middleware(app *newrelic.Application) mux.MiddlewareFunc {
// which is used to detect application URL mapping(api-endpoints) for provable security.
// In this version of the integration, this wrapper is only necessary if you are using the New Relic security agent integration [https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrsecurityagent],
// but it may be enhanced to provide additional functionality in future releases.
// r := mux.NewRouter()
// ....
// ....
// ....
//
// nrgorilla.WrapRouter(router)
// r := mux.NewRouter()
// ....
// ....
// ....
//

// nrgorilla.WrapRouter(router)
func WrapRouter(router *mux.Router) {
if router != nil && newrelic.IsSecurityAgentPresent() {
router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
Expand Down
Loading

0 comments on commit 3ed8bf7

Please sign in to comment.