Created
March 29, 2019 02:25
-
-
Save axw/04d8035f039d613650d23d369131ee0d to your computer and use it in GitHub Desktop.
Basic Elastic APM middleware for Goji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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