12

Suppose you have logs with some transaction ID and timestamp

12:00: transactionID1 handled by funcX
12:01: transactionID2 handled by funcX
12:03: transactionID2 handled by funcY
12:04: transactionID1 handled by funcY

I want to get the time between 2 logs of the same event and aggregate (e.g. sum, avg) the time difference.

For example, for transactionID1, the time diff would be (12:04 - 12:01) 3min and for transactionID2, the time diff would be (12:03 - 12:02) 1min. Then I'd like to take the average of all these time differences, so (3+1)/2 or 2min.

Is there a way to that?

kane
  • 5,465
  • 6
  • 44
  • 72

1 Answers1

0

This doesn't seem possible with CloudWatch alone. I don't know where your logs come from, e.g. EC2, Lambda function. What you could do is to use the AWS SDK to create custom metrics.

Approach 1

If the logs are written by the same process, you can keep a map of transactionID and startTimein memory and create a custom metric with transactionID as dimension and calculate the metric value with the startTime. In case the logs are from different processes e.g. Lambda function invocations, then you can use DynamoDB to store the startTime.

Approach 2

If the transactions are independent you could also create custom metrics per transaction and use CloudWatch DIFF_TIME which will create a calculated metric with values for each transaction.

With CloudWatch AVG it should then be possible to calculate the average duration.

Personally, I have used the first approach to calculate a duration across Lambda functions and other services.

st.huber
  • 1,481
  • 2
  • 24
  • 45