AWS SDK for Ruby
Developer Guide

Getting Information about Custom Metrics for Amazon CloudWatch

A CloudWatch alarm watches a single metric over a time period you specify. The CloudWatch alarm performs one or more actions based on the value of the metric, relative to a given threshold over a number of time periods. For more information, see Creating Amazon CloudWatch Alarms.

In this example, you use the AWS SDK for Ruby with CloudWatch to:

  1. Send custom metrics to CloudWatch by using Aws::CloudWatch::Client#put_metric_data.

  2. Get information about custom metrics by using Aws::CloudWatch::Client#list_metrics-instance.

Prerequisites

Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:

Example

require 'aws-sdk-cloudwatch' # v2: require 'aws-sdk' # Uncomment for Windows. # Aws.use_bundled_cert! cw = Aws::CloudWatch::Client.new(region: 'us-east-1') # Send custom metrics to Amazon CloudWatch. # In this example, add metrics to the custom namespace "SITE/TRAFFIC": # For the custom dimension named "SiteName", for the value named "example.com", add # "UniqueVisitors" of 5885 and "UniqueVisits" of 8628. # For the custom dimension named "PageURL", for the value named "my-page.html", add # "PageViews" of 18057. cw.put_metric_data({ namespace: "SITE/TRAFFIC", metric_data: [ { metric_name: "UniqueVisitors", dimensions: [ { name: "SiteName", value: "example.com" } ], value: 5885.0, unit: "Count" }, { metric_name: "UniqueVisits", dimensions: [ { name: "SiteName", value: "example.com" } ], value: 8628.0, unit: "Count" }, { metric_name: "PageViews", dimensions: [ { name: "PageURL", value: "my-page.html" } ], value: 18057.0, unit: "Count" } ] }) # Get information about custom metrics. list_metrics_output = cw.list_metrics({ namespace: "SITE/TRAFFIC" }) list_metrics_output.metrics.each do |metric| puts metric.metric_name metric.dimensions.each do |dimension| puts "#{dimension.name} = #{dimension.value}" end puts "\n" end

On this page: