Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort results by aggregate in Cloudwatch Log Insights?

I have a pretty straightforward query:

fields @timestamp, req.url, msg
| sort @timestamp desc
| filter msg = "request completed"
| stats count() by req.url

It presents all requests served by my app aggregated by url. However, I would also like to sort the results by the value of aggregate count() - but both | sort count desc and | sort "count()" desc don't work. How can I achieve that?

like image 226
Max Yankov Avatar asked Mar 03 '20 09:03

Max Yankov


People also ask

How do I sort CloudWatch Logs?

Use the sort command to display log events in ascending ( asc ) or descending ( desc ) order. Use the limit command to specify the number of log events that you want your query to return. Use the parse command to extract data from a log field and create an ephemeral field that you can process in your query.

How do I use aggregate Logs in CloudWatch?

To run a query with an aggregation function In the navigation pane, choose Logs, and then choose Logs Insights. In the Select log group(s) drop down, choose one or more log groups to query. You can enter the name of log groups that you want to query in the search bar.

How do I run a query in AWS CloudWatch Logs?

To run a CloudWatch Logs Insights sample query In the navigation pane, choose Logs, and then choose Logs Insights. On the Logs Insights page, the query editor contains a default query that returns the 20 most recent log events. In the Select log group(s) drop down, choose one or more log groups to query.


1 Answers

Turns out, all I had to do was to use an alias and then sort by it:

fields @timestamp, msg, req.url
| filter msg="request completed"
| stats count() as count by req.url
| sort count desc
like image 174
Max Yankov Avatar answered Sep 18 '22 23:09

Max Yankov