go-agent icon indicating copy to clipboard operation
go-agent copied to clipboard

Integration package for go-chi

Open dominic-oconnor opened this issue 3 years ago • 6 comments

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.

dominic-oconnor avatar Feb 09 '22 18:02 dominic-oconnor

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

scohen-examity avatar Oct 10 '22 20:10 scohen-examity

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)
}

Meroje avatar Nov 29 '22 15:11 Meroje

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

danesparza avatar Mar 15 '23 20:03 danesparza

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)
        ...
}

RiskyFeryansyahP avatar Apr 06 '23 20:04 RiskyFeryansyahP

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.

vithubati avatar Apr 22 '24 22:04 vithubati

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.

mirackara avatar May 30 '24 17:05 mirackara