Skip to content

Is it better to use GetMetricData or GetMetricStatistics for CloudWatch metrics?

4 minute read
2

I want to know if it's better to use the GetMetricData or GetMetricStatistics API operation to retrieve data points from Amazon CloudWatch metrics.

Short description

Because GetMetricData retrieves data faster at scale, it's a best practice to use the GetMetricData API operation instead of GetMetricStatistics. GetMetricData supports metric math and returns ordered, paginated results. 

The following are the service quotas for the GetMetricData API operation:

  • 50 transactions per second (TPS).
  • 180,000 data points per second (DPS) when the StartTime in the API request is less than or equal to 3 hours from the current time.
  • 396,000 DPS when the StartTime is more than 3 hours from the current time.

Note: Pricing for GetMetricData is different from GetMetricStatistics pricing. For more information, select the Metrics tab in the Paid tier section of the Amazon CloudWatch pricing page.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To use the GetMetricData API operation to get data points, complete the following steps in the AWS CLI:

  1. Create an input parameter for your GetMetricData API operation.
    Example operation:

     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
        }
    ]

    Note: Replace MyApplication with your application name.
    In the preceding operation, the input parameter has Invocations and Errors custom metrics. CloudWatch calculates the ErrorRate metric by the metric math of the other two metrics.

  2. Use PutMetricData to publish sample metric data as custom metrics.
    Example operation:

    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

    Note: To publish up to 20 metrics, add the --metric-data option to one PutMetricData operation under the same namespace, and then run the API.

  3. Run the following get-metric-data AWS CLI command:

    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

    Note: Replace //./metric-data-queries.json with your JSON file, 2018-06-19T04:00:00Z with your start time, and 2018-06-19T04:30:00Z with your end time.
    Example output:

    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"
            }
        ]
    }

    Note: In the preceding example output, CloudWatch calculates five data points with metric math and returns them as a time-ordered result. Because ReturnData is set to false, m1 and m2 aren't included in the response.