Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] fibernewrelic add skip middleware next function. #1148

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` |


amantinetti marked this conversation as resolved.
Show resolved Hide resolved
## 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
Loading