github.com/newrelic/newrelic-client-go@v1.1.0/pkg/eventstometrics/example_basic_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package eventstometrics
     5  
     6  import (
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/newrelic/newrelic-client-go/pkg/config"
    11  )
    12  
    13  func Example_basic() {
    14  	// Initialize the client configuration.  A Personal API key is required to
    15  	// communicate with the backend API.
    16  	cfg := config.New()
    17  	cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")
    18  
    19  	// Initialize the client.
    20  	client := New(cfg)
    21  
    22  	accountID := 12345678
    23  
    24  	// List the events to metrics rules for a given account.
    25  	rules, err := client.ListRules(accountID)
    26  	if err != nil {
    27  		log.Fatal("error listing events to metrics rules: ", err)
    28  	}
    29  
    30  	// Get a specific events to metrics rule by ID.
    31  	rule, err := client.GetRule(accountID, rules[0].ID)
    32  	if err != nil {
    33  		log.Fatal("error getting events to metric rule: ", err)
    34  	}
    35  
    36  	log.Printf("Rule name: %s", rule.Name)
    37  
    38  	// Create a new events to metrics rule.
    39  	createInput := []EventsToMetricsCreateRuleInput{
    40  		{
    41  			AccountID:   accountID,
    42  			Name:        "Example rule",
    43  			Description: "Example description",
    44  			NRQL:        "SELECT uniqueCount(account_id) AS `Transaction.account_id` FROM Transaction FACET appName, name",
    45  		},
    46  	}
    47  
    48  	rules, err = client.CreateRules(createInput)
    49  	if err != nil {
    50  		log.Fatal("error creating events to metrics rules: ", err)
    51  	}
    52  
    53  	// Update an existing events to metrics rule.
    54  	updateInput := []EventsToMetricsUpdateRuleInput{
    55  		{
    56  			AccountID: accountID,
    57  			RuleId:    rules[0].ID,
    58  			Enabled:   false,
    59  		},
    60  	}
    61  
    62  	rules, err = client.UpdateRules(updateInput)
    63  	if err != nil {
    64  		log.Fatal("error updating events to metrics rules: ", err)
    65  	}
    66  
    67  	// Delete an existing events to metrics rule.
    68  	deleteInput := []EventsToMetricsDeleteRuleInput{
    69  		{
    70  			AccountID: accountID,
    71  			RuleId:    rules[0].ID,
    72  		},
    73  	}
    74  	_, err = client.DeleteRules(deleteInput)
    75  	if err != nil {
    76  		log.Fatal("error deleting event to meetrics rules: ", err)
    77  	}
    78  }