github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/example_labels_test.go (about) 1 package apm 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 8 "github.com/newrelic/newrelic-client-go/pkg/config" 9 ) 10 11 func Example_labels() { 12 // Initialize the client configuration. A Personal API key is required to 13 // communicate with the backend API. 14 cfg := config.New() 15 cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY") 16 17 // Initialize the client. 18 client := New(cfg) 19 20 // Get an APM application by ID. 21 app, err := client.GetApplication(12345678) 22 if err != nil { 23 log.Fatal("error getting application:", err) 24 } 25 26 // List the existing labels for this account. 27 labels, err := client.ListLabels() 28 if err != nil { 29 log.Fatal("error listing labels:", err) 30 } 31 32 // Output the concatenated label key and associated application IDs for each. 33 for _, l := range labels { 34 fmt.Printf("Label key: %s, Application IDs: %v\n", l.Key, l.Links.Applications) 35 } 36 37 // Add a label to the application that describes its data center's location. 38 label := Label{ 39 Category: "Datacenter", 40 Name: "East", 41 Links: LabelLinks{ 42 Applications: []int{app.ID}, 43 }, 44 } 45 46 l, err := client.CreateLabel(label) 47 if err != nil { 48 log.Fatal("error creating label:", err) 49 } 50 51 // Delete a label from all linked applications and servers. 52 _, err = client.DeleteLabel(l.Key) 53 if err != nil { 54 log.Fatal("error deleting label:", err) 55 } 56 }