github.com/lulzWill/go-agent@v2.1.2+incompatible/examples/_gorilla/main.go (about)

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