Compare NRQL results across time windows to detect anomalies standard alerts can't catch.
Requirements:
- New Relic account
- Email destination (see Send notifications)
- Schedule via CreateSchedule API.
Key actions: newrelic.nrdb.query, newrelic.notification.sendEmail
Use case: Use this pattern when standard alerts can't meet requirements, such as:
Comparing metrics across multiple time windows
Applying custom mathematical operations on query results
Triggering only when specific thresholds or patterns are detected
Combining data from multiple queries with conditional logic
name: Complex_Alert_Workflowdescription: 'Compares NRQL results across time windows and sends alerts when new events are detected'workflowInputs:destinationId:type: Stringquery:type: StringdefaultValue: 'FROM Span SELECT count(*)'steps:- name: query1type: actionaction: newrelic.nrdb.queryversion: 1inputs:query: "${{ .workflowInputs.query }} SINCE 10 minutes ago UNTIL 5 minutes ago"accountIds:- 7401815selectors:- name: lengthexpression: '[ .results[] | length ]'- name: countexpression: '[ .results[0].count ]'- name: query2type: actionaction: newrelic.nrdb.queryversion: 1inputs:query: "${{ .workflowInputs.query }} SINCE 5 minutes ago"accountIds:- 7401815selectors:- name: lengthexpression: '[ .results[] | length ]'- name: countexpression: '[ .results[0].count ]'- name: CheckForNewEventstype: switchswitch:- condition: >-${{ (.steps.query2.outputs.count - .steps.query1.outputs.count) > 0 }}next: sendEmailnext: end- name: sendEmailtype: actionaction: newrelic.notification.sendEmailversion: 1inputs:destinationId: ${{ .workflowInputs.destinationId }}subject: Hello there!message: >-More spans incoming!!!There are --- ${{ (.steps.query2.outputs.count - .steps.query1.outputs.count) }} ---new Spans that were ingested in the last 5 minutesattachments:- type: QUERYquery: ${{ .workflowInputs.query }} SINCE 5 minutes agoformat: CSVfilename: span_count.csvnext: end
To schedule, use CreateSchedule API with cron expression */10 * * * * (every 10 minutes). Minimum interval is 10 minutes. See workflow limits for details.
What's next
- Deployment rollback: Automated deployment monitoring
- EC2 management: Infrastructure automation