Show number of sessions with an error on AWS CloudWatch Dashboard

0

I'm trying to display the number of sessions with failures on an AWS CloudWatch dashboard (type: gauge).

To enable this, I created a Metric Filter for the log-group, that will create a metric per occurrence of loglevel=ERROR. The value is the 1. Then I created a dashboard that displays a gauge showing the sum of events.

This is working fine, but sometimes there are multiple ERROR logs for a single session. I could set the value of a metric to the session_id, but then I would have to apply some kind of unique or distinct filter, so I see only one occurrence per session (metric-value). Is that possible, or is there another approach that would lead to the desired status?

1 Answer
0

Hi there,

It is possible to achieve what you want using CloudWatch Logs Metric Filters and some additional processing. Here's one approach you can take:

  1. Create a Metric Filter to extract the session_id: Create a metric filter on your log group that extracts the session_id from your log events. Set the metric value to 1 for each occurrence of the session_id.

For example, if your log events contain a field like "session_id": "abc123", you can create a metric filter with the following filter pattern:


{ $.session_id = "session_id" }


This will create a metric with the name SessionId and a value of 1 for each log event containing a session_id.

  1. Create a Metric Math to count unique session_ids: Use CloudWatch Metric Math to count the unique session_ids within a given time period. This will give you the number of sessions with failures.

Go to the "Metric Math" section in the CloudWatch console, and create a new metric math expression like this:


UNIQUE(m1, "SessionId")


Here, m1 is the namespace for your log group metrics. This expression will count the unique values of the SessionId metric, effectively giving you the number of distinct sessions with failures.

  1. Create a Dashboard Widget: Finally, create a dashboard widget that displays the result of the metric math expression. Choose the "Gauge" widget type, and select the metric math expression you created in step 2 as the metric to display.

With this approach, you'll get a gauge that shows the number of distinct sessions with failures, even if there are multiple error log entries for a single session.

Alternatively, if you want to count the total number of errors across all sessions, you can skip step 2 and simply create a dashboard widget that displays the sum of the SessionId metric values from step 1. This will give you the total number of error log entries, regardless of the number of distinct sessions.

If you face any further issues here, it is advised to create a Support Ticket to the cloudwatch team through the AWS Support.

AWS
SUPPORT ENGINEER
Sagar_T
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions