Skip to content

Instantly share code, notes, and snippets.

@axw
Created March 29, 2019 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axw/04d8035f039d613650d23d369131ee0d to your computer and use it in GitHub Desktop.
Save axw/04d8035f039d613650d23d369131ee0d to your computer and use it in GitHub Desktop.
Basic Elastic APM middleware for Goji
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"go.elastic.co/apm/module/apmhttp"
)
func main() {
// The Mux.Router middleware must be installed first in order for `web.GetMatch` to work.
goji.Use(goji.DefaultMux.Router)
// This middleware will extract the matched pattern, and use it for the transaction name.
goji.Use(func(c *web.C, h http.Handler) http.Handler {
routeRequestName := func(req *http.Request) string {
pattern := web.GetMatch(*c).RawPattern()
if pattern == nil {
return req.Method + " unknown route"
}
return fmt.Sprint(pattern)
}
return apmhttp.Wrap(h, apmhttp.WithServerRequestName(routeRequestName))
})
goji.Get("/hello/:name", func(c web.C, w http.ResponseWriter, r *http.Request) {
// ...
})
goji.Serve()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment