Do you want to understand your New Relic data better? New Relic Query Language (NRQL) can help. NRQL lets you analyze your data in real time. Although it can seem overwhelming at first glance, worry not. In this two-part blog, we cover 10 essential NRQL functions and clauses that will help you understand NRQL better and gain deeper insights into your data. These functions and clauses can help you calculate percentages, create heatmaps, create conditional widgets, and so much more. By using NRQL, you can get more out of your data analysis. Let's learn how to use NRQL to gain valuable insights from your data.
In part 1 of this blog, we covered the basic NRQL functions necessary for querying data. In part 2, we dive into the more advanced features of NRQL. We cover topics such as histograms, heatmaps, and advanced comparison operators. We explore how to use NRQL to analyze complex data sets and identify patterns and trends in your data. And we look at some best practices for writing efficient queries and optimizing your NRQL queries for performance.
Number 6: Latest
The latest()
function allows users to retrieve the most recent value of an attribute over a specified range of time. It can take in a single attribute as an input, and return the most recently recorded value.
Here's a simple example to fetch the most recent value of the attribute city in the map widget from the PageView event:
FROM PageView
SELECT LATEST(city) AS 'Name:City'
FACET STRING(asnLatitude, precision: 5) AS 'Lat',
STRING(asnLongitude, precision: 5) As 'Lang'
SINCE 1 HOUR AGO
LIMIT MAX
References
- Learn more about latest.
- Learn more about map widget.
Number 7: If
What do you do when you need to perform a simple if-then-else control flow with your query to get a conditional output? NRQL provides you with an if()
function, which comes in handy when combined with the SELECT
clause.
Here's a simple example where we check the workload status and display the output as On or Off based on the statusValue property:
FROM WorkloadStatus
SELECT LATEST(IF(statusValue = 'OPERATIONAL', '🟢 On', '🔴 Off')) AS 'Workload Status'
SINCE 30 SECONDS AGO
Yes, you can totally use emojis in the labels 😄. NRQL supports most emojis 🤩.
References
- Read more about if().
Number 8: Aparse
aparse
is a great function to extract specific values from a string. It allows you to parse strings that contain structured data and extract specific values from them. This can be especially useful when you need to analyze log data or other types of unstructured data.
aparse()
takes two arguments:
- An attribute with a string value.
- A pattern of characters that includes anchor strings and characters to be extracted. For example, 'www.*.com' can be used to extract only the domain part from a URL.
The following image is an example where the host location contains a pre-fixed value:
In the following sample, the query extracts only the desired part of the string from the result while retaining the location name after removing the prefix part based on the given anchor/regex pattern.
SELECT APARSE(hostname,'host-tower-*') AS hostLocation
FROM SystemSample
WHERE hostname LIKE '%tower%'
References
- Read more about aparse.
Number 9: Subqueries
NRQL also supports subqueries, which are queries within other queries. Subqueries are a powerful way to use multiple queries together in NRQL and generate results from multiple different datasets.
Here's a basic example where we filter and find all transactions for hosts that have log errors, without explicitly specifying any host names:
FROM Transaction
SELECT appId, appName, containerId, request.method
WHERE hostname = (
FROM Log
SELECT latest(host)
WHERE level = 'ERROR'
)
Subquery with IN clause
Another good use case of a subquery is when you want to create a more dynamic query instead of writing multiple values for conditions. We can use the IN()
clause with a subquery, which is useful when the filtering criteria might change or increase over time.
Here's a sample use case:
To find the average duration of all transactions for TransactionError across all the entities, we can use a sample query. This query searches for all entity.guids using a subquery and then applies the result to the WHERE
clause for the aggregation of the average(duration)
.
SELECT AVERAGE(duration) FROM Transaction
WHERE entity.guid IN (SELECT UNIQUES(entity.guid) FROM TransactionError)
FACET appName TIMESERIES LIMIT 3
References
- Read more about subquery.
Number 10: Histograms and heatmaps
Histogram
Histograms allow users to quickly identify patterns, distributions, and trends in their data.
histogram()
accepts three arguments:-
- Attribute name
- The maximum value of the sample range
- Total number of buckets (between 1 and 500) where range boundaries are inclusive
You can generate a histogram of response times from PageView, ranging up to 10 seconds over 20 buckets. The query basically produces an output of 20 different chunks of time (or buckets) on the X-axis for the total number of requests on the Y-axis for the attribute “duration” from the PageView.
SELECT HISTOGRAM(duration, 10, 20) FROM PageView SINCE 1 week ago
Heatmaps
Heatmaps()
are useful for visualizing patterns in a dataset. The color intensity in the heatmap changes from lighter to darker as the values increase or decrease.
To create a heatmap, use the Histogram() function with only a single attribute that is numeric and add a FACET clause. Here's an example of a heatmap showing the different operating systems from which our website has been accessed in the last three hours.
SELECT HISTOGRAM(duration) FROM PageView FACET userAgentOS SINCE 3 HOURS AGO
References
- Read more about histograms.
핵심 요약
Here's a list of all the functions and clauses covered in this series:
By mastering NRQL, you can capture and interpret your data, allowing you to break down the big picture into easily understandable pieces. This will help you identify problems as they occur and analyze your data in real time, gaining valuable insights. Whether you're looking to optimize performance, identify trends, or monitor key metrics to drive business decisions, NRQL is an indispensable tool that can help you understand your data comprehensively.
다음 단계
If you're not already using New Relic, sign up for a free account to test out all of our features. Your free account includes 100 GB/month of free data ingest, one free full-access user, and unlimited free basic users.
- You can learn NRQL interactively by using our NRQL lesson app.
- Explore the New Relic query builder and build your custom charts and dashboards.
- Learn about our NRQL console and become an NRQL master.
- Coming from the Prometheus world? New Relic supports PromQL-style queries.
이 블로그에 표현된 견해는 저자의 견해이며 반드시 New Relic의 견해를 반영하는 것은 아닙니다. 저자가 제공하는 모든 솔루션은 환경에 따라 다르며 New Relic에서 제공하는 상용 솔루션이나 지원의 일부가 아닙니다. 이 블로그 게시물과 관련된 질문 및 지원이 필요한 경우 Explorers Hub(discuss.newrelic.com)에서만 참여하십시오. 이 블로그에는 타사 사이트의 콘텐츠에 대한 링크가 포함될 수 있습니다. 이러한 링크를 제공함으로써 New Relic은 해당 사이트에서 사용할 수 있는 정보, 보기 또는 제품을 채택, 보증, 승인 또는 보증하지 않습니다.