Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse mixed text and JSON log entries in AWS CloudWatch for Log Metric Filter

Tags:

I am trying to parse log entries which are a mix of text and JSON. The first line is text representation and the next lines are JSON payload of the event. One of the possible examples are:

2016-07-24T21:08:07.888Z [INFO] Command completed lessonrecords-create {   "key": "lessonrecords-create",   "correlationId": "c1c07081-3f67-4ab3-a5e2-1b3a16c87961",   "result": {     "id": "9457ce88-4e6f-4084-bbea-14fff78ce5b6",     "status": "NA",     "private": false,     "note": "Test note",     "time": "2016-02-01T01:24:00.000Z",     "updatedAt": "2016-07-24T21:08:07.879Z",     "createdAt": "2016-07-24T21:08:07.879Z",     "authorId": null,     "lessonId": null,     "groupId": null   } } 

For these records I try to define Log Metric Filter to a) match records b) select data or dimensions if possible.

According to the AWS docs JSON pattern should look like this:

{ $.key = "lessonrecords-create" }

however, it does not match anything. My guess is that because of mix text and JSON in a single log entry.

So, the questions are: 1. Is it possible to define a pattern that will match this log format? 2. Is it possible to extract dimensions, values from such a log format? 3. Help me with a pattern to do this.

like image 507
Mike Chaliy Avatar asked Jul 24 '16 21:07

Mike Chaliy


People also ask

When creating a custom metric filter in CloudWatch logs what is filtered?

You can search and filter the log data coming into CloudWatch Logs by creating one or more metric filters. Metric filters define the terms and patterns to look for in log data as it is sent to CloudWatch Logs.

How do I find CloudWatch logs for string?

Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Log groups. For Log Groups, choose the name of the log group containing the log stream to search. For Log Streams, choose the name of the log stream to search.


1 Answers

If you set up the metric filter in the way that you have defined, the test will not register any matches (I have also had this issue), however when you deploy the metric filter it will still register matches (at least mine did). Just keep in mind that there is no way (as far as I am aware) to run this metric filter BACKWARDS (ie. it will only capture data from when it is created). [If you're trying to get stats on past data, you're better off using log insight queries]

I am currently experimenting with different parse statements to try and extract data (its also a mix of JSON and text), this thread MAY help you (it didn't for me) Amazon Cloudwatch Logs Insights with JSON fields .

UPDATE! I have found a way to parse the text but its a little bit clunky. If you export your cloudwatch logs using a lamda function to SumoLogic, their search tool allows for MUCH better log manipulation and lets you parse JSON fields (if you treat the entire entry as text). SumoLogic is also really helpful because you can just extract your search results as a CSV. For my purposes, I parse the entire log message in SumoLogic, extract all the logs as a CSV and then I used regex in Python to filter through and extract the values I need.

like image 100
David A Avatar answered Sep 28 '22 03:09

David A