登录
首页 >  Golang >  Go问答

集成NewRelic与Gin-Gonic

来源:stackoverflow

时间:2024-02-14 18:30:26 328浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《集成NewRelic与Gin-Gonic》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我正在尝试将 go 应用程序与 newrelic 集成,通过下面的代码,我可以在 new relic 中看到我的 api 事务。

import (
   "github.com/newrelic/go-agent/v3/integration/nrgin"
   "github.com/newrelic/go-agent/v3/newrelic"
  )

 var router = gin.Default()

 nr, _ := newrelic.NewApplication( 
          newrelic.ConfigAppName("TestApp"),
          newrelic.ConfigLicense("NRKEY"),
)
 router.Use(nrgin.Middleware(nr))

 router.GET("/user", userHandler)

使用上面的示例代码,当发起获取用户请求时,我可以在 newrelic 事务中看到 api 详细信息。但问题是 newrelic 中的数据库查询痕迹是空的。

我找到了一种通过在各个模型中添加 newrelic.datastoresegment 来获取查询详细信息的方法,但我不想要这种方法,因为我有很多模型。

我可以在路由器中处理这种情况吗?


正确答案


您需要传递函数调用请求上下文。

例如:

...
 router.GET("/user", userHandler) 
...


func userHandler(c *gin.Context){
     ctx := c.Request.Context()
     databaseCall(ctx)
}

func databaseCall(ctx context.Context) {
     db.ExecContext(ctx, ...)
}

此外,您还需要使用 newrelic 数据库集成。

集成:https://github.com/newrelic/go-agent/tree/master/v3/integrations

要检查示例,请参阅每个集成内的示例文件夹。

例如mysql:https://github.com/newrelic/go-agent/blob/master/v3/integrations/nrmysql/example/main.go

好了,本文到此结束,带大家了解了《集成NewRelic与Gin-Gonic》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>