• Log in

NerdGraph tutorial: NRQL condition alerts

You can manage alerts conditions using our NerdGraph API.

Tip

See the NerdGraph introduction for help getting started with NerdGraph and the NerdGraph explorer.

This document covers the following:

Steps to create a NRQL condition

Follow these steps:

  1. Decide which condition type you want to create (see NRQL Condition threshold types).

  2. Find your relevant policyID by doing one of the following:

  3. Provide the appropriate mutation for your NRQL condition type and the relevant values.

Tip

The NerdGraph GraphiQL explorer is the best place to find up-to-date documentation about the per-field specifics of the NerdGraph NRQL Conditions API. For example, questions like "What does the valueFunction field accept?" are best answered with the inline NerdGraph documentation.

NRQL static condition

Here's an example of creating a static condition:

mutation {
  alertsNrqlConditionStaticCreate(accountId: YOUR_ACCOUNT_ID, policyId: YOUR_POLICY_ID, condition: {
    name: "Low Host Count - Catastrophic"
    enabled: true
    nrql: {
      query: "SELECT uniqueCount(host) FROM Transaction WHERE appName='my-app-name'"
    }
    signal: {
      aggregationWindow: 60
      aggregationMethod: EVENT_FLOW
      aggregationDelay: 120
    }
    terms: {
      threshold: 2
      thresholdOccurrences: AT_LEAST_ONCE
      thresholdDuration: 600
      operator: BELOW
      priority: CRITICAL
    }
    valueFunction: SINGLE_VALUE
    violationTimeLimitSeconds: 86400
  }) {
    id
    name
  }
}

NRQL baseline condition

Here's an example of creating a baseline condition:

mutation {
  alertsNrqlConditionBaselineCreate(accountId: YOUR_ACCOUNT_ID, policyId: YOUR_POLICY_ID, condition: {
    name: "Baseline Condition"
    enabled: true
    baselineDirection: UPPER_ONLY
    nrql: {
      query: "SELECT average(duration) FROM Transaction"
    }
    signal: {
      aggregationWindow: 60
      aggregationMethod: EVENT_FLOW
      aggregationDelay: 120
    }
    terms: {
      threshold: 13
      thresholdDuration: 180
      thresholdOccurrences: ALL
      operator: ABOVE
      priority: CRITICAL
    }
    violationTimeLimitSeconds: 86400
  }) {
    id
    name
    baselineDirection
  }
}

Update a condition

Complete the following:

  1. Determine the type of your existing condition by requesting the type field in a nrqlConditionsSearch query like this:

    {
      actor {
        account(id: YOUR_ACCOUNT_ID) {
          alerts {
            nrqlConditionsSearch {
              nrqlConditions {
                id
                type
              }
            }
          }
        }
      }
    }

    Tip

    The type returned is what you use for your update mutation. For example, if the type returned is STATIC, use alertsNrqlConditionStaticUpdate. If the type returned is BASELINE, use alertsNrqlConditionBaselineUpdate.

  2. Provide the id of your condition to your relevant condition type mutation. Note that you can only update conditions of the relevant type.

Only provide update mutations for the fields you want to update. Fields you don't provide in the update are not touched.

Update mutations

Only fields that you provide in the update are changed. In the following example, baselineDirection returns unchanged, but name is updated.

mutation {
  alertsNrqlConditionBaselineUpdate(id: YOUR_CONDITION_ID, accountId: YOUR_ACCOUNT_ID, condition: {
    name: "Your updated name"
  }) {
    id
    name
    baselineDirection
  }
}

List and filter NRQL conditions

To list or filter your NRQL conditions, use the nrqlConditionsSearch query in NerdGraph.

Singular NRQL condition queries

You can use the NRQL condition API to query for a singular condition. Run the nrqlCondition query in the alerts namespace.

Similar to type specific fields on the nrqlConditionSearch query, you can also use these inline fragments to request fields that are restricted to a NRQL condition type.

{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        nrqlCondition(id: YOUR_CONDITION_ID) {
          id
          name
          ...on AlertsNrqlStaticCondition {
            valueFunction
          }
        }
      }
    }
  }
}

Update the description

This will walk you through the procedure to create a description for a NRQL alert condition.

  1. Get all the conditions for a policy:
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        nrqlConditions(policyId: YOUR_POLICY_ID) {
          nextCursor
          results {
            id
            name
            description
            enabled
            nrql {
              query
            }
            signal {
              aggregationWindow
              aggregationMethod
              aggregationDelay
              aggregationTimer
            }
            policyId
            runbookUrl
            terms {
              duration
              operator
              priority
              timeFunction
              threshold
            }
            type
            violationTimeLimitSeconds
          }
        }
      }
    }
  }
}
  1. Get the details for a single condition:
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        nrqlCondition(id: "YOUR_CONDITION_ID") {
          description
          id
          enabled
          name
          nrql {
            query
          }
          signal {
            aggregationWindow
            aggregationMethod
            aggregationDelay
            aggregationTimer
          }
          policyId
          runbookUrl
          terms {
            operator
            priority
            threshold
            thresholdDuration
            thresholdOccurrences
          }
          type
          violationTimeLimitSeconds
        }
      }
    }
  }
}
  1. Create a mutation with the description.

Here's an empty mutation template:

mutation {
  alertsNrqlConditionStaticUpdate(accountId: YOUR_ACCOUNT_ID, id: "YOUR_CONDITION_ID", condition: {description: ""}) {
    description
  }
}

Here's an example mutation with an included example description:

mutation {
alertsNrqlConditionStaticUpdate(accountId: 123456, id: "123456", condition: {
description: "timestamp : {{timestamp}} \n accountId : {{accountId}} \n type : {{type}} \n event : {{event}} \n description : {{description}} \n policyId : {{policyId}} \n policyName: {{policyName}} \n conditionName : {{conditionName}} \n conditionId : {{conditionId}} \n product : {{product}} \n conditionType : {{conditionType}} \n RunbookUrl : {{runbookUrl}} \n nrqlQuery : {{nrqlQuery}} \n nrqlEventType : {{nrqlEventType}} \n targetID : {{targetId}} \n targetName : {{targetName}} \n commandLine : {{tag.commandLine}} \n entityGuid : {{tag.entityGuid}} \n entityName : {{tag.entityName}} \n fullHostname : {{tag.fullHostname}} \n instanceType : {{tag.instanceType}} \n processDisplayName : {{tag.processDisplayName}}"}
) {
description
}
}

Delete conditions

You can use the alertsConditionDelete mutation to delete any type of condition. You can only request the id field on a delete mutation; for example:

mutation {
  alertsConditionDelete(accountId: YOUR_ACCOUNT_ID, id: YOUR_CONDITION_ID) {
    id
  }
}
Copyright © 2022 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.