Skip to main content
Version: websocket_v1.x.x

Fibernewrelic

Release Discord Test Security Linter

NewRelic support for Fiber.

Note: Requires Go 1.18 and above

Install

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/fibernewrelic

Signature

fibernewrelic.New(config fibernewrelic.Config) fiber.Handler

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

Usage

package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fibernewrelic"
)

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

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

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

app.Use(fibernewrelic.New(cfg))

app.Listen(":8080")
}

Usage with existing New Relic application

package main

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

func main() {
newrelicApp, err := newrelic.NewApplication(
newrelic.ConfigAppName("MyCustomApi"),
newrelic.ConfigLicense("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
newrelic.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 := newrelic.FromContext(ctx)
segment := txn.StartSegment("foo segment")
defer segment.End()

// do foo

return nil
})

cfg := fibernewrelic.Config{
Application: newrelicApp,
}

app.Use(fibernewrelic.New(cfg))

app.Listen(":8080")
}