fibernewrelic

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 6 Imported by: 7

README


id: fibernewrelic

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

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
TransportType string Can be HTTP or HTTPS (Deprecated) "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

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")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Application:            nil,
	License:                "",
	AppName:                "fiber-api",
	Enabled:                false,
	ErrorStatusCodeHandler: DefaultErrorStatusCodeHandler,
	Next:                   nil,
}

Functions

func DefaultErrorStatusCodeHandler added in v1.1.0

func DefaultErrorStatusCodeHandler(c *fiber.Ctx, err error) int

func FromContext added in v1.2.0

func FromContext(c *fiber.Ctx) *newrelic.Transaction

FromContext returns the Transaction from the context if present, and nil otherwise.

func New

func New(cfg Config) fiber.Handler

Types

type Config

type Config struct {
	// License parameter is required to initialize newrelic application
	License string
	// AppName parameter passed to set app name, default is fiber-api
	AppName string
	// Enabled parameter passed to enable/disable newrelic
	Enabled bool
	// TransportType can be HTTP or HTTPS, default is HTTP
	// Deprecated: The Transport type now acquiring from request URL scheme internally
	TransportType string
	// Application field is required to use an existing newrelic application
	Application *newrelic.Application
	// ErrorStatusCodeHandler is executed when an error is returned from handler
	// Optional. Default: DefaultErrorStatusCodeHandler
	ErrorStatusCodeHandler func(c *fiber.Ctx, err error) int
	// Next defines a function to skip this middleware when returned true.
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool
}

Jump to

Keyboard shortcuts

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