newrelic / go-agent

New Relic Go Agent
Apache License 2.0
764 stars 294 forks source link

Integration package for go-chi #450

Open dominic-oconnor opened 2 years ago

dominic-oconnor commented 2 years ago

Add an integration package for the go-chi router

Summary

This page recommends opening a ticket to request new integration packages. This issue was opened in the past but was closed last April without comment, so I'm opening a new issue requesting the addition of this package.

scohen-examity commented 1 year ago

Has anyone implemented an new rekic v3 middleware for go-chi yet?

Meroje commented 1 year ago

Using the .Match() trick from otelchi, I'm doing this

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    routePattern := "NotFoundHandler"
    rctx := chi.NewRouteContext()
    if mux.Match(rctx, r.Method, r.URL.Path) {
        routePattern = rctx.RoutePattern()
    }
    txn := nrapp.StartTransaction(r.Method + " " + routePattern)
    defer txn.End()
    w = txn.SetWebResponse(w)
    txn.SetWebRequestHTTP(r)
    r = newrelic.RequestWithTransactionContext(r, txn)
    mux.ServeHTTP(w, r)
}
danesparza commented 1 year ago

Here is a nice working example of a go-chi middleware using this .Match() trick:

https://github.com/rl404/fairy/blob/956467d5030f87074d2b31964931c8f9db0e76fc/monitoring/newrelic/middleware/http.go

RiskyFeryansyahP commented 1 year ago

Hi, is this PR or issue still pending review?

Can I give some advice on the implementation? Maybe it's better to use it as a public function for middleware directly, instead of wrapping it in NewRouter?

example:

func Middleware(app *newrelic.Application) func(http.Handler) http.Handler {
    return func(next http.Handler) http.Handler {
        fn := func(w http.ResponseWriter, r *http.Request) {
            txn := app.StartTransaction(r.Method + r.URL.RequestURI())
            defer txn.End()

            txn.SetWebRequestHTTP(r)

            w = txn.SetWebResponse(w)
            r = newrelic.RequestWithTransactionContext(r, txn)

            next.ServeHTTP(w, r)

        }

        return http.HandlerFunc(fn)
    }
}

And then when we use chi, we just call that function Middleware to our middleware, example:

package main

...

func main() {
    app, err := newrelic.NewApplication(
        newrelic.ConfigAppName("go-chi App"),
        newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
        newrelic.ConfigDebugLogger(os.Stdout),
    )
    if nil != err {
        fmt.Println(err)
        os.Exit(1)
    }

    r := chi.NewRouter(app)
        r.Use(nrchi.Middleware(app))
    r.Use(middleware.RequestID)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
        ...
}
vithubati commented 4 months ago

Hi, go-chi has been very popular among the go developers, specially for it's light weight implementation. It would be great if we could receive some update on this from New Relic team.

mirackara commented 3 months ago

Hello all,

An integration package for go-chi is currently on our roadmap. While we don't have an exact ETA just yet, we know this is a highly requested integration so we'll post any updates in this thread.