synthetics

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2020 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Overview

Package synthetics provides a programmatic API for interacting with the New Relic Synthetics API. It can be used for a variety of operations, including:

- Creating, reading, updating, and deleting Synthetics monitors

- Reading and updating Synthetics monitor scripts

- Associating Synthetics monitor scripts with existing Synthetics monitors

- Creating, reading, and deleting Synthetics monitor labels

- Creating, reading, updating, and deleting Synthetics secure credentials

Authentication

You will need a valid Admin's API key to communicate with the backend New Relic API that provides this functionality. See the API key documentation below for more information on how to locate these keys:

https://docs.newrelic.com/docs/apis/get-started/intro-apis/types-new-relic-api-keys

Package synthetics provides a programmatic API for interacting with the New Relic Synthetics product.

Example (Labels)
// Initialize the client configuration.  An Admin API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.AdminAPIKey = os.Getenv("NEW_RELIC_ADMIN_API_KEY")

// Initialize the client.
client := New(cfg)

monitorID := "fe3c7f8e-9200-4f4b-8643-13e156f22a2a"

// Get the labels for a given Synthetics monitor.
labels, err := client.GetMonitorLabels(monitorID)
if err != nil {
	log.Fatal("error updating Synthetics monitor: ", err)
}

// Output the label keys and values.
for _, l := range labels {
	fmt.Printf("monitor ID: %s, key: %s, value: %s", monitorID, l.Type, l.Value)
}

// Add a label to a given Synthetics monitor.
err = client.AddMonitorLabel(monitorID, "key", "value")
if err != nil {
	log.Fatal("error adding Synthetics label: ", err)
}

// Delete a label from a Synthetics monitor.
err = client.DeleteMonitorLabel(monitorID, "key", "value")
if err != nil {
	log.Fatal("error deleting Synthetics label: ", err)
}
Output:

Example (Monitor)
// Initialize the client configuration.  An Admin API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.AdminAPIKey = os.Getenv("NEW_RELIC_ADMIN_API_KEY")

// Initialize the client.
client := New(cfg)

// Create a simple Synthetics monitor.
simpleMonitor := Monitor{
	Name:         "Example monitor",
	Type:         MonitorTypes.Ping,
	Status:       MonitorStatus.Enabled,
	SLAThreshold: 2.0,
	URI:          "https://www.example.com",
	Frequency:    5,
	Locations:    []string{"AWS_US_EAST_1"},
}

created, err := client.CreateMonitor(simpleMonitor)
if err != nil {
	log.Fatal("error creating Synthetics monitor: ", err)
}

// Create a scripted browser monitor.
scriptedBrowser := Monitor{
	Name:         "Example scriptied browser monitor",
	Type:         MonitorTypes.ScriptedBrowser,
	Status:       MonitorStatus.Enabled,
	SLAThreshold: 2.0,
	URI:          "https://www.example.com",
	Frequency:    1440,
	Locations:    []string{"AWS_US_EAST_1", "AWS_US_WEST_1"},
}

monitorScript := MonitorScript{
	Text: `
var assert = require("assert");
$browser.get("http://www.example.com").then(function(){ 

	// Check the H1 title matches "Example Domain" 
	return $browser.findElement($driver.By.css("h1")).then(function(element){ 
		return element.getText().then(function(text){ 
		assert.equal("Example Domain", text, "Page H1 title did not match"); 
		}); 
	}); 
})`,
}

created, err = client.CreateMonitor(scriptedBrowser)
if err != nil {
	log.Fatal("error creating Synthetics monitor: ", err)
}

_, err = client.UpdateMonitorScript(created.ID, monitorScript)
if err != nil {
	log.Fatal("error updating Synthetics monitor script: ", err)
}

// Update an existing Synthetics monitor script.
created.Locations = []string{"AWS_US_WEST_1"}

updated, err := client.UpdateMonitor(*created)
if err != nil {
	log.Fatal("error updating Synthetics monitor: ", err)
}

// Delete an existing Synthetics monitor script.
err = client.DeleteMonitor(updated.ID)
if err != nil {
	log.Fatal("error deleting Synthetics monitor: ", err)
}
Output:

Example (SecureCredentials)
// Initialize the client configuration.  An Admin API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.AdminAPIKey = os.Getenv("NEW_RELIC_ADMIN_API_KEY")

// Initialize the client.
client := New(cfg)

// Get the Synthetics secure credentials for this account.
credentials, err := client.GetSecureCredentials()
if err != nil {
	log.Fatal("error getting Synthetics secure credentials: ", err)
}

// Get a single Synthetics secure credential belonging to this account.
credential, err := client.GetSecureCredential(credentials[0].Key)
if err != nil {
	log.Fatal("error getting Synthetics secure credential: ", err)
}

// Add a secure credential for use with Synthetics.
credential, err = client.AddSecureCredential("key", "value", "description")
if err != nil {
	log.Fatal("error adding Synthetics secure credential: ", err)
}

// Delete a Synthetics secure credential.
err = client.DeleteSecureCredential(credential.Key)
if err != nil {
	log.Fatal("error deleting Synthetics secure credential: ", err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// MonitorTypes specifies the possible types for a Synthetics monitor.
	MonitorTypes = struct {
		Ping            MonitorType
		Browser         MonitorType
		ScriptedBrowser MonitorType
		APITest         MonitorType
	}{
		Ping:            "SIMPLE",
		Browser:         "BROWSER",
		ScriptedBrowser: "SCRIPT_BROWSER",
		APITest:         "SCRIPT_API",
	}

	// MonitorStatus specifies the possible Synthetics monitor status types.
	MonitorStatus = struct {
		Enabled  MonitorStatusType
		Muted    MonitorStatusType
		Disabled MonitorStatusType
	}{
		Enabled:  "ENABLED",
		Muted:    "MUTED",
		Disabled: "DISABLED",
	}
)

Functions

This section is empty.

Types

type ErrorDetail

type ErrorDetail struct {
	Message string `json:"error,omitempty"`
}

ErrorDetail represents an single error from New Relic Synthetics.

type ErrorResponse

type ErrorResponse struct {
	Message            string        `json:"error,omitempty"`
	Messages           []ErrorDetail `json:"errors,omitempty"`
	ServerErrorMessage string        `json:"message,omitempty"`
}

ErrorResponse represents an error response from New Relic Synthetics.

func (*ErrorResponse) Error

func (e *ErrorResponse) Error() string

Error surfaces an error message from the New Relic Synthetics error response.

func (*ErrorResponse) New added in v0.13.0

func (e *ErrorResponse) New() http.ErrorResponse

New creates a new instance of ErrorResponse.

type Monitor

type Monitor struct {
	ID           string            `json:"id,omitempty"`
	Name         string            `json:"name"`
	Type         MonitorType       `json:"type"`
	Frequency    uint              `json:"frequency"`
	URI          string            `json:"uri"`
	Locations    []string          `json:"locations"`
	Status       MonitorStatusType `json:"status"`
	SLAThreshold float64           `json:"slaThreshold"`
	UserID       uint              `json:"userId,omitempty"`
	APIVersion   string            `json:"apiVersion,omitempty"`
	ModifiedAt   *Time             `json:"modifiedAt,omitempty"`
	CreatedAt    *Time             `json:"createdAt,omitempty"`
	Options      MonitorOptions    `json:"options,omitempty"`
}

Monitor represents a New Relic Synthetics monitor.

type MonitorLabel added in v0.10.0

type MonitorLabel struct {
	Type  string `json:"type"`
	Value string `json:"value"`
	Href  string `json:"href"`
}

MonitorLabel represents a single label for a New Relic Synthetics monitor.

type MonitorOptions

type MonitorOptions struct {
	ValidationString       string `json:"validationString,omitempty"`
	VerifySSL              bool   `json:"verifySSL,omitempty"`
	BypassHEADRequest      bool   `json:"bypassHEADRequest,omitempty"`
	TreatRedirectAsFailure bool   `json:"treatRedirectAsFailure,omitempty"`
}

MonitorOptions represents the options for a New Relic Synthetics monitor.

type MonitorScript

type MonitorScript struct {
	Text      string                  `json:"scriptText"`
	Locations []MonitorScriptLocation `json:"scriptLocations"`
}

MonitorScript represents a New Relic Synthetics monitor script.

type MonitorScriptLocation

type MonitorScriptLocation struct {
	Name string `json:"name"`
	HMAC string `json:"hmac"`
}

MonitorScriptLocation represents a New Relic Synthetics monitor script location.

type MonitorStatusType

type MonitorStatusType string

MonitorStatusType represents a Synthetics monitor status type.

type MonitorType

type MonitorType string

MonitorType represents a Synthetics monitor type.

type SecureCredential added in v0.10.0

type SecureCredential struct {
	Key         string `json:"key"`
	Description string `json:"description"`
	Value       string `json:"value"`
	CreatedAt   *Time  `json:"createdAt"`
	LastUpdated *Time  `json:"lastUpdated"`
}

SecureCredential represents a Synthetics secure credential.

type Synthetics

type Synthetics struct {
	// contains filtered or unexported fields
}

Synthetics is used to communicate with the New Relic Synthetics product.

func New

func New(config config.Config) Synthetics

New is used to create a new Synthetics client instance.

func (*Synthetics) AddMonitorLabel added in v0.10.0

func (s *Synthetics) AddMonitorLabel(monitorID, labelKey, labelValue string) error

AddMonitorLabel is used to add a label to a given monitor.

func (*Synthetics) AddSecureCredential added in v0.10.0

func (s *Synthetics) AddSecureCredential(key, value, description string) (*SecureCredential, error)

AddSecureCredential is used to add a secure credential to your New Relic account.

func (*Synthetics) CreateMonitor

func (s *Synthetics) CreateMonitor(monitor Monitor) (*Monitor, error)

CreateMonitor is used to create a New Relic Synthetics monitor.

func (*Synthetics) DeleteMonitor

func (s *Synthetics) DeleteMonitor(monitorID string) error

DeleteMonitor is used to delete a New Relic Synthetics monitor.

func (*Synthetics) DeleteMonitorLabel added in v0.10.0

func (s *Synthetics) DeleteMonitorLabel(monitorID, labelKey, labelValue string) error

DeleteMonitorLabel deletes a key:value label from the given Syntheics monitor.

func (*Synthetics) DeleteSecureCredential added in v0.10.0

func (s *Synthetics) DeleteSecureCredential(key string) error

DeleteSecureCredential deletes a secure credential from your New Relic account.

func (*Synthetics) GetMonitor

func (s *Synthetics) GetMonitor(monitorID string) (*Monitor, error)

GetMonitor is used to retrieve a specific New Relic Synthetics monitor.

func (*Synthetics) GetMonitorLabels added in v0.10.0

func (s *Synthetics) GetMonitorLabels(monitorID string) ([]*MonitorLabel, error)

GetMonitorLabels is used to retrieve all labels for a given Synthetics monitor.

func (*Synthetics) GetMonitorScript

func (s *Synthetics) GetMonitorScript(monitorID string) (*MonitorScript, error)

GetMonitorScript is used to retrieve the script that belongs to a New Relic Synthetics scripted monitor.

func (*Synthetics) GetSecureCredential added in v0.10.0

func (s *Synthetics) GetSecureCredential(key string) (*SecureCredential, error)

GetSecureCredential is used to retrieve a specific secure credential from your New Relic account.

func (*Synthetics) GetSecureCredentials added in v0.10.0

func (s *Synthetics) GetSecureCredentials() ([]*SecureCredential, error)

GetSecureCredentials is used to retrieve all secure credentials from your New Relic account.

func (*Synthetics) ListMonitors

func (s *Synthetics) ListMonitors() ([]*Monitor, error)

ListMonitors is used to retrieve New Relic Synthetics monitors.

func (*Synthetics) UpdateMonitor

func (s *Synthetics) UpdateMonitor(monitor Monitor) (*Monitor, error)

UpdateMonitor is used to update a New Relic Synthetics monitor.

func (*Synthetics) UpdateMonitorScript

func (s *Synthetics) UpdateMonitorScript(monitorID string, script MonitorScript) (*MonitorScript, error)

UpdateMonitorScript is used to add a script to an existing New Relic Synthetics monitor_script.

func (*Synthetics) UpdateSecureCredential added in v0.10.0

func (s *Synthetics) UpdateSecureCredential(key, value, description string) (*SecureCredential, error)

UpdateSecureCredential is used to update a secure credential in your New Relic account.

type Time

type Time time.Time

Time is a type used for unmarshaling timestamps generated by the Synthetics API. Its underlying type is time.Time.

func (Time) Equal

func (t Time) Equal(u Time) bool

Equal provides a comparator for the Time type.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON is responsible for marshaling the Time type.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(s []byte) (err error)

UnmarshalJSON is responsible for unmarshaling the Time type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL