newrelic

package module
v0.0.0-...-ec228b0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 4 Imported by: 54

README

newrelic

New Relic instrumentation for Iris v14, through the official Go agent v3.

The middleware starts a web transaction for each request, reports the response status code to it and stores it on the Context for the handlers.

Install

go get github.com/iris-contrib/middleware/newrelic@main
import nriris "github.com/iris-contrib/middleware/newrelic"

The package is named newrelic, like the agent's own. Import it under another name.

Example

package main

import (
	"fmt"
	"os"

	"github.com/kataras/iris/v14"

	nriris "github.com/iris-contrib/middleware/newrelic"
	"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
	nrApp, err := newrelic.NewApplication(
		newrelic.ConfigAppName("Iris App"),
		newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
	)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	app := iris.New()
	// On the root application Use also runs for requests that match no
	// route, so 404 answers are instrumented too.
	app.Use(nriris.MiddlewareWithFullPath(nrApp))

	// Reported as "GET /users/{id:int}", whatever the id.
	app.Get("/users/{id:int}", func(ctx iris.Context) {
		ctx.Writef("user %d", ctx.Params().GetInt("id"))
	})

	app.Get("/report", func(ctx iris.Context) {
		// The transaction of the current request.
		txn := nriris.Transaction(ctx)
		txn.AddAttribute("report", "monthly")

		ctx.WriteString("done")
	})

	app.Listen(":8000")
}

Functions

Name Description
Middleware(app *newrelic.Application) iris.Handler Names the transaction after the request method and the name of the next handler in the chain.
New(app) The same as Middleware.
MiddlewareWithFullPath(app) Names the transaction after the request method and the path of the route as it was registered, for example GET /users/{id:int}. A request that matched no route is named after the method and the full request URI.
Transaction(ctx) *newrelic.Transaction The transaction of the current request, or nil when the middleware did not run or its app was nil.
GetTransaction(ctx) The same as Transaction, for an iris.Context.
TransactionContextKey The Context key the transaction is stored under.

MiddleareWithFullPath, the earlier misspelled name, still works and is deprecated.

A nil *newrelic.Application turns the middleware into a handler that only calls the next one. That is how to switch instrumentation off in tests or in development.

Transaction names

Middleware takes the name from the handler that follows it in the chain. When more middleware is registered after it, that is the name of the next middleware and not of the route's handler, and every route ends up under one name. Register it last among your middleware, or use MiddlewareWithFullPath.

An anonymous handler function has a compiler-generated name such as main.main.func2. Named functions give readable transaction names.

Tests

The tests use the agent's integrationsupport test application, which never connects and keeps what it would have sent in memory. Transaction names, the reported error for a 5xx response and the wiring of the response writer are covered. Sending the data to New Relic is not.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TransactionContextKey = "newRelicTransaction"

TransactionContextKey is used as the context key in newrelic.New and newrelic.Transaction. Iris requires a string context key. We use two different context keys (and check both in newrelic.Transaction and newrelic.FromContext) rather than use a single string key because context.WithValue will fail golint if used with a string key.

Functions

func GetTransaction

func GetTransaction(ctx iris.Context) *newrelic.Transaction

GetTransaction returns the current request's newrelic transaction. Kept for backwards compatibility, it calls the Transaction function.

func MiddleareWithFullPath deprecated

func MiddleareWithFullPath(app *newrelic.Application) iris.Handler

MiddleareWithFullPath calls MiddlewareWithFullPath.

Deprecated: the name is misspelled. Use MiddlewareWithFullPath instead.

func Middleware

func Middleware(app *newrelic.Application) iris.Handler

Middleware creates an Iris middleware that instruments requests. Each request is a web transaction named after the request's method and the name of the handler that follows the middleware in the chain. When more middleware is registered after it, that is the next middleware and not the route's handler; MiddlewareWithFullPath names the transaction after the route instead.

import nriris "github.com/iris-contrib/middleware/newrelic"
router := iris.New()
router.Use(nriris.Middleware(app))

A nil app makes it a handler that only calls the next one.

func MiddlewareWithFullPath

func MiddlewareWithFullPath(app *newrelic.Application) iris.Handler

MiddlewareWithFullPath is the same as Middleware but it names the transaction after the method and the path of the route as it was registered, for example "GET /users/{id:int}", instead of the method and the handler name. A request that matched no route is named after the method and the full request URI.

func New

func New(app *newrelic.Application) iris.Handler

New calls the Middleware(app). Kept for backwards compatibility.

func Transaction

func Transaction(ctx Context) *newrelic.Transaction

Transaction returns the transaction stored inside the context, or nil if not found.

Types

type Context

type Context interface {
	Value(key interface{}) interface{}
}

Context avoids making this package 1.7+ specific.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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