• /
  • EnglishEspañolFrançais日本語한국어Português
  • Log inStart now

Create and manage NRQL macros

|View as Markdown

You can create macros in the New Relic UI or with NerdGraph, use them in your NRQL queries, and edit them when your queries change. To learn what macros are and how parameters work, refer to NRQL macros.

Permissions

If you're an Organization manager or Organization product admin, you have the permissions to create, read, update, and delete any macro in the organization by default. If you're not, the Authentication Domain Manager needs to grant you an organization-scoped custom role with the required NRQL Macros permissions, using either of the following methods:

The following permission IDs control access to macros:

Capability

Permission ID

Description

NRQL Macros > Read

nrql_macros.read.macro

Read any macro in the organization. The Organization read only role includes this capability.

NRQL Macros > Modify

  • nrql_macros.create.macro

  • nrql_macros.read.macro

  • nrql_macros.update.macro

  • nrql_macros.delete.macro

Create, read, update, and delete any macro in the organization. The Organization manager and Organization product admin roles include these capabilities.

Access macros

In the New Relic platform, go to Administration > NRQL Macros. From this page, you can view, create, update, and delete macros.

Macro rules

Before you create or edit a macro, follow the naming, expression, and parameter rules in this section. You can't save a macro that doesn't follow these rules.

Create a macro

Important

Macros are visible to all users in your organization. Don't include sensitive information in a macro. To keep account-specific values out of a macro, use parameters and pass the values in the query where you use the macro. Refer to Parameters in macros.

From the NRQL Macros page, create a macro by providing a unique name and an expression. Follow the naming and expression rules. If you use parameters in the expression, follow the parameter rules and enter the total number of parameters in the optional field for the number of parameters. Leave that field blank for a macro without parameters.

After you create a macro, it appears in the NRQL Macros list. You can use it in any NRQL query in your organization.

You can create macros programmatically using the NerdGraph API. Before creating or updating macros, review the naming rules, expression rules, and parameter rules.

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!
$numParameters: Int
$orgId: ID!
) {
entityManagementCreateNrqlMacro(
nrqlMacroEntity: {
expression: $expression
name: $name
numParameters: $numParameters
scope: { type: ORGANIZATION, id: $orgId }
}
) {
entity {
id
name
expression
numParameters
}
}
}

Set the variables as in the following example:

{
"name": "prod_filter",
"expression": "(appName = 'ProductionApp' AND environment = 'prod')",
"numParameters": null,
"orgId": "your-organization-id"
}

Set numParameters to null for a macro without parameters. If your macro has parameters, set numParameters to the total number of parameters in the expression, such as 2 for $1 and $2.

To find your organization ID, refer to Find your organization or macro ID.

Usage considerations

Consider the following when you use a macro in a query:

  • Special characters: Wrap macro names containing special characters in backticks at query time. For example, $`my#Macro`.
  • Operator precedence: 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.

Use a macro in a query

To use a macro in your NRQL queries, type $ followed by the macro name in the query builder. If the macro has parameters, pass one value for each parameter in parentheses after the macro name, separated by commas, as in $macroName(value1, value2). The values replace the parameters in the order you pass them.

For example, if you have a macro named slow_filter with the expression appName = 'ProductionApp' AND duration > $1 that has one parameter, you can use it in a query by passing a value for the parameter:

SELECT count(*) FROM Transaction WHERE $slow_filter(2) SINCE 1 hour ago

When you execute the query, the macro name is replaced with its expression, and $1 is replaced with the value you passed:

SELECT count(*) FROM Transaction WHERE appName = 'ProductionApp' AND duration > 2 SINCE 1 hour ago

You can also pass a macro as the value of a parameter, so that one macro is used inside another. You can nest macros this way up to 3 levels deep, as in $a($b($c($d))). The innermost macro is evaluated first, and its result is passed to the macro around it. To see how this looks in a query, refer to Example: Pass a macro as a parameter value.

The following examples show macros solving common query problems:

Update macros

Before you update a macro, consider the following:

  • Name: You can't update a macro's name after creation. Queries refer to a macro by its name, so changing the name breaks every query that uses it.
  • Expression: When you update a macro expression, 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 delay before the update takes effect.

To update a macro, use either of the following methods:

From the NRQL Macros page, select the macro and update its expression, following the expression rules. If you change the number of parameters in the expression, update the field for the number of parameters to match.

You can update a macro's expression programmatically using NerdGraph. Review the expression rules before you update it.

Use the entityManagementUpdateNrqlMacro mutation.

mutation UpdateMacro($expression: String!, $numParameters: Int, $macroId: ID!) {
entityManagementUpdateNrqlMacro(
id: $macroId
nrqlMacroEntity: { expression: $expression, numParameters: $numParameters }
) {
entity {
id
name
expression
numParameters
}
}
}

Set the variables as in the following example:

{
"expression": "(appName = 'ProductionApp' AND environment = 'prod' AND region = 'us-east-1')",
"numParameters": null,
"macroId": "your-macro-id"
}

To update a macro with parameters, set numParameters to the number of parameters in the new expression.

To find a macro's ID, refer to Find your organization or macro ID.

Find your organization or macro ID

NRQL macros

Learn what NRQL macros are, what you can do with them.

NRQL reference

Explore NRQL syntax, clauses, and functions.

Copyright © 2026 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.