Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

How to count unique dimensions in AWS CloudWatch metrics?

Let's say my application publishes AWS CloudWatch metrics with a single dimension. The value of that dimension can be any string. Then, in any given time interval, I'll have something like this:

(myDimension: "foo", value: 12)
(myDimension: "bar", value: 5)
(myDimension: "foo", value: 99)
(myDimension: "quux", value: 42)

Now I want to create an alarm based on a metric derived from this. I want to ignore the values and only count the unique occurrences of each dimension. In this case, the value would be 3 -- 1 for "foo" (even though it appeared twice), 1 for "bar" and 1 for "quux".

How can I do that? I've looked at the Metric Math documentation, but I didn't find any function that could help me. The one that seems to get close is IF, but I'm not sure if I can use it to solve this problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it using CloudWatch Insights https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html

Create a query similar to this:

parse @message '*(myDimension: "*"*' as f1, myDimensionKey, f2
| filter @message like '(myDimension:'
| stats count(*) by myDimensionKey

And add the result to a dashboard. This would display a table with the distinct keys and the number of occurrences


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...