Documentation ¶
Overview ¶
Package nrgorilla instruments https://github.com/gorilla/mux applications.
Use this package to instrument inbound requests handled by a gorilla mux.Router. Use the nrgorilla.Middleware as the first middleware registered with your router.
Complete example: https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrgorilla/example/main.go
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InstrumentRoutes
deprecated
func Middleware ¶
func Middleware(app *newrelic.Application) mux.MiddlewareFunc
Middleware creates a new mux.MiddlewareFunc. When used, this middleware will create a transaction for each inbound request. The transaction will be available in the Request's context throughout the call chain, including in any other middlewares that are registered after this one. For this reason, it is important for this middleware to be registered first.
Note that mux.MiddlewareFuncs are not called for the NotFoundHandler or MethodNotAllowedHandler. To instrument these handlers, use newrelic.WrapHandle (https://godoc.org/github.com/newrelic/go-agent/v3/newrelic#WrapHandle).
Note that if you are moving from the now deprecated InstrumentRoutes to this Middleware, the reported time of your transactions may increase. This is expected and nothing to worry about. This method includes in the transaction total time request time that is spent in other custom middlewares whereas InstrumentRoutes does not.
Example ¶
package main import ( "net/http" "github.com/gorilla/mux" "github.com/newrelic/go-agent/v3/integrations/nrgorilla" newrelic "github.com/newrelic/go-agent/v3/newrelic" ) var ( app *newrelic.Application MyCustomMiddleware mux.MiddlewareFunc ) func makeHandler(text string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(text)) }) } func main() { r := mux.NewRouter() r.Use(nrgorilla.Middleware(app)) // All handlers and custom middlewares will be instrumented. The // transaction will be available in the Request's context. r.Use(MyCustomMiddleware) r.Handle("/", makeHandler("index")) http.ListenAndServe(":8000", r) }
Output:
Example (SpecialHandlers) ¶
package main import ( "net/http" "github.com/gorilla/mux" "github.com/newrelic/go-agent/v3/integrations/nrgorilla" newrelic "github.com/newrelic/go-agent/v3/newrelic" ) var app *newrelic.Application func makeHandler(text string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(text)) }) } func main() { r := mux.NewRouter() r.Use(nrgorilla.Middleware(app)) // The NotFoundHandler and MethodNotAllowedHandler must be instrumented // separately using newrelic.WrapHandle. The second argument to // newrelic.WrapHandle is used as the transaction name; the string returned // from newrelic.WrapHandle should be ignored. _, r.NotFoundHandler = newrelic.WrapHandle(app, "NotFoundHandler", makeHandler("not found")) _, r.MethodNotAllowedHandler = newrelic.WrapHandle(app, "MethodNotAllowedHandler", makeHandler("method not allowed")) }
Output:
func WrapRouter ¶ added in v1.2.0
WrapRouter extracts API endpoints from the router object passed to it 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)
Types ¶
This section is empty.