New Relic
New Relic support for Fiber.
Incoming request headers are forwarded to New Relic transactions by default. This enables distributed tracing header processing, but can also forward sensitive headers. Use RequestHeaderFilter to allowlist or redact headers as needed.
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"
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. | nil (forward all headers) |
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())