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

后端 未结 3 1290
滥情空心
滥情空心 2021-02-18 19:08

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 ex

相关标签:
3条回答
  • 2021-02-18 19:22

    you can use fluentd agent to send logs to Cloudwatch. Create custom grok pattern based on your metric filter.

    Steps:

    • Install fluentd agent in your server
    • Install fluent-plugin-cloudwatch-logs plugin and fluent-plugin-grok-parser plugin
    • write your custom grok pattern based on your log format

      please refer this blog for more information

    0 讨论(0)
  • 2021-02-18 19:47

    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.

    0 讨论(0)
  • 2021-02-18 19:47

    You can use CloudWatch Events for such purpose(aka Subscription Filters), what you will need to do is define a cloudwatch Rule which uses an expression statement to match your logs. Here, I will let you do all the reading:

    https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html

    https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Scheduled-Rule.html

    :)

    0 讨论(0)
提交回复
热议问题