The New Relic Query Language (NRQL) allows you to parse, transform, and analyse your data. NRQL is a very powerful query language, however queries can become complex and difficult to update in bulk. That is why we are excited to introduce NRQL Macros, a way to save a NRQL expression as a reusable, named shortcut.
Macros create a single source of truth for a snippet of NRQL, as well as shrink the amount of text in your query so it is easily understood by others.
In the new “NRQL Macros” section under “Administration” (or via our API), you can create and edit macros. Once named and created, macros can be used in a NRQL query by adding a “$” then the name of the macro. Below are three key examples how macros can be utilized:
1) Simplify a large query
Queries can become very large and complex. Using Macros you can group a large section of that query (Eg. a WHERE clause or WITH clause) all into one macro.
Pre-macro: SELECT count(*) FROM Transaction WHERE appId = 1234 AND appName!=’test’ AND http.statusCode IN (400, 401, 404)
Post-macro: SELECT count(*) FROM Transaction WHERE $where_macro
2) Create an alias for attributes with multiple namings
Not all data comes into New Relic name exactly the same thing, however they are indeed the same attribute. Using macros you can alias all of these namings.
Pre-macro: SELECT (podName OR pod_name OR pod.name) FROM Log
Post-macro: SELECT $podnameAlias FROM Log
3) Update an entire dashboard with one edit
Say you have a comprehensive dashboard with 30 widgets showing different angles of PageView data, however there is an addition you need to make to every widget (Eg. There is a test user that needs to be excluded from the data so it doesn’t skew it). Now you can update every widget by changing a single macro.
Pre-macro of widgets similar to this query: SELECT count(*) FROM PageView WHERE enduser.id != ‘test-user’
Post-macro of widgets similar to this query: SELECT count(*) FROM PageView WHERE $test_user_exclude
Having a macro in each widget gives you the ability to make changes to every widget with one change. No longer do you have to click into each widget one by one.
These three examples help give you a sense of what is possible, and hopefully spark some ideas on how you can simplify and/or optimize your NRQL queries.
To learn more about all the ins and outs of macros, check out our documentation.
本ブログに掲載されている見解は著者に所属するものであり、必ずしも New Relic 株式会社の公式見解であるわけではありません。また、本ブログには、外部サイトにアクセスするリンクが含まれる場合があります。それらリンク先の内容について、New Relic がいかなる保証も提供することはありません。