github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrgorilla/v1/example/main.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package main 5 6 import ( 7 "fmt" 8 "net/http" 9 "os" 10 11 "github.com/gorilla/mux" 12 newrelic "github.com/newrelic/go-agent" 13 nrgorilla "github.com/newrelic/go-agent/_integrations/nrgorilla/v1" 14 ) 15 16 func makeHandler(text string) http.Handler { 17 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 18 w.Write([]byte(text)) 19 }) 20 } 21 22 func mustGetEnv(key string) string { 23 if val := os.Getenv(key); "" != val { 24 return val 25 } 26 panic(fmt.Sprintf("environment variable %s unset", key)) 27 } 28 29 func main() { 30 cfg := newrelic.NewConfig("Gorilla App", mustGetEnv("NEW_RELIC_LICENSE_KEY")) 31 cfg.Logger = newrelic.NewDebugLogger(os.Stdout) 32 app, err := newrelic.NewApplication(cfg) 33 if nil != err { 34 fmt.Println(err) 35 os.Exit(1) 36 } 37 38 r := mux.NewRouter() 39 r.Handle("/", makeHandler("index")) 40 r.Handle("/alpha", makeHandler("alpha")) 41 42 users := r.PathPrefix("/users").Subrouter() 43 users.Handle("/add", makeHandler("adding user")) 44 users.Handle("/delete", makeHandler("deleting user")) 45 46 // The route name will be used as the transaction name if one is set. 47 r.Handle("/named", makeHandler("named route")).Name("special-name-route") 48 49 // The NotFoundHandler will be instrumented if it is set. 50 r.NotFoundHandler = makeHandler("not found") 51 52 http.ListenAndServe(":8000", nrgorilla.InstrumentRoutes(r, app)) 53 }