New Relic
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
| Property | Type | Description | Default |
|---|---|---|---|
| License | string | Required - New Relic License Key | "" |
| AppName | string | New Relic Application Name | fiber-api |
| Enabled | bool | Enable/Disable New Relic | false |
string | "HTTP" | ||
| Application | Application | Existing New Relic App | nil |
| ErrorStatusCodeHandler | func(c fiber.Ctx, err error) int | If you want to change newrelic status code, you can use it. | DefaultErrorStatusCodeHandler |
| Next | func(c fiber.Ctx) bool | Next defines a function to skip this middleware when returned true. | nil |
| RequestHeaderFilter | func(key, value string) bool | Return 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())