github.com/newrelic/go-agent@v3.26.0+incompatible/examples/client/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 "time" 11 12 "github.com/newrelic/go-agent" 13 ) 14 15 func mustGetEnv(key string) string { 16 if val := os.Getenv(key); "" != val { 17 return val 18 } 19 panic(fmt.Sprintf("environment variable %s unset", key)) 20 } 21 22 func doRequest(txn newrelic.Transaction) error { 23 req, err := http.NewRequest("GET", "http://localhost:8000/segments", nil) 24 if nil != err { 25 return err 26 } 27 client := &http.Client{} 28 seg := newrelic.StartExternalSegment(txn, req) 29 defer seg.End() 30 resp, err := client.Do(req) 31 if nil != err { 32 return err 33 } 34 fmt.Println("response code is", resp.StatusCode) 35 return nil 36 } 37 38 func main() { 39 cfg := newrelic.NewConfig("Client App", mustGetEnv("NEW_RELIC_LICENSE_KEY")) 40 cfg.Logger = newrelic.NewDebugLogger(os.Stdout) 41 app, err := newrelic.NewApplication(cfg) 42 if nil != err { 43 fmt.Println(err) 44 os.Exit(1) 45 } 46 47 // Wait for the application to connect. 48 if err = app.WaitForConnection(5 * time.Second); nil != err { 49 fmt.Println(err) 50 } 51 52 txn := app.StartTransaction("client-txn", nil, nil) 53 err = doRequest(txn) 54 if nil != err { 55 txn.NoticeError(err) 56 } 57 txn.End() 58 59 // Shut down the application to flush data to New Relic. 60 app.Shutdown(10 * time.Second) 61 }