如何在戈朗为新遗物创建通用或全局上下文(戈朗新遗物集成)?

我正在main.go中创建新的遗物事务,并且必须将其传递给处理程序,然后传递到控制器等。有没有办法我可以全局定义它,然后可以在任何处理程序,控制器或数据库事务中访问?


import(

    "github.com/gin-gonic/gin"

    newrelic "github.com/newrelic/go-agent"

)


// main.go

func newrelicHandler() (gin.HandlerFunc, newrelic.Application) {

    newrelicConfig := newrelic.NewConfig("NEW_RELIC_APP_NAME", "NEW_RELIC_APP_KEY")

    app, err := newrelic.NewApplication(newrelicConfig)

    if err != nil {

        return func(c *gin.Context) {

            c.Next()

        }, app

    }

    return func(c *gin.Context) {

        txn := app.StartTransaction(c.Request.URL.Path, c.Writer, c.Request)

        c.Set("newRelicTransaction", txn)

        apm, ok := c.Get("newRelicTransaction")

        defer txn.End()

        c.Next()

    }, app

}


func main() {

    r := gin.New()

    x, app := newrelicHandler()

    r.Use(x)

}



// test/handler.go


func (t *TestHandler) Display(c *gin.Context) {

    apmTxn, ok := c.Get("newRelicTransaction")


    test, err := t.testCtrl.Display(apmTxn)

    if err != nil {

        return err

    }


    c.JSON(http.StatusOK, gin.H{"message": "success"})

}



// test/controller.go


func (t *TestCtrl) Display(txn interface{}) {

    apmTxn, ok := txn.(newrelic.Transaction)

    if !ok {

        fmt.Println("ERR")

    }

    segment := newrelic.StartSegment(apmTxn, "SomeSegmentName")

    // get data

    segment.End()

    return 

}


森栏
浏览 92回答 1
1回答

慕沐林林

避免使用全局上下文,而是在入口点创建一个全局上下文,然后将其作为参数传递给需要它的任何函数。您可以使用Gin框架提供的包。nrgin并在功能中main()创建纽瑞克的实例 -newrelic.NewApplication(cfg)调用在新功能实例中传递的 - 函数。这会将 Gin 事务上下文键 - 添加到上下文中。nrgin.Middleware(app)newRelicTransaction将步骤 2 中的函数注册为所有路由的中间件 -router.Use(nrgin.Middleware(app))然后,您可以将相同的上下文对象传递给可以接受类型的参数的其他函数,因为它只是实现Go的接口。context.Contextgin.Contextcontext示例代码import "github.com/newrelic/go-agent/_integrations/nrgin/v1"func main() {    cfg := newrelic.NewConfig("Gin App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))        app, err := newrelic.NewApplication(cfg)    if nil != err {        fmt.Println(err)    }    router := gin.Default()    router.Use(nrgin.Middleware(app))    router.GET("/example-get", GetController)    router.Run(":80")}func GetController(c *gin.Context) {    if txn := nrgin.Transaction(c); nil != txn {        txn.SetName("custom-name")    }    databaseGet(c)    c.Writer.WriteString("example response...")}func databaseGet(c context.Context) {    if txn := nrgin.Transaction(c); nil != txn {        txn.SetName("custom-name")    }    c.Writer.WriteString("example response...")}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go