github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrhttprouter/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/julienschmidt/httprouter" 12 newrelic "github.com/newrelic/go-agent" 13 "github.com/newrelic/go-agent/_integrations/nrhttprouter" 14 ) 15 16 func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 17 w.Write([]byte("welcome\n")) 18 } 19 20 func hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { 21 w.Write([]byte(fmt.Sprintf("hello %s\n", ps.ByName("name")))) 22 } 23 24 func mustGetEnv(key string) string { 25 if val := os.Getenv(key); "" != val { 26 return val 27 } 28 panic(fmt.Sprintf("environment variable %s unset", key)) 29 } 30 31 func main() { 32 cfg := newrelic.NewConfig("httprouter App", mustGetEnv("NEW_RELIC_LICENSE_KEY")) 33 cfg.Logger = newrelic.NewDebugLogger(os.Stdout) 34 app, err := newrelic.NewApplication(cfg) 35 if nil != err { 36 fmt.Println(err) 37 os.Exit(1) 38 } 39 40 // Use an *nrhttprouter.Router in place of an *httprouter.Router. 41 router := nrhttprouter.New(app) 42 43 router.GET("/", index) 44 router.GET("/hello/:name", hello) 45 46 http.ListenAndServe(":8000", router) 47 }