Skip to content

Commit

Permalink
Merge pull request #1148 from FirmexCL/main
Browse files Browse the repository at this point in the history
[feature] fibernewrelic add skip middleware next function.
  • Loading branch information
ReneWerner87 authored Jul 3, 2024
2 parents 484c131 + 6e643b2 commit 2ca8b42
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
18 changes: 10 additions & 8 deletions fibernewrelic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions fibernewrelic/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -33,6 +36,7 @@ var ConfigDefault = Config{
AppName: "fiber-api",
Enabled: false,
ErrorStatusCodeHandler: DefaultErrorStatusCodeHandler,
Next: nil,
}

func New(cfg Config) fiber.Handler {
Expand Down Expand Up @@ -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()

Expand Down
64 changes: 64 additions & 0 deletions fibernewrelic/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2ca8b42

Please sign in to comment.