nrgorilla

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 4 Imported by: 15

README

v3/integrations/nrgorilla GoDoc

Package nrgorilla instruments https://github.com/gorilla/mux applications.

import "github.com/newrelic/go-agent/v3/integrations/nrgorilla"

For more information, see godocs.

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 InstrumentRoutes(r *mux.Router, app *newrelic.Application) *mux.Router

InstrumentRoutes instruments requests through the provided mux.Router. Use this after the routes have been added to the router.

Deprecated: Use the newer and more complete Middleware method instead.

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

func WrapRouter(router *mux.Router)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL