Skip to content

Commit 1ae37e7

Browse files
authored
Merge pull request #255 from mstrYoda/main
add newrelic middleware, unit tests
2 parents f7b420a + 0f10d8d commit 1ae37e7

File tree

9 files changed

+377
-0
lines changed

9 files changed

+377
-0
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ updates:
4444
- "🤖 Dependencies"
4545
schedule:
4646
interval: "daily"
47+
- package-ecosystem: "gomod"
48+
directory: "/fibernewrelic/" # Location of package manifests
49+
labels:
50+
- "🤖 Dependencies"
51+
schedule:
52+
interval: "daily"

.github/workflows/security.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ jobs:
3535
working-directory: ./casbin
3636
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
3737
# -----
38+
- name: Run Gosec (fibernewrelic)
39+
working-directory: ./fibernewrelic
40+
run: "`go env GOPATH`/bin/gosec -exclude-dir=internal ./..."
41+
# -----

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ jobs:
5050
run: cd ./swagger && go test ./... -v -race
5151
- name: Test casbin Middleware
5252
run: cd ./casbin && go test ./... -v -race
53+
- name: Test fibernewrelic Middleware
54+
run: cd ./fibernewrelic && go test ./... -v -race

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ Repository for third party middlewares with dependencies
2828
* [Casbin](/casbin) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Tests%22">
2929
<img src="https://img.shields.io/github/workflow/status/gofiber/contrib/Tests?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
3030
</a>
31+
* [NewRelic](/fibernewrelic) <a href="https://github.com/gofiber/contrib/actions?query=workflow%3A%22Tests%22">
32+
<img src="https://img.shields.io/github/workflow/status/gofiber/contrib/Tests?label=%F0%9F%A7%AA%20&style=flat&color=75C46B">
33+
</a>

fibernewrelic/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
### NewRelic
2+
![Release](https://img.shields.io/github/release/gofiber/contrib.svg)
3+
[![Discord](https://img.shields.io/discord/704680098577514527?style=flat&label=%F0%9F%92%AC%20discord&color=00ACD7)](https://gofiber.io/discord)
4+
![Test](https://github.com/gofiber/contrib/workflows/Tests/badge.svg)
5+
![Security](https://github.com/gofiber/contrib/workflows/Security/badge.svg)
6+
![Linter](https://github.com/gofiber/contrib/workflows/Linter/badge.svg)
7+
8+
[NewRelic](https://github.com/newrelic/go-agent) support for Fiber.
9+
10+
### Install
11+
12+
```
13+
go get -u github.com/gofiber/fiber/v2
14+
go get -u github.com/gofiber/contrib/fibernewrelic
15+
```
16+
17+
### Signature
18+
19+
```go
20+
fibernewrelic.New(config fibernewrelic.Config) fiber.Handler
21+
```
22+
23+
### Config
24+
25+
| Property | Type | Description | Default |
26+
|:---------------|:---------|:---------------------------------|:------------|
27+
| License | `string` | Required - New Relic License Key | `""` |
28+
| AppName | `string` | New Relic Application Name | `fiber-api` |
29+
| Enabled | `bool` | Enable/Disable New Relic | `false` |
30+
| TransportType | `string` | Can be HTTP or HTTPS | `"HTTP"` |
31+
32+
### Usage
33+
34+
```go
35+
package main
36+
37+
import (
38+
"github.com/gofiber/fiber/v2"
39+
"github.com/gofiber/contrib/fibernewrelic"
40+
)
41+
42+
func main() {
43+
app := fiber.New()
44+
45+
app.Get("/", func(ctx *fiber.Ctx) error {
46+
return ctx.SendStatus(200)
47+
})
48+
49+
cfg := fibernewrelic.Config{
50+
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
51+
AppName: "MyCustomApi",
52+
Enabled: true,
53+
TransportType: "HTTP",
54+
}
55+
56+
app.Use(fibernewrelic.New(cfg))
57+
58+
app.Listen(":8080")
59+
}
60+
```

fibernewrelic/fiber.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package fibernewrelic
2+
3+
import (
4+
"fmt"
5+
"github.com/gofiber/fiber/v2"
6+
"github.com/newrelic/go-agent/v3/newrelic"
7+
"net/url"
8+
)
9+
10+
type Config struct {
11+
// License parameter is required to initialize newrelic application
12+
License string
13+
// AppName parameter is required to initialize newrelic application, default is fiber-api
14+
AppName string
15+
// Enabled parameter passed to enable/disable newrelic
16+
Enabled bool
17+
// TransportType can be HTTP or HTTPS (case-sensitive), default is HTTP
18+
TransportType string
19+
}
20+
21+
var ConfigDefault = Config{
22+
License: "",
23+
AppName: "fiber-api",
24+
Enabled: false,
25+
TransportType: string(newrelic.TransportHTTP),
26+
}
27+
28+
func New(cfg Config) fiber.Handler {
29+
if cfg.TransportType != "HTTP" && cfg.TransportType != "HTTPS" {
30+
cfg.TransportType = ConfigDefault.TransportType
31+
}
32+
33+
if cfg.AppName == "" {
34+
cfg.AppName = ConfigDefault.AppName
35+
}
36+
37+
if cfg.License == "" {
38+
panic(fmt.Errorf("unable to create New Relic Application -> License can not be empty"))
39+
}
40+
41+
app, err := newrelic.NewApplication(
42+
newrelic.ConfigAppName(cfg.AppName),
43+
newrelic.ConfigLicense(cfg.License),
44+
newrelic.ConfigEnabled(cfg.Enabled),
45+
)
46+
47+
if err != nil {
48+
panic(fmt.Errorf("unable to create New Relic Application -> %w", err))
49+
}
50+
51+
return func(c *fiber.Ctx) error {
52+
txn := app.StartTransaction(c.Method() + " " + c.Path())
53+
originalURL, err := url.Parse(c.OriginalURL())
54+
if err != nil {
55+
return c.Next()
56+
}
57+
58+
txn.SetWebRequest(newrelic.WebRequest{
59+
URL: originalURL,
60+
Method: c.Method(),
61+
Transport: newrelic.TransportType(cfg.TransportType),
62+
Host: c.Hostname(),
63+
})
64+
65+
err = c.Next()
66+
if err != nil {
67+
txn.NoticeError(err)
68+
}
69+
70+
defer txn.SetWebResponse(nil).WriteHeader(c.Response().StatusCode())
71+
defer txn.End()
72+
73+
return err
74+
}
75+
}

fibernewrelic/fiber_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package fibernewrelic
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/stretchr/testify/assert"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func TestNewrelicAppConfig(t *testing.T) {
11+
t.Run("Panic occurs when License empty",
12+
func(t *testing.T) {
13+
assert.Panics(t, func() {
14+
New(Config{
15+
License: "",
16+
AppName: "",
17+
Enabled: false,
18+
TransportType: "",
19+
})
20+
})
21+
})
22+
23+
t.Run("Run without panic when License not empty",
24+
func(t *testing.T) {
25+
assert.NotPanics(t, func() {
26+
New(Config{
27+
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
28+
AppName: "",
29+
Enabled: false,
30+
TransportType: "",
31+
})
32+
})
33+
})
34+
35+
t.Run("Run successfully as middleware",
36+
func(t *testing.T) {
37+
app := fiber.New()
38+
39+
app.Get("/", func(ctx *fiber.Ctx) error {
40+
return ctx.SendStatus(200)
41+
})
42+
43+
cfg := Config{
44+
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
45+
AppName: "",
46+
Enabled: true,
47+
TransportType: "",
48+
}
49+
50+
app.Use(New(cfg))
51+
52+
r := httptest.NewRequest("GET", "/", nil)
53+
resp, _ := app.Test(r, -1)
54+
55+
assert.Equal(t, 200, resp.StatusCode)
56+
})
57+
}

fibernewrelic/go.mod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module github.com/gofiber/contrib/fibernewrelic
2+
3+
go 1.18
4+
5+
require (
6+
github.com/gofiber/fiber/v2 v2.35.0
7+
github.com/newrelic/go-agent/v3 v3.17.0
8+
github.com/stretchr/testify v1.5.1
9+
)
10+
11+
require (
12+
github.com/andybalholm/brotli v1.0.4 // indirect
13+
github.com/davecgh/go-spew v1.1.0 // indirect
14+
github.com/golang/protobuf v1.4.3 // indirect
15+
github.com/klauspost/compress v1.15.9 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/valyala/bytebufferpool v1.0.0 // indirect
18+
github.com/valyala/fasthttp v1.38.0 // indirect
19+
github.com/valyala/tcplisten v1.0.0 // indirect
20+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
21+
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 // indirect
22+
golang.org/x/text v0.3.7 // indirect
23+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
24+
google.golang.org/grpc v1.39.0 // indirect
25+
google.golang.org/protobuf v1.25.0 // indirect
26+
gopkg.in/yaml.v2 v2.2.3 // indirect
27+
)

0 commit comments

Comments
 (0)