You can use our NerdGraph API to make NRQL queries.
Overview of querying with NerdGraph
You can run NRQL queries using the query builder in the UI, or you can use our NerdGraph API. NerdGraph gives you the ability to do some things you can't do from the UI, such as query across multiple accounts, and run asynchronous queries.
Requirements
- All user types can query data for the accounts they have access to. If you can't query via NerdGraph, it may be because you're lacking access to that account.
- All queries are subject to NRQL query limits
Basic NRQL queries with NerdGraph
To make basic NRQL queries using NerdGraph, there are two main requirements:
- You must pass the NRQL query as a string argument to the NRQL object
- You must include the
results
field in your query
For example, to get a count of all transaction events in the last hour, you'd run the following query, specifying your New Relic account ID.
{ actor { account(id: YOUR_ACCOUNT_ID) { nrql(query: "SELECT count(*) FROM Transaction SINCE 1 HOUR AGO") { results } } }}
This query example would return a result like this:
{ "data": { "actor": { "account": { "nrql": { "results": [ { "count": 1000 } ] } } } }}
Looking for details on how to write NRQL queries? See the NRQL docs section.
Cross-account queries
With NerdGraph, you can run a query across more than one account, which you can't do in the query builder. You'll need the account IDs for the accounts you want to query.
Here's an example of running a cross-account NRQL query:
{ actor { nrql( accounts: [ACCOUNT_ID_1, ACCOUNT_ID_2, ACCOUNT_ID_3] options: {} query: "YOUR_NRQL_QUERY" timeout: 70 ) { results } }}
For how to create a dashboard with data from multiple accounts, see the NerdGraph dashboard tutorial.
Create embeddable charts
In addition to returning raw data, you can fetch embeddable chart links for the data to use in an application. For example, instead of a single count of transaction, you can create a chart that illustrates a time series of bucketed counts over time. Add TIMESERIES
to your query with embeddedChartUrl
:
{ actor { account(id: YOUR_ACCOUNT_ID) { nrql(query: "SELECT count(*) from Transaction TIMESERIES") { embeddedChartUrl } } }}
This NerdGraph query example returns the URL for the chart in the following response:
{ "data": { "actor": { "account": { "nrql": { "embeddedChartUrl": "https://chart-embed.service.newrelic.com/charts/EMBEDDABLE-CHART-ID" } } } }}
If you view the embedded chart URL using any standard HTTP client, it returns an image showing a visualization of the response to the query you submitted. These charts follow the same embedded chart rules as embedded charts that are created elsewhere. To change the style of the data visualization, pass a chartType
argument to embeddedChartUrl
.
Suggested facets
When using NerdGraph to explore your data, you can use the suggestedFacets
field to return suggested attributes for use in faceted NRQL queries.
Longer-running queries
If you need to run longer-running NRQL queries, see Asynchronous queries.
Other querying options
NerdGraph gives you other querying options, such as: