I am using the gin-gonic, newrelic go-agent v3, and nrgin v3
I am trying to figure out how to propagate a custom error message from a Handler so that it shows up in New Relic.
What I see now is that when I have a handler that returns with a http.StatusInternalServerError, the error message shows up in New Relic as "500: Internal Server Error".
I'd like to figure out how to have that be suffixed with the message in the error being thrown.
Here's what I'm doing now, and it's clearly not working:
err := errors.New("This is a custom error message!")
c.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Thanks!
I think you have to set the error on the transaction manually.
The nrgin
middleware doesn't do anything after c.Next
, which is where you would typically inspect errors, however it does set the newrelic.Transaction
into the Gin context:
// file: github.com/newrelic/go-agent/v3/integrations/nrgin/nrgin.go
func middleware(app *newrelic.Application, useNewNames bool) gin.HandlerFunc {
return func(c *gin.Context) {
if app != nil {
// ... omitted
c.Set(internal.GinTransactionContextKey, txn)
}
c.Next()
// here is where you would typically inspect Gin errors,
// i.e. after the handler chain
}
}
So inside your Gin handler, extract the newrelic.Transaction
and set errors on it:
err := errors.New("This is a custom error message!")
txn := nrgin.Transaction(c)
txn.NoticeError(err)