github.com/newrelic/newrelic-client-go@v1.1.0/pkg/events/example_basic_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package events 5 6 import ( 7 "log" 8 "os" 9 "strconv" 10 11 "github.com/newrelic/newrelic-client-go/pkg/config" 12 ) 13 14 func Example_basic() { 15 // Initialize the client configuration. An Insights insert key is required 16 // to communicate with the backend API. 17 cfg := config.New() 18 cfg.InsightsInsertKey = os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY") 19 20 accountID, err := strconv.Atoi(os.Getenv("NEW_RELIC_ACCOUNT_ID")) 21 if err != nil { 22 log.Fatal("environment variable NEW_RELIC_ACCOUNT_ID required") 23 } 24 25 // Initialize the client. 26 client := New(cfg) 27 28 event := struct { 29 EventType string `json:"eventType"` 30 Amount float64 `json:"amount"` 31 }{ 32 EventType: "Purchase", 33 Amount: 123.45, 34 } 35 36 // Post a custom event. 37 if err := client.CreateEvent(accountID, event); err != nil { 38 log.Fatal("error posting custom event:", err) 39 } 40 }