github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/example_monitor_test.go (about)

     1  package synthetics
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	"github.com/newrelic/newrelic-client-go/pkg/config"
     8  )
     9  
    10  func Example_monitor() {
    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  	// Create a simple Synthetics monitor.
    20  	simpleMonitor := Monitor{
    21  		Name:         "Example monitor",
    22  		Type:         MonitorTypes.Ping,
    23  		Status:       MonitorStatus.Enabled,
    24  		SLAThreshold: 2.0,
    25  		URI:          "https://www.example.com",
    26  		Frequency:    5,
    27  		Locations:    []string{"AWS_US_EAST_1"},
    28  	}
    29  
    30  	created, err := client.CreateMonitor(simpleMonitor)
    31  	if err != nil {
    32  		log.Fatal("error creating Synthetics monitor: ", err)
    33  	}
    34  
    35  	// Create a scripted browser monitor.
    36  	scriptedBrowser := Monitor{
    37  		Name:         "Example scriptied browser monitor",
    38  		Type:         MonitorTypes.ScriptedBrowser,
    39  		Status:       MonitorStatus.Enabled,
    40  		SLAThreshold: 2.0,
    41  		URI:          "https://www.example.com",
    42  		Frequency:    1440,
    43  		Locations:    []string{"AWS_US_EAST_1", "AWS_US_WEST_1"},
    44  	}
    45  
    46  	monitorScript := MonitorScript{
    47  		Text: `
    48  var assert = require("assert");
    49  $browser.get("http://www.example.com").then(function(){ 
    50  
    51  	// Check the H1 title matches "Example Domain" 
    52  	return $browser.findElement($driver.By.css("h1")).then(function(element){ 
    53  		return element.getText().then(function(text){ 
    54  		assert.equal("Example Domain", text, "Page H1 title did not match"); 
    55  		}); 
    56  	}); 
    57  })`,
    58  	}
    59  
    60  	created, err = client.CreateMonitor(scriptedBrowser)
    61  	if err != nil {
    62  		log.Fatal("error creating Synthetics monitor: ", err)
    63  	}
    64  
    65  	_, err = client.UpdateMonitorScript(created.ID, monitorScript)
    66  	if err != nil {
    67  		log.Fatal("error updating Synthetics monitor script: ", err)
    68  	}
    69  
    70  	// Update an existing Synthetics monitor script.
    71  	created.Locations = []string{"AWS_US_WEST_1"}
    72  
    73  	updated, err := client.UpdateMonitor(*created)
    74  	if err != nil {
    75  		log.Fatal("error updating Synthetics monitor: ", err)
    76  	}
    77  
    78  	// Delete an existing Synthetics monitor script.
    79  	err = client.DeleteMonitor(updated.ID)
    80  	if err != nil {
    81  		log.Fatal("error deleting Synthetics monitor: ", err)
    82  	}
    83  
    84  	// Get all valid Synthetics monitor locations.
    85  	locations, err := client.GetMonitorLocations()
    86  	if err != nil {
    87  		log.Fatal("error retrieving valid Synthetics monitor locations: ", err)
    88  	}
    89  
    90  	log.Printf("found %d valid Synthetics monitor locations", len(locations))
    91  }