Skip to content
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

Integration package for go-chi #450

Open
dominic-oconnor opened this issue Feb 9, 2022 · 7 comments
Open

Integration package for go-chi #450

dominic-oconnor opened this issue Feb 9, 2022 · 7 comments

Comments

@dominic-oconnor
Copy link

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
Copy link

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

@Meroje
Copy link
Contributor

Meroje commented Nov 29, 2022

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
Copy link

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
Copy link

RiskyFeryansyahP commented Apr 6, 2023

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
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

Hello all,

I'm happy to report that Go-Chi support is in active development!

#999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants