Before you create a macro, review the macro permissions and the naming and expression rules below. A macro that doesn't follow these rules fails validation when you try to save it.
Macro rules and limitations
重要
Macros are visible to all users in your organization. Don't include sensitive or account-specific information when you create or update a macro.
Access macros
In the New Relic platform, go to Administration > NRQL Macros. From this page, you can view, create, edit, and delete macros.
Create a macro
From the NRQL Macros page, create a macro by providing a unique name and an expression. Follow the naming and expression rules. Don't prefix the name with the $ character. For example, to save a WHERE clause condition, enter appName = 'ProductionApp' AND environment = 'prod' without the WHERE keyword.
After you create a macro, it appears in the NRQL Macros list. You can use it in any NRQL query throughout New Relic.
You can also create, update, and query macros programmatically using the NerdGraph API. Before creating or updating macros, review the naming rules, expression rules, and limitations.
To get started, go to the NerdGraph API explorer. If you need help using the NerdGraph API explorer, refer to the NerdGraph API explorer tutorial.
Use the entityManagementCreateNrqlMacro mutation:
mutation CreateMacro($name: String!, $expression: String!, $orgId: ID!) { entityManagementCreateNrqlMacro( nrqlMacroEntity: { expression: $expression name: $name scope: { type: ORGANIZATION, id: $orgId } } ) { entity { id name expression } }}Example variables:
{ "name": "prod_filter", "expression": "(appName = 'ProductionApp' AND environment = 'prod')", "orgId": "your-organization-id"}To find your organization ID, refer to Find your organization or macro ID.
Use a macro in a query
To use a macro in your NRQL queries, type $ followed by the macro name in the query builder. For example, suppose you have a macro named prod_filter with the expression appName = 'ProductionApp' AND environment = 'prod'. You can use it in a query like this:
SELECT count(*) FROM Transaction WHERE $prod_filter SINCE 1 hour agoWhen the query executes, New Relic replaces the macro with its expression:
SELECT count(*) FROM Transaction WHERE appName = 'ProductionApp' AND environment = 'prod' SINCE 1 hour agoヒント
Wrap macro names containing special characters in backticks at query time. For example: $`my#Macro`.
重要
Macros are inlined prior to execution, which affects binary operator precedence. For example, a macro named five defined as 1 + 4 and used as $five * 3 evaluates to 13, not 15, because the expression becomes 1 + 4 * 3. To avoid this, wrap the expression in parentheses when you define the macro, as in (1 + 4), or wrap the macro in parentheses when you use it, as in ($five) * 3. Either approach correctly evaluates to 15.
The following examples show macros solving common query problems:
Manage macros
Edit a macro
From the NRQL Macros page, select the macro and update its expression, following the expression rules.
You can also update a macro's expression programmatically using NerdGraph. Review the expression rules before you update it.
Use the entityManagementUpdateNrqlMacro mutation.
mutation UpdateMacro($expression: String!, $macroId: ID!) { entityManagementUpdateNrqlMacro( id: $macroId nrqlMacroEntity: { expression: $expression } ) { entity { id name expression } }}Example variables:
{ "expression": "(appName = 'ProductionApp' AND environment = 'prod' AND region = 'us-east-1')", "macroId": "your-macro-id"}To find a macro's ID, refer to Find your organization or macro ID.
重要
When you edit a macro, all queries using that macro automatically use the updated version. Review your changes to make sure they don't break existing queries. Due to caching, there may be a slight delay before the update takes effect. You can't update a macro's name after creation. If you need to change the name, delete the macro and create a new one.