With a quickstart, you let your users quickly install dashboards, alerts, and other resources. Here, you learn how to use New Relic's GraphQL API, NerdGraph, to query your existing alert conditions and configure them in your quickstart.
Important
This guide assumes you already have alerts in your New Relic account and a quickstart that you want to add those alerts to. If you don't already have a quickstart, follow our lab to learn how to build one.
Set up your quickstart's alerts directory
This guide assumes you have a quickstart. However, your quickstart may or may not already have alerts. Either way, to add alerts to your quickstart, you need an alerts directory. If your quickstart already has one, you can skip this section.
The _template directory of New Relic quickstarts contains an alerts template folder. Copy this folder to your quickstart. From here, you use the YAML file that corresponds to the alert condition type you want to add to your quickstart.
Once your quickstart is ready to add alerts, you need to look up your alert condition's ID.
Look up your alert's condition ID
To populate your alert configurations with NerdGraph, you first need to look up its identifier.
Click Policies in left hand navigation.
Choose your policy from the list.
Here, you see the list of conditions. Choose the condition that you want to query.
On the Next page, you see ID, Account, and Policy associated with the condition. Copy the ID.
With this identifier, you can now query your alert conditions and use the response to build out alert resources in your quickstart.
Query alert conditions in NerdGraph
There are three types of alerts you can have in New Relic:
Jump to the appropriate section for the kind of alert you want to add to your quickstart.
Query your static alert condition
Copy the following GraphQL query and paste it in the center pane of the explorer.
{ actor { account(id: REPLACE_ACCOUNT_ID) { alerts { nrqlCondition(id: REPLACE_CONDITION_ID) { ... on AlertsNrqlStaticCondition { id name type nrql { query } valueFunction terms { operator priority threshold thresholdDuration thresholdOccurrences } violationTimeLimitSeconds } } } } }}
Here, you query AlertsNrqlStaticCondition
for your condition's ID, name, query and more. These are the required fields you need to create the same alert in your quickstart.
Important
Make sure you replace your account ID and condition ID in the above query.
Execute the query to get a JSON representation of the specified condition.
Next, use this response to add a static alert to your quickstart.
Tip
Notice the checkboxes in the left-hand pane get checked when you paste the query in the explorer. This query returns the fields required to add alert to the quickstart. If you've set custom fields or want to query more information, feel free to either edit the query in the center pane of the explorer or check the corresponding box in the left-hand pane.
Populate static-alert.yml from your alerts quickstart folder with the data returned from your query. Given the example response from the last step, our file looks like:
---
# Name of the alertname: Static Condition
# Description and detailsdetails: |+ This alert is triggered whenever the host count is < 2.
# Type of alerttype: STATIC
# NRQL querynrql: query: "SELECT uniqueCount(host) FROM Transaction"
# Function used to aggregate the NRQL query value(s) for comparison to the terms.threshold (Default: SINGLE_VALUE)valueFunction: SINGLE_VALUE
# List of Critical and Warning thresholds for the conditionterms:- priority: CRITICAL # Operator used to compare against the threshold. operator: BELOW # Value that triggers a violation threshold: 2 # Time in seconds; 120 - 3600 thresholdDuration: 300 # How many data points must be in violation for the duration thresholdOccurrences: AT_LEAST_ONCE
# Duration after which a violation automatically closes# Time in seconds; 300 - 2592000 (Default: 86400 [1 day])violationTimeLimitSeconds: 259200
Here, you added a static alert to your quickstart. If it's helpful, you can rename this file to whatever you want.
Query your baseline alert condition
Copy the following GraphQL query and paste it in the center pane of the explorer.
{ actor { account(id: REPLACE_ACCOUNT_ID) { alerts { nrqlCondition(id: REPLACE_CONDITION_ID) { ... on AlertsNrqlBaselineCondition { id name nrql { query } baselineDirection terms { priority threshold thresholdDuration thresholdOccurrences } violationTimeLimitSeconds } } } } }}
Here, you query AlertsNrqlBaselineCondition
for your condition's name, query, baselineDirection
, and other fields required to add the condition to your quickstart.
Important
Make sure you replace your account ID and condition ID in the above query.
Execute the query to get the configuration data of your alert.
Next, use this response to add baseline alert to your quickstart.
Tip
Notice the checkboxes in the left-hand pane get checked when you paste the query in the explorer. This query returns the fields required to add alert to the quickstart. If you've set custom fields or want to query more information, feel free to either edit the query in the center pane of the explorer or check the corresponding box in the left-hand pane.
Populate baseline-alert.yml from your alerts quickstart folder with the data returned from your query. Given the example response from the last step, our file looks like:
---
# Name of the alertname: Baseline Condition
# Description and detailsdetails: |+ This alert is triggered whenever the average Transaction duration deviates 2 standard deviations from the normal.
# Type of alerttype: BASELINE
# NRQL querynrql: # Baseline alerts can use an optional FACET query: "SELECT average(duration) FROM Transaction"
# Direction in which baseline is set (Default: LOWER_ONLY)baselineDirection: UPPER_ONLY
# List of Critical and Warning thresholds for the conditionterms:- priority: CRITICAL # Value that triggers a violation threshold: 2 # Time in seconds; 120 - 3600, must be a multiple of 60 for Baseline conditions thresholdDuration: 180 # How many data points must be in violation for the duration thresholdOccurrences: ALL
# Duration after which a violation automatically closes# Time in seconds; 300 - 2592000 (Default: 86400 [1 day])violationTimeLimitSeconds: 259200
Here, you added a baseline alert to your quickstart. If it's helpful, you can rename this file to whatever you want.
Summary
In this guide, you learned how to query your existing alert conditions using NerdGraph and how to use the query's JSON response to populate corresponding alert files in quickstart. Read our documentation to learn more about: