go-agent
go-agent copied to clipboard
Integration package for go-chi
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.
Has anyone implemented an new rekic v3 middleware for go-chi yet?
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)
}
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
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)
...
}
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.
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.