-1

I am trying to integrate go app with newrelic and with below code I am able to see my api transactions in new relic.

 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) 

with above sample code, when get user request is initiated I am able to see the api details in newrelic transactions. But the problem is database query traces are empty in newrelic.

I found a way to get query details by adding newrelic.DatastoreSegment in individual models, but I don't want this approach because I have many models.

Is there anyway I can handle this situation in router?

Vikranth Kumar
  • 59
  • 1
  • 1
  • 4

1 Answers1

0

You need to pass along the function call the request context.

For instance:

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


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

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

Also, you need to use the newrelic database integration.

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

To check an example, see the example folder inside each integration.

For instance, mysql: https://github.com/newrelic/go-agent/blob/master/v3/integrations/nrmysql/example/main.go

Lucas Katayama
  • 4,445
  • 27
  • 34