跳到主要内容
版本:websocket_v1.x.x

Fibernewrelic

Release Discord Test

Fiber 对 NewRelic 的支持。

注意:需要 Go 1.18 及以上版本

安装

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/fibernewrelic

签名

fibernewrelic.New(config fibernewrelic.Config) fiber.Handler

配置

属性类型描述默认值
Licensestring必需 - New Relic License Key""
AppNamestringNew Relic 应用名称fiber-api
是否启用bool启用/禁用 New Relicfalse
TransportTypestring可以是 HTTP 或 HTTPS (已废弃)"HTTP"
ApplicationApplication现有的 New Relic 应用nil
ErrorStatusCodeHandlerfunc(c *fiber.Ctx, err error) int如果你想改变 newrelic 的状态码,可以使用此配置。DefaultErrorStatusCodeHandler
下一页Nextfunc(c *fiber.Ctx) boolnil

Next 定义一个函数,当返回 true 时跳过此中间件。

package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fibernewrelic"
)

func main() {
app := fiber.New()

app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendStatus(200)
})

cfg := fibernewrelic.Config{
License: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
AppName: "MyCustomApi",
Enabled: true,
}

app.Use(fibernewrelic.New(cfg))

app.Listen(":8080")
}

与现有 New Relic 应用一起使用

package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fibernewrelic"
"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
newrelicApp, err := newrelic.NewApplication(
newrelic.ConfigAppName("MyCustomApi"),
newrelic.ConfigLicense("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
newrelic.ConfigEnabled(true),
)

app := fiber.New()

app.Get("/", func(ctx *fiber.Ctx) error {
return ctx.SendStatus(200)
})

app.Get("/foo", func(ctx *fiber.Ctx) error {
txn := newrelic.FromContext(ctx)
segment := txn.StartSegment("foo segment")
defer segment.End()

// do foo

return nil
})

cfg := fibernewrelic.Config{
Application: newrelicApp,
}

app.Use(fibernewrelic.New(cfg))

app.Listen(":8080")
}