我應該對 CloudWatch 指標使用 GetMetricData 或 GetMetricStatistics?

3 分的閱讀內容
0

我想要從 Amazon CloudWatch 指標中擷取資料點。我應該使用哪個 API,GetMetricData 或是 GetMetricStatistics?

簡短描述

最佳實務是使用 GetMetricData API,而不是使用GetMetricStatistics,因為您可以使用 GetMetricData 更快大規模地擷取資料。GetMetricData 也支援指標數學,並傳回排序的分頁結果。這種功能差異會反映於每項服務的成本。GetMetricStatistics 包含在 CloudWatch 的免費方案中,最多 1 百萬個 API 請求,而 GetMetricData 則不包含在該方案中。請參閱下面的圖表和 CloudWatch 定價圖表,以查看哪種 API 更適合您。

每次呼叫的指標每次呼叫的資料點支援指標數學傳回排序和分頁結果包含免費方案****限制
GetMetricData500100,800是*
GetMetricStatistics11,440

*若要傳回下一組資料點,您必須使用回應中提供的 NextToken (--next-token) 反覆運算資料。

GetMetricData API 的 Service Quotas 如下:

  • 50 筆交易/秒 (TPS)。
  • 180,000 個資料點/秒 (DPS) (如果 API 請求中的 StartTime 距目前時間小於或等於三小時)。
  • 396,000 DPS (如果 StartTime 距目前時間超過三小時)。

解決方案

**注意事項:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請確認您使用的是最新的 AWS CLI 版本

使用下列範例呼叫作為在 AWS CLI 中進行自己的 GetMetricData API 呼叫的參考。

1.    為您的 GetMetricData API 呼叫 (metric-data-queries.json) 建立輸入參數。輸入參數具有兩個自訂指標 (調用錯誤) 和一個由其他兩個指標的指標數學 (ErrorRate) 計算而成的指標 (ErrorRate)。

$ cat metric-data-queries.json
[
    {
        "Id": "e1",
        "Expression": "m1 / m2",
        "Label": "ErrorRate"
    },
    {
        "Id": "m1",
        "MetricStat": {
            "Metric": {
                "Namespace": "MyApplication",
                "MetricName": "Errors",
                "Dimensions": [
                    {
                        "Name": "FunctionName",
                        "Value": "MyFunc"
                    }
                ]
            },
            "Period": 300,
            "Stat": "Sum",
            "Unit": "Count"
        },
        "ReturnData": false
    },
    {
        "Id": "m2",
        "MetricStat": {
            "Metric": {
                "Namespace": "MyApplication",
                "MetricName": "Invocations",
                "Dimensions": [
                    {
                        "Name": "FunctionName",
                        "Value": "MyFunc"
                    }
                ]
            },
            "Period": 300,
            "Stat": "Sum",
            "Unit": "Count"
        },
        "ReturnData": false
    }
]

2.    使用 PutMetricData 將範例指標資料發佈為自訂指標。例如:

$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:00:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:05:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:10:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:15:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:20:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 3 --unit Count --timestamp 2018-06-19T04:00:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 6 --unit Count --timestamp 2018-06-19T04:05:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 2 --unit Count --timestamp 2018-06-19T04:10:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 9 --unit Count --timestamp 2018-06-19T04:15:00Z
$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 1 --unit Count --timestamp 2018-06-19T04:20:00Z

**注意事項:**您也可以使用相同命名空間下的單一 PutMetricData API 呼叫發佈最多 20 項指標。若要這麼做,請在 PutMetricData 呼叫中使用 --metric-data 選項。

3.    使用您的輸入參數執行命令 aws cloudwatch get-metric-data

4.    檢閱輸出。在此範例中,使用指標數學計算五個資料點,並傳回按時間排序的結果。m1m2 不包括在回應中,因為 ReturnData 設定為 False。

$ aws cloudwatch get-metric-data --metric-data-queries file://./metric-data-queries.json --start-time 2018-06-19T04:00:00Z --end-time 2018-06-19T04:30:00Z
{
    "MetricDataResults": [
        {
            "Timestamps": [
                "2018-06-19T04:20:00Z",
                "2018-06-19T04:15:00Z",
                "2018-06-19T04:10:00Z",
                "2018-06-19T04:05:00Z",
                "2018-06-19T04:00:00Z"
            ],
            "StatusCode": "Complete",
            "Values": [
                0.1,
                0.9,
                0.2,
                0.6,
                0.3
            ],
            "Id": "e1",
            "Label": "ErrorRate"
        }
    ]
}

相關資訊

get-metric-data AWS CLI 參考

get-metric-statistics AWS CLI 參考

AWS 官方
AWS 官方已更新 2 年前