GithubHelp home page GithubHelp logo

Gin Framework support about go-agent HOT 5 CLOSED

newrelic avatar newrelic commented on August 18, 2024
Gin Framework support

from go-agent.

Comments (5)

bbrodriges avatar bbrodriges commented on August 18, 2024 3

Well, you can add newrelic to gin fairly easy:

func NewrelicMiddleware(appName string, key string) gin.HandlerFunc {

    if appName == "" || key == "" {
        return func(c *gin.Context) {}
    }

    config := newrelic.NewConfig(appName, key)
    app, err := newrelic.NewApplication(config)

    if err != nil {
                panic(err)
        }

    return func(c *gin.Context) {
        txn := app.StartTransaction(c.Request.URL.Path, c.Writer, c.Request)
        defer txn.End()
        c.Next()
    }
}

from go-agent.

johnzeng avatar johnzeng commented on August 18, 2024

@bbrodriges I have tried this but it's not getting the errors from my response, so I think this is not good enough for me. I think a better way is put the txn into the gin.Context so I can make more segment and set error for it.

from go-agent.

bbrodriges avatar bbrodriges commented on August 18, 2024

@johnzeng totally agree with you. I have wrote a small wrapper to log errors with new relic transaction in gin context:

// WrapError records error using New Relic transaction
// and returns err back to caller function.
// Example:
//    myint, err := strconv.Atoi("golang")
//    if newrelic.WrapError(c, err) != nil {
//       panic(err)
//    }
func WrapError(c *gin.Context, err error) error {
    if err != nil {
        if txn, ok := c.Get("nrtxn"); ok {
            if nrtxn, ok := txn.(newrelic.Transaction); ok {
                nrtxn.NoticeError(err)
            }
        }
    }

    return err
}

from go-agent.

willnewrelic avatar willnewrelic commented on August 18, 2024

Hi All,

Proposed Gin support available here: #28

Give it a try! We are eager to get your feedback.

from go-agent.

johnzeng avatar johnzeng commented on August 18, 2024

Great, I think I can close this issue now.

from go-agent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.