Search code examples
aws-cloudwatch-log-insights

Cloudwatch Logs insights - Count 500 & total requests in the same query for Visualization


I am building a cloud watch dashboard to show the availability of our service. For this metrics, I need to get the number of 500 errors and the total requests. I have the below Logs Insights Query.

fields resourcePath, userAgent
| filter resourcePath = "myService/orders/{id}"
| stats bin(1w), count(status) by status

Produce output like

status bin(4w) count(status)
---------------------------
500             7
200             90

But, when I click the Visualization, it shows The data is not suitable for a line chart. . I need to show two lines one for 200 and one for 500. Is it possible in a single query? Thanks in advance.

I can't use the API Gateway's metrics as I need to manually filter few calls from specific userAgent.


Solution

  • Visualizations only support a single bin operator in the by expression.

    Give this a try:

    fields resourcePath, userAgent
    | filter resourcePath = "myService/orders/{id}"
    | fields (status == 200) as is200, (status == 500) as is500
    | stats sum(is200) as total200, sum(is500) as total500 by bin(1w)