New Relic query editor
This document explains how to use the New Relic query editor to build queries in Grafana.
Before you begin
- Ensure you have configured the New Relic data source.
- Verify your API key has the appropriate permissions for the data you want to query.
Query types
The query editor supports five query types, selectable from the Service switcher at the top of the editor:
- Metrics — Query APM metric data for a specific application.
- Data Explorer — Build NRQL queries visually without writing raw NRQL.
- NRQL Editor — Write raw NRQL queries with code completion.
- Logs — Search and view logs stored in New Relic.
- Traces — Search for distributed traces or look up a specific trace by ID.
Metrics
The Metrics query type lets you query APM metric data for a specific application.
Note
The search icon next to the Application and Metric selectors returns up to 1000 results. Use the name filter to narrow results if you have more than 1000 applications or metrics. Search is case-sensitive.
If you don’t find an option in the drop-down lists, you can create a custom entry by selecting Create: (your text).
Data Explorer
Data Explorer lets you build NRQL queries visually without writing raw NRQL. The editor generates a NRQL query based on your selections, which you can preview before running.
You can preview the generated NRQL query in the NRQL preview section. To further customize the query, click Switch to NRQL Editor to switch to the raw NRQL editor.
The available aggregation functions in the Select field include: count, average, sum, latest, median, max, min, uniqueCount, and uniques.
For more information about NRQL syntax, refer to the New Relic NRQL documentation.
NRQL Editor
The NRQL Editor lets you write raw New Relic Query Language queries. Code completion displays as you type — highlight an option with the arrow keys and press Tab to select it.
Macros
The NRQL Editor supports the following macros for dynamic queries.
NRQL query examples
Average transaction duration as a time series:
SELECT average(duration) FROM Transaction $__timeFilter TIMESERIESError rate by application:
SELECT percentage(count(*), WHERE error IS true) FROM Transaction FACET appName $__timeFilter TIMESERIES95th percentile response time:
SELECT percentile(duration, 95) FROM Transaction WHERE appName = 'my-app' $__timeFilter TIMESERIESTop 10 slowest transactions (table format):
SELECT average(duration), count(*) FROM Transaction FACET name $__timeFilter LIMIT 10Using the $__timeSeries macro for dynamic aggregation:
SELECT average(cpuPercent) FROM SystemSample $__timeFilter $__timeSeries(60m, 1 MINUTE, MAX)Note
Always include the
$__timeFiltermacro in your NRQL queries. Without it, query results don’t sync with the dashboard time range.
COMPARE WITH queries
NRQL queries that use the COMPARE WITH clause automatically render comparison data as dotted lines in time-series visualizations, making it easy to compare current data with historical periods.
For example, compare this week’s response time with last week:
SELECT average(duration) FROM Transaction $__timeFilter TIMESERIES COMPARE WITH 1 WEEK AGOSynthetics queries
You can query New Relic Synthetics data from the SyntheticCheck and SyntheticRequest tables using the NRQL Editor.
The following example returns the count of all requests by response status:
SELECT count(*) FROM SyntheticRequest FACET responseStatus $__timeFilterThe following example returns the success rate for all monitors:
SELECT (
filter(count(*), WHERE result = 'SUCCESS')
/ filter(count(*), WHERE result IN ('SUCCESS', 'FAILED'))
* 100
) as 'Success Rate' FROM SyntheticCheck
FACET monitorName $__timeFilter TIMESERIESFor more information about Synthetics monitoring queries, refer to Events reported by synthetic monitoring.
Logs
The Logs query type lets you search and view logs stored in the New Relic Log table. Results are displayed in Grafana’s log format and are automatically filtered by the dashboard time range. A maximum of 2000 records are returned per query due to NRQL API limitations.
Note
The logs volume panel in Explore can help identify periods with high log volume. If your query returns the maximum 2000 records, narrow the time range to view all relevant logs.
Log query examples
Search for error logs from a specific container:
message LIKE '%ERROR%' AND container_name = 'petclinic'Filter logs by severity level:
level = 'ERROR' OR level = 'FATAL'Search for logs containing a specific trace ID:
traceId = '951ae3313c093aa3278cbc27c0714c94'If a log row contains a field such as traceId, traceid, or trace.id, the logs panel provides a link to the corresponding trace view.
Traces
The Traces query type lets you search for distributed traces or look up a specific trace by ID.
The Query Type selector determines the mode:
Search
Search mode lets you find traces by filtering on span attributes.
Trace ID
Trace ID mode lets you look up a specific trace by its ID.
For more information about the New Relic tracing API, refer to Query distributed trace data.


