github.com/newrelic/newrelic-client-go@v1.1.0/pkg/apm/example_application_test.go (about)

     1  package apm
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	"github.com/newrelic/newrelic-client-go/pkg/config"
     8  )
     9  
    10  func Example_application() {
    11  	// Initialize the client configuration.  A Personal API key is required to
    12  	// communicate with the backend API.
    13  	cfg := config.New()
    14  	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
    15  
    16  	// Initialize the client.
    17  	client := New(cfg)
    18  
    19  	// Search the applications for the current account by name.
    20  	listParams := &ListApplicationsParams{
    21  		Name: "Example application",
    22  	}
    23  
    24  	apps, err := client.ListApplications(listParams)
    25  	if err != nil {
    26  		log.Fatal("error listing applications:", err)
    27  	}
    28  
    29  	// Get an application by ID.  This example assumes that at least one application
    30  	// has been returned by the list endpoint, but in practice it is possible
    31  	// that an empty slice is returned.
    32  	app, err := client.GetApplication(apps[0].ID)
    33  	if err != nil {
    34  		log.Fatal("error getting application:", err)
    35  	}
    36  
    37  	// Update an application's settings.  The following example updates the
    38  	// application's Apdex threshold.
    39  	updateParams := UpdateApplicationParams{
    40  		Name: app.Name,
    41  		Settings: ApplicationSettings{
    42  			AppApdexThreshold: 0.6,
    43  		},
    44  	}
    45  
    46  	app, err = client.UpdateApplication(app.ID, updateParams)
    47  	if err != nil {
    48  		log.Fatal("error updating application settings:", err)
    49  	}
    50  
    51  	// Delete an application that is no longer reporting data.
    52  	if !app.Reporting {
    53  		_, err = client.DeleteApplication(app.ID)
    54  		if err != nil {
    55  			log.Fatal("error deleting application:", err)
    56  		}
    57  	}
    58  }