-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gin Framework Support #28
Conversation
Is the |
Should the Transaction be added to the gin Context so that it can be accessed in the handlers? |
I would love to see some error integration into this middleware, so that if I do AbortWithError, that is automatically picked up by the Gin integration and flashed to NR. |
Hi @willnewrelic . We using your middleware. It works great. As you mention in your comment, it would be very useful to have the transaction available in the handlers. Is it possible to add it to the gin.Context? Without the transaction we cannot show segments and other useful stuff. On the other hand, do you now when it would be released? Thanks |
@rodriguezgustavo Just added a new commit that adds a function func handlerAccessTransaction(c *gin.Context) {
if txn := nrgin.Transaction(c); nil != txn {
txn.SetName("custom-name")
}
c.Writer.WriteString("changed the name of the transaction!")
} @jasonab Interesting idea. Upon investigating, I was surprised to see that using |
Looks like someone already made a (broken) PR to gin to fix |
What is the status of this? How does this get in and then also how does it get "approved" for production use? |
HI, we were using this code in production for more than 1 month without any issue (APIs with more than 1 million RPM). Do you know when will you merge this PR to master? |
I'm also very interested in when this integration will become generally available. It sounds like its pretty stable according to @rodriguezgustavo , I'd be willing to try this pre-release out on a pre-release app but I'm unable to get the integration to pull into my vendor dir so can't try it out. |
@rodriguezgustavo could you elaborate on how you are running this? How did you set this up with a basic setup of |
I don't know how @rodriguezgustavo has done it, but what I've done so far myself is implement a simple Middleware for Gin which is added to the handler chain if a This middleware for each request creates the NR transaction, gives it a default name and properties and then puts the transaction on the gin context so the request handler can annotate it further. A simplest middleware would look like this: package middleware
import newrelic "github.com/newrelic/go-agent"
func NewRelic(license string) gin.HandlerFunc {
app := newrelic.NewApplication("My App Name", license)
return func(c *gin.Context) {
tx := app.StartTransaction(c.Request.URL.Path, c.Writer, c.Request)
c.Set("newrelic.tx", tx)
c.Next()
}
} Then in your handler you can call |
Hi @LeoAdamek, Yo can use nrgin.Middleware(newrelicApp) to avoid writting your custom middleware. Then in your gingonic handlers you can access to the transaccion calling to nrgin.Transaction(*gin.Context) |
Hi All This work has been merged into master and is now released. |
@willnewrelic Not sure if I should open a new issue but status codes are not captured correctly in newrelic. For this sample code: ctx.JSON(
http.StatusUnprocessableEntity, // 422
util.ErrorResp(validationErrMsg, vErrs),
) ... newrelic shows the And a header which was sent in the request is missing too. |
Does New Relic correctly capture the HTTP Status Code now of requests? I have data populating in my dashboard, but when a response is 400 or 403 I don't see any errors displayed |
Thanks @AndyEsser for your question. I will submit a ticket for this question and get back to you. |
Are there any updates on what @sudo-suhas reported? I have pretty much the same issue, we raise a 500, the response code is 500 but new relic reports a 200. |
So here's the work around I used:
cfg := newrelic.NewConfig("myapp-"+config.App.Env, config.App.NewrelicLicense)
// With gin, newrelic is not able to capture HTTP response code correctly.
// So we disable the defaults as shown here -
// https://github.com/newrelic/go-agent/blob/master/GUIDE.md#attributes
// and track it ourself.
cfg.Attributes.Exclude = append(cfg.Attributes.Exclude, newrelic.AttributeResponseCode)
var err error
if Agent, err = newrelic.NewApplication(cfg); err != nil {
log.WithError(err).Panic("Newrelic agent spawn failed!")
}
// trackInNR tracks additional attributes for all requests including errors, if
// any. It only tracks the HTTP response status code as newrelic seems unable to
// track it correctly.
func trackInNR(ctx *gin.Context) {
ctx.Next()
if txn := nrgin.Transaction(ctx); txn != nil {
err := txn.AddAttribute(newrelic.AttributeResponseCode, ctx.Writer.Status())
if err != nil {
log.WithError(err).Warn("Got error while trying to track attribute")
}
errMsgs := ctx.Errors.ByType(gin.ErrorTypePublic | gin.ErrorTypePrivate)
if len(errMsgs) > 0 {
qryStr := ctx.Request.URL.RawQuery
txn.AddAttribute("queryParams", qryStr)
for _, errMsg := range errMsgs {
if err := txn.NoticeError(errMsg.Err); err != nil {
log.WithError(err).Warn("Got error while trying to track attribute")
}
// For now, not trying to send the meta to newrelic.
// `txn.AddAttribute` will only accept and send string, numbers
// and booleans. We'd need to flatten all the values recursively
// which might be costly.
}
}
}
}
// and use middleware
router.Use(nrgin.Middleware(monitor.Agent), trackInNR) |
Hi all Regarding the http response codes: Yes, unfortunately using any I have created a PR against Gin to get this fixed here: gin-gonic/gin#1606 Hoping that gets some love soon! |
WARNING: THIS CODE IS IN DEVELOPMENT. IT IS NOT A FINAL NEW RELIC PRODUCT OR OFFERING. DO NOT USE IN PRODUCTION. USE AT YOUR OWN RISK. TERMS AND CONDITIONS APPLY - SEE LICENSE FILE.
Hi All
This is pre-release support for the Gin framework. Let us know what you think. We are eager to get your feedback!