Skip to main content
Version: v3_prometheus_v0.x.x

New Relic

Release Discord Test

New Relic support for Fiber.

Only the headers the New Relic agent itself consumes are forwarded to New Relic transactions by default: the distributed tracing headers (traceparent, tracestate, newrelic), the synthetic monitor headers (X-NewRelic-Synthetics, X-NewRelic-Synthetics-Info) and the queue timing headers (X-Request-Start, X-Queue-Start). Any other header, including W3C baggage, has to be opted into with RequestHeaderFilter. The default is exported as DefaultRequestHeaderFilter, so a custom filter can build on top of it instead of restating the allowlist.

Compatible with Fiber v3.

Go version support

We only support the latest two versions of Go. Visit https://go.dev/doc/devel/release for more information.

Install

go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/v3/newrelic

Signature

middleware.New(config middleware.Config) fiber.Handler
middleware.FromContext(ctx any) *nr.Transaction // nr "github.com/newrelic/go-agent/v3/newrelic"
middleware.DefaultRequestHeaderFilter(key, value string) bool

FromContext accepts a fiber.Ctx, fiber.CustomCtx, *fasthttp.RequestCtx, or a standard context.Context (e.g. the value returned by c.Context() when PassLocalsToContext is enabled). It returns an *nr.Transaction (a New Relic transaction from github.com/newrelic/go-agent/v3/newrelic).

Config

PropertyTypeDescriptionDefault
LicensestringRequired - New Relic License Key""
AppNamestringNew Relic Application Namefiber-api
EnabledboolEnable/Disable New Relicfalse
TransportTypestringCan be HTTP or HTTPS (Deprecated)"HTTP"
ApplicationApplicationExisting New Relic Appnil
ErrorStatusCodeHandlerfunc(c fiber.Ctx, err error) intIf you want to change newrelic status code, you can use it.DefaultErrorStatusCodeHandler
Nextfunc(c fiber.Ctx) boolNext defines a function to skip this middleware when returned true.nil
RequestHeaderFilterfunc(key, value string) boolReturn true to forward a request header to New Relic, false to skip it.DefaultRequestHeaderFilter (distributed tracing, synthetics and queue timing headers only)

Usage

package main

import (
"github.com/gofiber/fiber/v3"
middleware "github.com/gofiber/contrib/v3/newrelic"
)

func main() {
app := fiber.New()

app.Get("/", func(ctx fiber.Ctx) error {
return ctx.SendStatus(200)
})

cfg := middleware.Config{
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
AppName: "MyCustomApi",
Enabled: true,
}

app.Use(middleware.New(cfg))

app.Listen(":8080")
}

Usage with existing New Relic application

package main

import (
"github.com/gofiber/fiber/v3"
middleware "github.com/gofiber/contrib/v3/newrelic"
nr "github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
nrApp, err := nr.NewApplication(
nr.ConfigAppName("MyCustomApi"),
nr.ConfigLicense("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
nr.ConfigEnabled(true),
)

app := fiber.New()

app.Get("/", func(ctx fiber.Ctx) error {
return ctx.SendStatus(200)
})

app.Get("/foo", func(ctx fiber.Ctx) error {
txn := middleware.FromContext(ctx)
segment := txn.StartSegment("foo segment")
defer segment.End()

// do foo

return nil
})

cfg := middleware.Config{
Application: nrApp,
}

app.Use(middleware.New(cfg))

app.Listen(":8080")
}

Retrieving the transaction with PassLocalsToContext

When fiber.Config{PassLocalsToContext: true} is set, the New Relic transaction stored by the middleware is also available in the underlying context.Context. Use FromContext with any of the supported context types:

// From a fiber.Ctx (most common usage)
txn := middleware.FromContext(c)

// From the underlying context.Context (useful in service layers or when PassLocalsToContext is enabled)
txn := middleware.FromContext(c.Context())