Skip to content

Commit

Permalink
Merge pull request #441 from mstrYoda/main
Browse files Browse the repository at this point in the history
change newrelic fiber to group transaction metrics under the same route
  • Loading branch information
ReneWerner87 authored Feb 6, 2023
2 parents f03afad + 0cc2657 commit 7efcdb2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 113 deletions.
15 changes: 7 additions & 8 deletions fibernewrelic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ 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 | `"HTTP"` |
| Application | `Application` | Existing New Relic App | `nil` |
| 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` |

### Usage

Expand All @@ -51,7 +51,6 @@ func main() {
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
AppName: "MyCustomApi",
Enabled: true,
TransportType: "HTTP",
}

app.Use(fibernewrelic.New(cfg))
Expand Down
71 changes: 46 additions & 25 deletions fibernewrelic/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package fibernewrelic

import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/newrelic/go-agent/v3/newrelic"
"net/url"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/newrelic/go-agent/v3/newrelic"
)

type Config struct {
Expand All @@ -16,17 +18,17 @@ type Config struct {
// 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
}

var ConfigDefault = Config{
Application: nil,
License: "",
AppName: "fiber-api",
Enabled: false,
TransportType: string(newrelic.TransportHTTP),
Application: nil,
License: "",
AppName: "fiber-api",
Enabled: false,
}

func New(cfg Config) fiber.Handler {
Expand Down Expand Up @@ -55,36 +57,55 @@ func New(cfg Config) fiber.Handler {
}
}

normalizeTransport := strings.ToUpper(cfg.TransportType)
return func(c *fiber.Ctx) error {
txn := app.StartTransaction("")
defer txn.End()

if normalizeTransport != "HTTP" && normalizeTransport != "HTTPS" {
cfg.TransportType = ConfigDefault.TransportType
} else {
cfg.TransportType = normalizeTransport
}
err := c.Next()

return func(c *fiber.Ctx) error {
txn := app.StartTransaction(c.Method() + " " + c.Path())
originalURL, err := url.Parse(c.OriginalURL())
if err != nil {
return c.Next()
method := utils.CopyString(c.Method())
routePath := utils.CopyString(c.Route().Path)
host := string(c.Request().URI().Host())

u := url.URL{
Scheme: string(c.Request().URI().Scheme()),
Host: host,
Path: string(c.Request().URI().Path()),
RawQuery: string(c.Request().URI().QueryString()),
}

txn.SetWebRequest(newrelic.WebRequest{
URL: originalURL,
Method: c.Method(),
Transport: newrelic.TransportType(cfg.TransportType),
Host: c.Hostname(),
URL: &u,
Method: method,
Transport: transport(u.Scheme),
Host: host,
})
txn.SetName(fmt.Sprintf("%s %s", method, routePath))

statusCode := c.Context().Response.StatusCode()

err = c.Next()
if err != nil {
if fiberErr, ok := err.(*fiber.Error); ok {
statusCode = fiberErr.Code
}

txn.NoticeError(err)
}

defer txn.SetWebResponse(nil).WriteHeader(c.Response().StatusCode())
defer txn.End()
txn.SetWebResponse(nil).WriteHeader(statusCode)

return err
}
}

func transport(schema string) newrelic.TransportType {
if strings.HasPrefix(schema, "https") {
return newrelic.TransportHTTPS
}

if strings.HasPrefix(schema, "http") {
return newrelic.TransportHTTP
}

return newrelic.TransportUnknown
}
Loading

0 comments on commit 7efcdb2

Please sign in to comment.