Athena query AWS metrics value
Vấn đề:
Metrics view trên Dashboard mặc định của CW khá hạn chế
Metrics Insight hiện tại chỉ query được 3 tiếng gần nhất=> Cần một cơ chế query khác!
Để query, có thể sử dụng thằng này:
https://docs.aws.amazon.com/athena/latest/ug/connectors-cwmetrics.html
Cách cài đặt và sử dụng:
Tạo cloud9, clone repo:
git clone https://github.com/awslabs/aws-athena-query-federation.git
cd athena-cloudwatch-metrics
Tải connector tới cloudwatch metrics:
Tải tại đây: https://github.com/awslabs/aws-athena-query-federation/releases
Và đưa vào thư mục: aws-athena-query-federation/athena-cloudwatch-metrics/target
Chuẩn bị môi trường:
cd aws-athena-query-federation
./tools/prepare_dev_env.sh
Lưu ý: Trước khi chạy tool chuẩn bị môi trường, cần sửa trong file bash:
#sudo yum -y install java-11-openjdk-devel
# If using amazon linux and the above doesn't work, you can try this line instead
sudo amazon-linux-extras install -y java-openjdk11
Comment dòng sudo yum -y install java-11-openjdk-devel và sử dụng dòng sudo amazon-linux-extras install -y java-openjdk11 nếu như tạo cloud9 sử dụng AMZ linux 2.
Sau khi chạy xong, thì:
source ~/.profile
cd athena-cloudwatch-metrics
sam deploy --template-file athena-cloudwatch-metrics.yaml -g
Điền các thông tin cần thiết để deploy.
Sau khi deploy, SAM CLI template sẽ tạo ra 1 Serverless App như sau:
metrics
metric_samples
Để query các metdata, thì dùng bảng metrics, xem values thì chúng ta sử dụng metrics_samples. Lưu ý rằng, dữ liệu sẽ rất nhiều nên chúng ta cần chỉ định khoảng thời gian để tiết kiệm thời gian và chi phí. Thời gian trong câu query của Athena sẽ sử dụng epoch time, chúng ta có thể dùng tool sau quy đổi:
https://www.epochconverter.com/
Ví dụ muốn query trong ngày 6/11:
Và chỉ muốn query metrics CPUUtilization của EC2 thôi, câu lệnh sẽ có dạng như sau:
select * from "lambda:test-cw2".default.metric_samples where "namespace" = 'AWS/EC2' and "metric_name" = 'CPUUtilization' and timestamp between 1699228800 and 1699315199;
Kết quả trả về trên Athena:
Xem và download sample result tại đây:
https://github.com/shinchan79/test-data/blob/main/ec2utilization611.csv
\====
Note thêm:
Ngoài cách query trực tiếp như bên trên, thì sẽ có cách khác là sử dụng CloudWatch metrics streams, đưa dữ liệu CW metrics near realtime về S3, đây là cách còn có thể tích hợp với các bên thứ ba một cách dễ dàng: https://aws.amazon.com/blogs/aws/cloudwatch-metric-streams-send-aws-metrics-to-partners-and-to-your-apps-in-real-time/
Chi phí cho thằng metrics streams này là $0.003/1000 cập nhật, ngoài ra sẽ cần trả thêm chi phí lưu trữ S3 và chi phí của Kinesis Firehose
Hoặc call api get-metrics-data, cách này thì cần tự xử lý code (maybe nghiên cứu theo hướng batching). Chi phí: $0.01 cho 1000 request.
Theo câu trả lời của AWS support:
From the case notes I can understand that you want to export metrics from CloudWatch to S3 bucket and have come across two solutions, either use metric streams or build a Lambda function to pull the metric and store in S3. Please correct me if I have misunderstood.
Both the solutions can be used to export the metrics and each method will have its associated cost. Please refer the CloudWatch pricing document [1] that will provide the information related to the costs metric stream and the GetMetricData call will incur. You can also use the AWS price calculator to get an estimated price.
With respect to the methods above, the simpler and easy way will be through metric streams and you can use options to exclude the metrics that are not required [3]. For the solution related to the Lambda, you will have to determine the logic how to pull the metrics and then store them in S3 (Providing the code support is outside the scope of CloudWatch team)[4][5][6].
References:
[1] https://aws.amazon.com/cloudwatch/pricing/
[2] https://calculator.aws/#/addService
[4] https://repost.aws/knowledge-center/cloudwatch-getmetricdata-api
[5] https://repost.aws/knowledge-center/cloudwatch-understand-and-reduce-charges
[6] https://bhargavamin.com/how-to-do/get-cloudwatch-metrics-migrate-s3-bucket/
Lưu ý quan trọng:
Cách sử dụng Athena connector này: Không dùng được với metric cross account! Code tạo bảng cũng không có cột định nghĩa account id:
Trên console, trong phần test, mặc dù mình đã có thể thấy metrics của account khác khi lọc theo account id, nhưng khi query ở Athena như sau:
select * from "lambda:test-cw2".default.metric_samples where "namespace" = 'AWS/EC2' and timestamp > 1699315200
Dữ liệu trả về:
https://github.com/shinchan79/test-data/blob/main/ec2metric711.csv
Không tìm thấy trong kết quả metrics có dimension là instance id trong account liên kết!
Có link này nhưng không đúng expect lắm: https://docs.aws.amazon.com/athena/latest/ug/xacct-fed-query-enable.html
Kết:
Nếu cần lấy data metric cross account thì cách khỏe nhất vẫn là Metrics streams:
Còn nếu không cần cross account thì có thể theo cách athena connector này!