Class: AWS::CloudWatch::Metric

Inherits:
AWS::Core::Resource
  • Object
show all
Defined in:
lib/aws/cloud_watch/metric.rb

Overview

Metric

Represents a single metric.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, metric_name, options = {}) ⇒ Metric

Returns a new instance of Metric.

Parameters:

  • namespace (String)

    The metric namespace.

  • metric_name (String)

    The metric name.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :dimensions (Array<Hash>)

    An array of dimensions.

    Each hash must have a :name and a value key (with string values).



31
32
33
34
35
36
# File 'lib/aws/cloud_watch/metric.rb', line 31

def initialize namespace, metric_name, options = {}
  @namespace = namespace
  @metric_name = metric_name
  @dimensions = options[:dimensions] || []
  super
end

Instance Attribute Details

#dimensionsArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


47
48
49
# File 'lib/aws/cloud_watch/metric.rb', line 47

def dimensions
  @dimensions
end

#metric_nameString (readonly) Also known as: name

Returns:

  • (String)


42
43
44
# File 'lib/aws/cloud_watch/metric.rb', line 42

def metric_name
  @metric_name
end

#namespaceString (readonly)

Returns:

  • (String)


39
40
41
# File 'lib/aws/cloud_watch/metric.rb', line 39

def namespace
  @namespace
end

Instance Method Details

#alarmsMetricAlarmCollection



50
51
52
# File 'lib/aws/cloud_watch/metric.rb', line 50

def alarms
  MetricAlarmCollection.new(self, :config => config)
end

#exists?Boolean

Returns true if this metric exists.

Returns:

  • (Boolean)

    Returns true if this metric exists.



115
116
117
# File 'lib/aws/cloud_watch/metric.rb', line 115

def exists?
  !get_resource.data[:metrics].empty?
end

#put_data(*metric_data) ⇒ nil

Publishes metric data points to Amazon CloudWatch.

Parameters:

  • metric_data (Array<Hash>)

    An array of hashes. Each hash must pass :value (number) or :statistic_values (hash).

Returns:

  • (nil)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aws/cloud_watch/metric.rb', line 58

def put_data *metric_data
  
  metric_opts = {}
  metric_opts[:metric_name] = metric_name
  metric_opts[:dimensions] = dimensions unless dimensions.empty?

  options = {}
  options[:namespace] = namespace
  options[:metric_data] = metric_data.flatten.map do |data|
    data.merge(metric_opts)
  end

  client.put_metric_data(options)
  nil

end

#statistics(options = {}) ⇒ MetricStatistics

Gets statistics for this metric.

metric = CloudWatch::Metric.new('my/namepace', 'metric-name')

stats = metric.statistics(
  :start_time => Time.now - 3600,
  :end_time => Time.now,
  :statistics => ['Average'])

stats.label #=> 'some-label'
stats.each do |datapoint|
  # datapoint is a hash
end

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :start_time (Time, required)
  • :end_time (Time, required)
  • :statistics (Array<String>, required)
  • :unit (String)
  • :period (Integer) — default: 60

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aws/cloud_watch/metric.rb', line 96

def statistics options = {}

  start = options.delete(:start_time)
  stop = options.delete(:end_time)

  options[:namespace] = namespace
  options[:metric_name] = metric_name
  options[:dimensions] = dimensions unless dimensions.empty?
  options[:start_time] = start.respond_to?(:iso8601) ? start.iso8601 : start
  options[:end_time] = stop.respond_to?(:iso8601) ? stop.iso8601 : stop
  options[:period] ||= 60

  resp = client.get_metric_statistics(options)

  MetricStatistics.new(self, resp[:label], resp[:datapoints])

end