I am using this query in CloudWatch Logs Insights:
filter level = 'ERROR' and not isempty(exception)
| parse exception /S+.(?<Exception>w+(Exception|Error))/
| parse exception /(?<ExceptionFull>S+(Exception|Error))/
| stats count() as NumExceptions by Exception, ExceptionFull
| sort NumExceptions desc, Exception asc
| display Exception, NumExceptions, ExceptionFull
It shows how many times a specific exception occurs. The problem is that it only lists the first exception from a message, while the message may contain multiple exceptions. For example, the following message contains TaskExecutionException
and IllegalStateException
:
mypackage.TaskExecutionException: Unexpected error
at mypackage.TaskProcessor.doWork(TaskProcessor.java:50)
at mypackage.TaskScheduler.run(TaskScheduler.java:100)
at mypackage.Service.main(Service.java:90)
Caused by: java.lang.IllegalStateException: Attempted to run a task with invalid input
at mypackage.TaskExecutor.executeTask(TaskExecutor.java:51)
... 8 more
but the regex stops on TaskExecutionException
, and CloudWatch Logs Insights doesn’t seem to support multiple iterations over a single message. My log data is in JSON format, so multi-line logs (such as stack traces) are handled as single entries.
Is there any way to achieve this in CW Logs Insights? To display all exceptions even when a single log entry has nested exceptions in the stack trace? Or at least display the root cause instead of the top exception from the stack trace?