github.com/lulzWill/go-agent@v2.1.2+incompatible/examples/short-lived-process/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "time" 7 8 "github.com/lulzWill/go-agent" 9 ) 10 11 func mustGetEnv(key string) string { 12 if val := os.Getenv(key); "" != val { 13 return val 14 } 15 panic(fmt.Sprintf("environment variable %s unset", key)) 16 } 17 18 func main() { 19 cfg := newrelic.NewConfig("Short Lived App", mustGetEnv("NEW_RELIC_LICENSE_KEY")) 20 cfg.Logger = newrelic.NewDebugLogger(os.Stdout) 21 app, err := newrelic.NewApplication(cfg) 22 if nil != err { 23 fmt.Println(err) 24 os.Exit(1) 25 } 26 27 // Wait for the application to connect. 28 if err := app.WaitForConnection(5 * time.Second); nil != err { 29 fmt.Println(err) 30 } 31 32 // Do the tasks at hand. Perhaps record them using transactions and/or 33 // custom events. 34 tasks := []string{"white", "black", "red", "blue", "green", "yellow"} 35 for _, task := range tasks { 36 txn := app.StartTransaction("task", nil, nil) 37 time.Sleep(10 * time.Millisecond) 38 txn.End() 39 app.RecordCustomEvent("task", map[string]interface{}{ 40 "color": task, 41 }) 42 } 43 44 // Shut down the application to flush data to New Relic. 45 app.Shutdown(10 * time.Second) 46 }