apm

package
v2.39.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package apm provides a programmatic API for interacting with the New Relic APM product.

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

- Reading, updating, and deleting APM applications

- Creating, reading, and deleting APM deployment markers

- Reading APM key transactions

- Creating, reading, and deleting APM labels

Authentication

You will need a valid Personal API key to communicate with the backend New Relic APIs that provide this functionality. See the API key documentation below for more information on how to locate this key:

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

Labels

New Relic One entity tags are currently the preferred method for organizing your New Relic resources. Consider using entity tags via the `entities` package if you are just getting started. More information about entity tags and APM labels can be found at the following URL:

https://docs.newrelic.com/docs/new-relic-one/use-new-relic-one/core-concepts/tagging-use-tags-organize-group-what-you-monitor#labels

Example (Application)
// Initialize the client configuration.  A Personal API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

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

// Search the applications for the current account by name.
listParams := &ListApplicationsParams{
	Name: "Example application",
}

apps, err := client.ListApplications(listParams)
if err != nil {
	log.Fatal("error listing applications:", err)
}

// Get an application by ID.  This example assumes that at least one application
// has been returned by the list endpoint, but in practice it is possible
// that an empty slice is returned.
app, err := client.GetApplication(apps[0].ID)
if err != nil {
	log.Fatal("error getting application:", err)
}

// Update an application's settings.  The following example updates the
// application's Apdex threshold.
updateParams := UpdateApplicationParams{
	Name: app.Name,
	Settings: ApplicationSettings{
		AppApdexThreshold: 0.6,
	},
}

app, err = client.UpdateApplication(app.ID, updateParams)
if err != nil {
	log.Fatal("error updating application settings:", err)
}

// Delete an application that is no longer reporting data.
if !app.Reporting {
	_, err = client.DeleteApplication(app.ID)
	if err != nil {
		log.Fatal("error deleting application:", err)
	}
}
Output:

Example (Application_instances)
// Initialize the client configuration.  A Personal API key
// is required to communicate with the backend API.
cfg := config.New()
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

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

applicationID := 12345
instanceID := 12345678

params := ListApplicationInstancesParams{
	Hostname: "hostname",
	IDs:      []int{instanceID},
}

// List an application's instances.
instances, err := client.ListApplicationInstances(applicationID, &params)
if err != nil {
	log.Fatal("error listing application instances:", err)
}

// Output the application instance count.
fmt.Printf("Instance count: %d", len(instances))

// Get a specific application instance.
instance, err := client.GetApplicationInstance(applicationID, instanceID)
if err != nil {
	log.Fatal("error deleting application:", err)
}

// Output the application instance's host and port.
fmt.Printf("Host: %s, Port: %d\n", instance.Host, instance.Port)
Output:

Example (KeyTransaction)
// Initialize the client configuration. A Personal API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

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

// Search the key transactions for the current account by name.
listParams := &ListKeyTransactionsParams{
	Name: "Example key transaction",
}

transactions, err := client.ListKeyTransactions(listParams)
if err != nil {
	log.Fatal("error listing key transactions:", err)
}

// Get a key transaction by ID.  This example assumes that at least one key
// transaction has been returned by the list endpoint, but in practice it is
// possible that an empty slice is returned.
transaction, err := client.GetKeyTransaction(transactions[0].ID)
if err != nil {
	log.Fatal("error getting key transaction:", err)
}

// Output the key transaction's health status.
fmt.Printf("Key transaction status: %s\n", transaction.HealthStatus)
Output:

Example (Labels)
// Initialize the client configuration. A Personal API key is required to
// communicate with the backend API.
cfg := config.New()
cfg.PersonalAPIKey = os.Getenv("NEW_RELIC_API_KEY")

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

// Get an APM application by ID.
app, err := client.GetApplication(12345678)
if err != nil {
	log.Fatal("error getting application:", err)
}

// List the existing labels for this account.
labels, err := client.ListLabels()
if err != nil {
	log.Fatal("error listing labels:", err)
}

// Output the concatenated label key and associated application IDs for each.
for _, l := range labels {
	fmt.Printf("Label key: %s, Application IDs: %v\n", l.Key, l.Links.Applications)
}

// Add a label to the application that describes its data center's location.
label := Label{
	Category: "Datacenter",
	Name:     "East",
	Links: LabelLinks{
		Applications: []int{app.ID},
	},
}

l, err := client.CreateLabel(label)
if err != nil {
	log.Fatal("error creating label:", err)
}

// Delete a label from all linked applications and servers.
_, err = client.DeleteLabel(l.Key)
if err != nil {
	log.Fatal("error deleting label:", err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APM

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

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

func New

func New(config config.Config) APM

New is used to create a new APM client instance.

func (*APM) CreateDeployment

func (a *APM) CreateDeployment(applicationID int, deployment Deployment) (*Deployment, error)

CreateDeployment creates a deployment marker for an application.

func (*APM) CreateDeploymentWithContext

func (a *APM) CreateDeploymentWithContext(ctx context.Context, applicationID int, deployment Deployment) (*Deployment, error)

CreateDeploymentWithContext creates a deployment marker for an application.

func (*APM) CreateLabel

func (a *APM) CreateLabel(label Label) (*Label, error)

CreateLabel creates a new label within an account.

func (*APM) CreateLabelWithContext

func (a *APM) CreateLabelWithContext(ctx context.Context, label Label) (*Label, error)

CreateLabelWithContext creates a new label within an account.

func (*APM) DeleteApplication

func (a *APM) DeleteApplication(applicationID int) (*Application, error)

DeleteApplication is used to delete a New Relic application. This process will only succeed if the application is no longer reporting data.

func (*APM) DeleteApplicationWithContext

func (a *APM) DeleteApplicationWithContext(ctx context.Context, applicationID int) (*Application, error)

DeleteApplicationWithContext is used to delete a New Relic application. This process will only succeed if the application is no longer reporting data.

func (*APM) DeleteDeployment

func (a *APM) DeleteDeployment(applicationID int, deploymentID int) (*Deployment, error)

DeleteDeployment deletes a deployment marker for an application.

func (*APM) DeleteDeploymentWithContext

func (a *APM) DeleteDeploymentWithContext(ctx context.Context, applicationID int, deploymentID int) (*Deployment, error)

DeleteDeploymentWithContext deletes a deployment marker for an application.

func (*APM) DeleteLabel

func (a *APM) DeleteLabel(key string) (*Label, error)

DeleteLabel deletes a label by key. A label's key is a string hash formatted as <Category>:<Name>.

func (*APM) DeleteLabelWithContext

func (a *APM) DeleteLabelWithContext(ctx context.Context, key string) (*Label, error)

DeleteLabelWithContext deletes a label by key. A label's key is a string hash formatted as <Category>:<Name>.

func (*APM) GetApplication

func (a *APM) GetApplication(applicationID int) (*Application, error)

GetApplication is used to retrieve a single New Relic application.

func (*APM) GetApplicationInstance

func (a *APM) GetApplicationInstance(applicationID int, instanceID int) (*ApplicationInstance, error)

GetApplicationInstance is used to retrieve a specific New Relic application instance.

func (*APM) GetApplicationInstanceWithContext

func (a *APM) GetApplicationInstanceWithContext(ctx context.Context, applicationID int, instanceID int) (*ApplicationInstance, error)

GetApplicationInstanceWithContext is used to retrieve a specific New Relic application instance.

func (*APM) GetApplicationWithContext

func (a *APM) GetApplicationWithContext(ctx context.Context, applicationID int) (*Application, error)

GetApplicationWithContext is used to retrieve a single New Relic application.

func (*APM) GetKeyTransaction

func (a *APM) GetKeyTransaction(id int) (*KeyTransaction, error)

GetKeyTransaction returns a specific key transaction by ID.

func (*APM) GetKeyTransactionWithContext

func (a *APM) GetKeyTransactionWithContext(ctx context.Context, id int) (*KeyTransaction, error)

GetKeyTransactionWithContext returns a specific key transaction by ID.

func (*APM) GetLabel

func (a *APM) GetLabel(key string) (*Label, error)

GetLabel gets a label by key. A label's key is a string hash formatted as <Category>:<Name>.

func (*APM) GetLabelWithContext

func (a *APM) GetLabelWithContext(ctx context.Context, key string) (*Label, error)

GetLabelWithContext gets a label by key. A label's key is a string hash formatted as <Category>:<Name>.

func (*APM) GetMetricData

func (a *APM) GetMetricData(applicationID int, params MetricDataParams) ([]*MetricData, error)

GetMetricData is used to retrieve a list of values for each of the requested metrics.

https://rpm.newrelic.com/api/explore/applications/metric_data

func (*APM) GetMetricDataWithContext

func (a *APM) GetMetricDataWithContext(ctx context.Context, applicationID int, params MetricDataParams) ([]*MetricData, error)

GetMetricDataWithContext is used to retrieve a list of values for each of the requested metrics.

https://rpm.newrelic.com/api/explore/applications/metric_data

func (*APM) GetMetricNames

func (a *APM) GetMetricNames(applicationID int, params MetricNamesParams) ([]*MetricName, error)

GetMetricNames is used to retrieve a list of known metrics and their value names for the given resource.

https://rpm.newrelic.com/api/explore/applications/metric_names

func (*APM) GetMetricNamesWithContext

func (a *APM) GetMetricNamesWithContext(ctx context.Context, applicationID int, params MetricNamesParams) ([]*MetricName, error)

GetMetricNamesWithContext is used to retrieve a list of known metrics and their value names for the given resource.

https://rpm.newrelic.com/api/explore/applications/metric_names

func (*APM) ListApplicationInstances

func (a *APM) ListApplicationInstances(applicationID int, params *ListApplicationInstancesParams) ([]*ApplicationInstance, error)

ListApplicationInstances is used to retrieve New Relic application instances.

func (*APM) ListApplicationInstancesWithContext

func (a *APM) ListApplicationInstancesWithContext(ctx context.Context, applicationID int, params *ListApplicationInstancesParams) ([]*ApplicationInstance, error)

ListApplicationInstancesWithContext is used to retrieve New Relic application instances.

func (*APM) ListApplications

func (a *APM) ListApplications(params *ListApplicationsParams) ([]*Application, error)

ListApplications is used to retrieve New Relic applications.

func (*APM) ListApplicationsWithContext

func (a *APM) ListApplicationsWithContext(ctx context.Context, params *ListApplicationsParams) ([]*Application, error)

ListApplicationsWithContext is used to retrieve New Relic applications.

func (*APM) ListDeployments

func (a *APM) ListDeployments(applicationID int) ([]*Deployment, error)

ListDeployments returns deployments for an application.

func (*APM) ListDeploymentsWithContext

func (a *APM) ListDeploymentsWithContext(ctx context.Context, applicationID int) ([]*Deployment, error)

ListDeploymentsWithContext returns deployments for an application.

func (*APM) ListKeyTransactions

func (a *APM) ListKeyTransactions(params *ListKeyTransactionsParams) ([]*KeyTransaction, error)

ListKeyTransactions returns all key transactions for an account.

func (*APM) ListKeyTransactionsWithContext

func (a *APM) ListKeyTransactionsWithContext(ctx context.Context, params *ListKeyTransactionsParams) ([]*KeyTransaction, error)

ListKeyTransactionsWithContext returns all key transactions for an account.

func (*APM) ListLabels

func (a *APM) ListLabels() ([]*Label, error)

ListLabels returns the labels within an account.

func (*APM) ListLabelsWithContext

func (a *APM) ListLabelsWithContext(ctx context.Context) ([]*Label, error)

ListLabelsWithContext returns the labels within an account.

func (*APM) UpdateApplication

func (a *APM) UpdateApplication(applicationID int, params UpdateApplicationParams) (*Application, error)

UpdateApplication is used to update a New Relic application's name and/or settings.

func (*APM) UpdateApplicationWithContext

func (a *APM) UpdateApplicationWithContext(ctx context.Context, applicationID int, params UpdateApplicationParams) (*Application, error)

UpdateApplicationWithContext is used to update a New Relic application's name and/or settings.

type Application

type Application struct {
	ID             int                       `json:"id,omitempty"`
	Name           string                    `json:"name,omitempty"`
	Language       string                    `json:"language,omitempty"`
	HealthStatus   string                    `json:"health_status,omitempty"`
	Reporting      bool                      `json:"reporting"`
	LastReportedAt string                    `json:"last_reported_at,omitempty"`
	Summary        ApplicationSummary        `json:"application_summary,omitempty"`
	EndUserSummary ApplicationEndUserSummary `json:"end_user_summary,omitempty"`
	Settings       ApplicationSettings       `json:"settings,omitempty"`
	Links          ApplicationLinks          `json:"links,omitempty"`
}

Application represents information about a New Relic application.

type ApplicationEndUserSummary

type ApplicationEndUserSummary struct {
	ResponseTime float64 `json:"response_time"`
	Throughput   float64 `json:"throughput"`
	ApdexTarget  float64 `json:"apdex_target"`
	ApdexScore   float64 `json:"apdex_score"`
}

ApplicationEndUserSummary represents performance information about a New Relic application.

type ApplicationInstance

type ApplicationInstance struct {
	ID              int                       `json:"id,omitempty"`
	ApplicationName string                    `json:"application_name,omitempty"`
	Host            string                    `json:"host,omitempty"`
	Port            int                       `json:"port,omitempty"`
	Language        string                    `json:"language,omitempty"`
	HealthStatus    string                    `json:"health_status,omitempty"`
	Summary         ApplicationSummary        `json:"application_summary,omitempty"`
	EndUserSummary  ApplicationEndUserSummary `json:"end_user_summary,omitempty"`
	Links           ApplicationInstanceLinks  `json:"links,omitempty"`
}

ApplicationInstance represents information about a New Relic application instance.

type ApplicationInstanceLinks struct {
	Application     int `json:"application,omitempty"`
	ApplicationHost int `json:"application_host,omitempty"`
}

ApplicationInstanceLinks represents all the links for a New Relic application instance.

type ApplicationLinks struct {
	ServerIDs     []int `json:"servers,omitempty"`
	HostIDs       []int `json:"application_hosts,omitempty"`
	InstanceIDs   []int `json:"application_instances,omitempty"`
	AlertPolicyID int   `json:"alert_policy"`
}

ApplicationLinks represents all the links for a New Relic application.

type ApplicationSettings

type ApplicationSettings struct {
	AppApdexThreshold        float64 `json:"app_apdex_threshold,omitempty"`
	EndUserApdexThreshold    float64 `json:"end_user_apdex_threshold,omitempty"`
	EnableRealUserMonitoring bool    `json:"enable_real_user_monitoring"`
	UseServerSideConfig      bool    `json:"use_server_side_config"`
}

ApplicationSettings represents some of the settings of a New Relic application.

type ApplicationSummary

type ApplicationSummary struct {
	ResponseTime            float64 `json:"response_time"`
	Throughput              float64 `json:"throughput"`
	ErrorRate               float64 `json:"error_rate"`
	ApdexTarget             float64 `json:"apdex_target"`
	ApdexScore              float64 `json:"apdex_score"`
	HostCount               int     `json:"host_count"`
	InstanceCount           int     `json:"instance_count"`
	ConcurrentInstanceCount int     `json:"concurrent_instance_count"`
}

ApplicationSummary represents performance information about a New Relic application.

type ApplicationsInterface

type ApplicationsInterface interface {
	// contains filtered or unexported methods
}

ApplicationsInterface interface should be refactored to be a global interface for fetching NR type things

type Deployment

type Deployment struct {
	Links       *DeploymentLinks `json:"links,omitempty"`
	ID          int              `json:"id,omitempty"`
	Revision    string           `json:"revision"`
	Changelog   string           `json:"changelog,omitempty"`
	Description string           `json:"description,omitempty"`
	User        string           `json:"user,omitempty"`
	Timestamp   string           `json:"timestamp,omitempty"`
}

Deployment represents information about a New Relic application deployment.

type DeploymentLinks struct {
	ApplicationID int `json:"application,omitempty"`
}

DeploymentLinks contain the application ID for the deployment.

type KeyTransaction

type KeyTransaction struct {
	ID              int                       `json:"id,omitempty"`
	Name            string                    `json:"name,omitempty"`
	TransactionName string                    `json:"transaction_name,omitempty"`
	HealthStatus    string                    `json:"health_status,omitempty"`
	LastReportedAt  string                    `json:"last_reported_at,omitempty"`
	Reporting       bool                      `json:"reporting"`
	Summary         ApplicationSummary        `json:"application_summary,omitempty"`
	EndUserSummary  ApplicationEndUserSummary `json:"end_user_summary,omitempty"`
	Links           KeyTransactionLinks       `json:"links,omitempty"`
}

KeyTransaction represents information about a New Relic key transaction.

type KeyTransactionLinks struct {
	Application int `json:"application,omitempty"`
}

KeyTransactionLinks represents associations for a key transaction.

type Label

type Label struct {
	Key      string     `json:"key,omitempty"`
	Category string     `json:"category,omitempty"`
	Name     string     `json:"name,omitempty"`
	Links    LabelLinks `json:"links,omitempty"`
}

Label represents a New Relic label.

type LabelLinks struct {
	Applications []int `json:"applications"`
	Servers      []int `json:"servers"`
}

LabelLinks represents external references on the Label.

type ListApplicationInstancesParams

type ListApplicationInstancesParams struct {
	Hostname string `url:"filter[hostname],omitempty"`
	IDs      []int  `url:"filter[ids],omitempty,comma"`
}

ListApplicationInstancesParams represents a set of filters to be used when querying New Relic application instances.

type ListApplicationsParams

type ListApplicationsParams struct {
	Name     string `url:"filter[name],omitempty"`
	Host     string `url:"filter[host],omitempty"`
	IDs      []int  `url:"filter[ids],omitempty,comma"`
	Language string `url:"filter[language],omitempty"`
}

ListApplicationsParams represents a set of filters to be used when querying New Relic applications.

type ListKeyTransactionsParams

type ListKeyTransactionsParams struct {
	Name string `url:"filter[name],omitempty"`
	IDs  []int  `url:"filter[ids],omitempty,comma"`
}

ListKeyTransactionsParams represents a set of filters to be used when querying New Relic key transactions.

type MetricData

type MetricData struct {
	Name       string            `json:"name,omitempty"`
	Timeslices []MetricTimeslice `json:"timeslices,omitempty"`
}

MetricData is the series of time windows and the data therein, for a given metric name.

type MetricDataParams

type MetricDataParams struct {
	Names     []string   `url:"names[],omitempty"`
	Values    []string   `url:"values[],omitempty"`
	From      *time.Time `url:"from,omitempty"`
	To        *time.Time `url:"to,omitempty"`
	Period    int        `url:"period,omitempty"`
	Summarize bool       `url:"summarize,omitempty"`
	Raw       bool       `url:"raw,omitempty"`
}

MetricDataParams are the request parameters for the /metrics/data.json endpoint.

type MetricName

type MetricName struct {
	Name   string   `json:"name,omitempty"`
	Values []string `json:"values,omitempty"`
}

MetricName is the name of a metric, and the names of the values that can be retrieved.

type MetricNamesParams

type MetricNamesParams struct {
	Name string `url:"name,omitempty"`
}

MetricNamesParams are the request parameters for the /metrics.json endpoint.

type MetricTimeslice

type MetricTimeslice struct {
	From   *time.Time            `json:"from"`
	To     *time.Time            `json:"to"`
	Values MetricTimesliceValues `json:"values"`
}

MetricTimeslice is a single window of time for a given metric, with the associated metric data.

type MetricTimesliceValues

type MetricTimesliceValues struct {
	AsPercentage           float64 `json:"as_percentage,omitempty"`
	AverageTime            float64 `json:"average_time,omitempty"`
	CallsPerMinute         float64 `json:"calls_per_minute,omitempty"`
	MaxValue               float64 `json:"max_value,omitempty"`
	TotalCallTimePerMinute float64 `json:"total_call_time_per_minute,omitempty"`
	Utilization            float64 `json:"utilization,omitempty"`

	Values map[string]float64 `json:"-"`
}

MetricTimesliceValues is the collection of metric values for a single time slice. Note that according to the API documentation, these values are from a `hashmap`. The static values have been left in the struct to maintain backwards compatibility. Users of this type should prefer the `Values` map over struct fields.

func (*MetricTimesliceValues) UnmarshalJSON

func (m *MetricTimesliceValues) UnmarshalJSON(b []byte) error

UnmarshalJSON is a custom unmarshaling function that unmarshals the JSON into a `MetricTimesliceValues` and into the `Values` field.

type UpdateApplicationParams

type UpdateApplicationParams struct {
	Name     string
	Settings ApplicationSettings
}

UpdateApplicationParams represents a set of parameters to be used when updating New Relic applications.

Jump to

Keyboard shortcuts

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