Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count unique values in aws cloudwatch metric

I have a set of cloudwatch logs in json format that contain a username field. How can I write a cloudwatch metric query that counts the number of unique users per month?

like image 716
postelrich Avatar asked Jun 26 '17 17:06

postelrich


People also ask

What is sample count in CloudWatch metric?

SampleCount is the number of data points during the period. Sum is the sum of the values of the all data points collected during the period. Average is the value of Sum/SampleCount during the specified period. Minimum is the lowest value observed during the specified period.

Can you tag CloudWatch metrics?

You can add tags to existing rules by using the tag-resource AWS CLI command and the TagResource API. Metric streams – You can tag metric streams when you create them by using the put-metric-stream AWS CLI command and the PutMetricStream API.


2 Answers

You can now do this! Using CloudWatch Insights.

API: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_StartQuery.html

I am working on a similar problem and my query for this API looks something like:

fields @timestamp, @message
| filter @message like /User ID/
| parse @message "User ID: *" as @userId
| stats count(*) by @userId

To get the User Ids. Right now this returns with a list of them then counts for each one. Getting a total count of unique can either be done after getting the response or probably by playing with the query more.

You can easily play with queries using the CloudWatch Insights page in the AWS Console.

like image 117
ImperviousPanda Avatar answered Sep 21 '22 21:09

ImperviousPanda


Now you can count unique field values using the count_distinct instruction inside CloudWatch Insights queries.

Example:

fields userId, @timestamp
| stats count_distinct(userId)

More info on CloudWatch Insights: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html

like image 22
Leonardo Kuffo Avatar answered Sep 22 '22 21:09

Leonardo Kuffo