diff --git a/fibernewrelic/README.md b/fibernewrelic/README.md index 28f954ce..c6636fe1 100644 --- a/fibernewrelic/README.md +++ b/fibernewrelic/README.md @@ -29,14 +29,16 @@ 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` | +| 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 diff --git a/fibernewrelic/fiber.go b/fibernewrelic/fiber.go index 16ebd226..f3833279 100644 --- a/fibernewrelic/fiber.go +++ b/fibernewrelic/fiber.go @@ -25,6 +25,9 @@ type Config struct { // 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 } var ConfigDefault = Config{ @@ -33,6 +36,7 @@ var ConfigDefault = Config{ AppName: "fiber-api", Enabled: false, ErrorStatusCodeHandler: DefaultErrorStatusCodeHandler, + Next: nil, } func New(cfg Config) fiber.Handler { @@ -66,6 +70,10 @@ func New(cfg Config) fiber.Handler { } return func(c *fiber.Ctx) error { + if cfg.Next != nil && cfg.Next(c) { + return c.Next() + } + txn := app.StartTransaction(createTransactionName(c)) defer txn.End() diff --git a/fibernewrelic/fiber_test.go b/fibernewrelic/fiber_test.go index 36525aa2..d48308b9 100644 --- a/fibernewrelic/fiber_test.go +++ b/fibernewrelic/fiber_test.go @@ -272,6 +272,70 @@ func TestNewrelicAppConfig(t *testing.T) { assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) assert.True(t, errorStatusCodeHandlerCalled) }) + + t.Run("Jump Newrelic execution if next function is set", + func(t *testing.T) { + app := fiber.New() + + cfg := Config{ + License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + AppName: "", + Enabled: true, + Next: func(c *fiber.Ctx) bool { + return c.OriginalURL() == "/jump" + }, + } + + newRelicApp, _ := newrelic.NewApplication( + newrelic.ConfigAppName(cfg.AppName), + newrelic.ConfigLicense(cfg.License), + newrelic.ConfigEnabled(cfg.Enabled), + ) + + cfg.Application = newRelicApp + + app.Use(New(cfg)) + + app.Get("/jump", func(ctx *fiber.Ctx) error { + return ctx.SendStatus(200) + }) + + r := httptest.NewRequest("GET", "/jump", nil) + resp, _ := app.Test(r, -1) + assert.Equal(t, 200, resp.StatusCode) + }) + + t.Run("Continue Newrelic execution if next function is set", + func(t *testing.T) { + app := fiber.New() + + cfg := Config{ + License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", + AppName: "", + Enabled: true, + Next: func(c *fiber.Ctx) bool { + return c.OriginalURL() == "/jump" + }, + } + + newRelicApp, _ := newrelic.NewApplication( + newrelic.ConfigAppName(cfg.AppName), + newrelic.ConfigLicense(cfg.License), + newrelic.ConfigEnabled(cfg.Enabled), + ) + + cfg.Application = newRelicApp + + app.Use(New(cfg)) + + app.Get("/", func(ctx *fiber.Ctx) error { + return ctx.SendStatus(200) + }) + + r := httptest.NewRequest("GET", "/", nil) + resp, _ := app.Test(r, -1) + assert.Equal(t, 200, resp.StatusCode) + }) } func TestDefaultErrorStatusCodeHandler(t *testing.T) {