As a New Relic user, you may find yourself writing complex NRQL queries repeatedly across dashboards. Remembering and rewriting multiple lines of a query each time is time-consuming and prone to errors. When your data changes and queries need to be adjusted, you have to manually update each instance of that query across your organization, which becomes a maintenance burden.
NRQL macros solve this problem by letting you save a NRQL expression as a reusable, named shortcut. You reference a macro in a query with the $ symbol followed by its name, for example $prod_filter, and New Relic substitutes the saved expression before the query runs. You can use macros in the query builder and in dashboards.
What you can do with macros
You can use NRQL macros to do the following:
- Create reusable expressions: Convert an expression of your NRQL query into a macro with a descriptive name, so you don't have to retype or remember the full expression each time.
- Update queries centrally: Edit a macro once, and all queries using that macro automatically reflect the change, without you having to find and update every query individually.
- Pass parameters to a macro: Add parameters to a macro so that one macro works in multiple places where the same expression is used and only a few values change. The structure of the expression stays the same, and you can pass different values each time you use the macro. You maintain one macro instead of a separate macro for each variation. Refer to Parameters in macros.
- Reuse expressions across accounts: Use a macro in any account in your organization without redefining it. Macros are organization-level entities, and all users with the appropriate role can access and use all macros created within the organization.
Prerequisites
To use NRQL macros, you need the following:
- Account access: Access to an account in the organization where you want to use macros. Macros are organization-level entities, so once created, they're available across every account in your organization.
- NRQL knowledge: Basic knowledge of NRQL syntax, since a macro's expression is a NRQL expression.
Parameters in macros
A macro saves a NRQL expression from your query as a reusable, named shortcut. Sometimes the same expression appears in many queries and only a few values in it change from query to query. Instead of saving a separate macro for each variation, you can save the expression once and mark the parts that change as parameters.
A parameter is a placeholder you write inside a macro expression. You write it as $ followed by a number, such as $1 or $2. When you use the macro in a query, you pass a value for each parameter in parentheses after the macro name, separated by commas. The first value you pass maps to $1, the second value maps to $2, and so on, and each parameter is replaced with the value it maps to.
For example, if a macro named add has the expression ($2 + $1), the query $add(5, 3) replaces $1 with 5 and $2 with 3, so the expression becomes (3 + 5).
Turn a query into a macro with parameters
Suppose you write this query to show the duration in milliseconds, because duration is in seconds:
FROM Transaction SELECT duration * 1000 AS 'durationMs'To reuse the same calculation with other attributes and other multipliers, write the parts that change as parameters in a macro expression:
($1 * $2)If you save this expression in a macro named scale, you pass the attribute as the first value and the multiplier as the second value:
FROM Transaction SELECT $scale(duration, 1000) AS 'durationMs'New Relic replaces $1 with duration and $2 with 1000, so the calculation becomes (duration * 1000).
Organization association
Macro access is organization-level only. Users who have access can use all macros created within the organization. Every macro is a resource that belongs to the organization in which it was created. As a result:
- This association is permanent and can't be changed.
- Macros can't be shared between different organizations.
- If the organization is deleted, the macro is deleted as well.
- If a user moves to a different organization, they won't have access to macros from their previous organization.
Limitations
NRQL macros have the following limitations:
- Alerts: Macros aren't supported in alert conditions.
- Autocomplete: The query builder doesn't suggest macros or use macro expressions to refine suggestions.
- Template variables: You can't use template variables within macros.
- Nested macros: A macro's expression can't contain a reference to another macro, but you can pass a macro as the value of a parameter in a query, up to 3 levels deep. Refer to Example: Pass a macro as a parameter value.
- Region-specific: Macros are region-specific and can only be used within your organization's primary region.
- Time clauses: Clauses related to time, including
TIMESERIES,PREDICT,BY,USING,SINCE,UNTIL, andCOMPARE WITH, don't support macros.
Next steps
To create, use, or manage macros, refer to Create and manage macros. To learn who can create, edit, and use macros, refer to Permissions.