'Add UUID for request till response in cloudwatch logs

I am pushing logs from gradle project to Cloudwatch. The configuration looks like

        "logDriver": "awslogs",
        "options": {
          "awslogs-datetime-format": "%Y-%m-%d %H:%M:%S%L",
          "awslogs-region": "ap-southeast-1",
          "awslogs-stream-prefix": "ecs",
          "awslogs-group": "/ecs/xxxx"
        }
      }

I want to push some UUID to each log so that I can distinguish each request till its response.

Basically, I am looking for something like %X regex in log4j2.yml file where we just set a UUID in MDC context and it is printed for every log.

How can I configure cloudwatch config to use unique identifier for logs for request till response?

I saw this but I am not finding any proper regex to add-in.



Solution 1:[1]

It seems like you want to create a unique log stream for each request, and the simple answer is that you can't.

You're using the awslogs driver, which is configured at the time the container starts, and does not change while the container is running. You could possibly do this with an agent configuration if you use default (stdout/stderr) logging and are running on an EC2 instance, but that would be a really bad idea.

I'm assuming that your container processes web requests. In which case, if you did hack something together to send each request to a different stream, here's what would happen:

  • You would almost certainly run into the CreateLogStream throttling rate of 50 requests per second.
  • You would end up with thousands (or, depending on your site's activity, millions) of individual log streams. This will be a nightmare to manage, since AWS does not automatically delete streams. Just listing them all would cause you to hit the quota in DescribeLogStreams.

Instead, I recommend that you add your UUID to your request inside your app, via the MDC, and use CloudWatch Logs Insights to extract the messages for a single request.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1