Skip to content

How to group log events using correlationid in cloudwatch log insight

0

I have multiple log events in a log stream which I want to group. One event has question and another event has answer. Both events have common correlationid field present.

How can I group these log events and run a query in log insight so that I can get question and its corresponding answer in single line tabular format.

asked 2 years ago1.2K views

1 Answer
-1

To group log events using a correlationid in CloudWatch Logs Insights and display the question and its corresponding answer in a single line tabular format, you can use the stats command along with string concatenation. Here's how you can approach this:

  1. Open the CloudWatch console and navigate to Logs Insights.

  2. Select the log group(s) containing your log events.

  3. In the query editor, you can use a query similar to the following:

fields @timestamp, correlationid,
    filter @message like /question/ as question,
    filter @message like /answer/ as answer
| stats latest(question) as Question, latest(answer) as Answer by correlationid
| sort @timestamp desc
| limit 100

This query does the following:

  • It filters the log messages to identify those containing "question" and "answer".
  • It uses the stats command to group the events by correlationid.
  • It selects the latest question and answer for each correlationid.
  • The results are sorted by timestamp in descending order and limited to 100 rows.

You may need to adjust the filters (e.g., like /question/ and like /answer/) based on the exact format of your log messages.

  1. Run the query by clicking the "Run query" button.

  2. The results will be displayed in a tabular format, showing the correlationid, Question, and Answer in separate columns.

  3. You can further customize the visualization by selecting the "Visualization" tab and choosing an appropriate chart type, such as a table view.

This approach allows you to group related log events based on the common correlationid field and present the question and its corresponding answer together in the results. You can modify the query as needed to include additional fields or apply more specific filters to refine your results.
Sources
Tutorial: Run a query that produces a visualization grouped by log fields - Amazon CloudWatch Logs
Tutorial: Run a query with an aggregation function - Amazon CloudWatch Logs

answered 2 years ago

EXPERT

reviewed 2 years ago

  • This is not valid CW Insights syntax:

    fields @timestamp, correlationid,
        filter @message like /question/ as question,
        filter @message like /answer/ as answer
    

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.