ruby/example_code/cloudwatch/cw-ruby-example-metrics-basics.rb
def datapoint_added_to_metric?(
  cloudwatch_client,
  metric_namespace,
  metric_name,
  dimension_name,
  dimension_value,
  metric_value,
  metric_unit
)
  cloudwatch_client.put_metric_data(
    namespace: metric_namespace,
    metric_data: [
      {
        metric_name: metric_name,
        dimensions: [
          {
            name: dimension_name,
            value: dimension_value
          }
        ],
        value: metric_value,
        unit: metric_unit
      }
    ]
  )
  puts "Added data about '#{metric_name}' to namespace " \
    "'#{metric_namespace}'."
  return true
rescue StandardError => e
  puts "Error adding data about '#{metric_name}' to namespace " \
    "'#{metric_namespace}': #{e.message}"
  return false
end
# Adds a datapoint to a metric in Amazon CloudWatch.
#
# @param cloudwatch_client [Aws::CloudWatch::Client]
#   An initialized CloudWatch client.
# @param metric_namespace [String] The namespace of the metric to add the
#   datapoint to.
# @param metric_name [String] The name of the metric to add the datapoint to.
# @param dimension_name [String] The name of the dimension to add the
#   datapoint to.
# @param dimension_value [String] The value of the dimension to add the
#   datapoint to.
# @param metric_value [Float] The value of the datapoint.
# @param metric_unit [String] The unit of measurement for the datapoint.
# @return [Boolean]
# @example
#   exit 1 unless datapoint_added_to_metric?(
#     Aws::CloudWatch::Client.new(region: 'us-east-1'),
#     'SITE/TRAFFIC',
#     'UniqueVisitors',
#     'SiteName',
#     'example.com',
#     5_885.0,
#     'Count'
#   )
Adds a datapoint to a metric in Amazon CloudWatch.
Adds a datapoint to a metric in Amazon CloudWatch .
def datapoint_added_to_metric?(
  cloudwatch_client,
  metric_namespace,
  metric_name,
  dimension_name,
  dimension_value,
  metric_value,
  metric_unit
)
  cloudwatch_client.put_metric_data(
    namespace: metric_namespace,
    metric_data: [
      {
        metric_name: metric_name,
        dimensions: [
          {
            name: dimension_name,
            value: dimension_value
          }
        ],
        value: metric_value,
        unit: metric_unit
      }
    ]
  )
  puts "Added data about '#{metric_name}' to namespace " \
    "'#{metric_namespace}'."
  return true
rescue StandardError => e
  puts "Error adding data about '#{metric_name}' to namespace " \
    "'#{metric_namespace}': #{e.message}"
  return false
end
def datapoint_added_to_metric? ( cloudwatch_client , metric_namespace , metric_name , dimension_name , dimension_value , metric_value , metric_unit ) cloudwatch_client . put_metric_data ( namespace : metric_namespace , metric_data : [ { metric_name : metric_name , dimensions : [ { name : dimension_name , value : dimension_value } ] , value : metric_value , unit : metric_unit } ] ) puts "Added data about '#{metric_name}' to namespace " "'#{metric_namespace}'." return true rescue StandardError => e puts "Error adding data about '#{metric_name}' to namespace " "'#{metric_namespace}': #{e.message}" return false end
Adds a datapoint to a metric in Amazon CloudWatch.
Adds a datapoint to a metric in Amazon CloudWatch .
None
Boolean
cloudwatch_client
An initialized CloudWatch client.
An initialized CloudWatch client .
metric_namespace
The namespace of the metric to add the datapoint to.
The namespace of the metric to add the datapoint to .
metric_name
The name of the metric to add the datapoint to.
The name of the metric to add the datapoint to .
dimension_name
The name of the dimension to add the datapoint to.
The name of the dimension to add the datapoint to .
dimension_value
The value of the dimension to add the datapoint to.
The value of the dimension to add the datapoint to .
metric_value
The value of the datapoint.
The value of the datapoint .
metric_unit
The unit of measurement for the datapoint.
The unit of measurement for the datapoint .
example
exit 1 unless datapoint_added_to_metric?( Aws::CloudWatch::Client.new(region: 'us-east-1'), 'SITE/TRAFFIC', 'UniqueVisitors', 'SiteName', 'example.com', 5_885.0, 'Count' )
exit 1 unless datapoint_added_to_metric? ( Aws :: CloudWatch :: Client . new ( region : ' us - east - 1 ' ) ' SITE / TRAFFIC ' ' UniqueVisitors ' ' SiteName ' ' example . com ' 5_885 . 0 ' Count ' )