Class: AWS::CloudWatch::Metric
- Inherits:
-
AWS::Core::Resource
- Object
- AWS::Core::Resource
- AWS::CloudWatch::Metric
- Defined in:
- lib/aws/cloud_watch/metric.rb
Overview
Metric
Represents a single metric.
Instance Attribute Summary collapse
- #dimensions ⇒ Array<Hash> readonly
- #metric_name ⇒ String (also: #name) readonly
- #namespace ⇒ String readonly
Instance Method Summary collapse
- #alarms ⇒ MetricAlarmCollection
-
#exists? ⇒ Boolean
Returns
true
if this metric exists. -
#initialize(namespace, metric_name, options = {}) ⇒ Metric
constructor
A new instance of Metric.
-
#put_data(*metric_data) ⇒ nil
Publishes metric data points to Amazon CloudWatch.
-
#statistics(options = {}) ⇒ MetricStatistics
Gets statistics for this metric.
Constructor Details
#initialize(namespace, metric_name, options = {}) ⇒ Metric
Returns a new instance of Metric.
31 32 33 34 35 36 |
# File 'lib/aws/cloud_watch/metric.rb', line 31 def initialize namespace, metric_name, = {} @namespace = namespace @metric_name = metric_name @dimensions = [:dimensions] || [] super end |
Instance Attribute Details
#dimensions ⇒ Array<Hash> (readonly)
47 48 49 |
# File 'lib/aws/cloud_watch/metric.rb', line 47 def dimensions @dimensions end |
#metric_name ⇒ String (readonly) Also known as: name
42 43 44 |
# File 'lib/aws/cloud_watch/metric.rb', line 42 def metric_name @metric_name end |
#namespace ⇒ String (readonly)
39 40 41 |
# File 'lib/aws/cloud_watch/metric.rb', line 39 def namespace @namespace end |
Instance Method Details
#alarms ⇒ MetricAlarmCollection
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.
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.
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? = {} [:namespace] = namespace [:metric_data] = metric_data.flatten.map do |data| data.merge(metric_opts) end client.put_metric_data() 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
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 = {} start = .delete(:start_time) stop = .delete(:end_time) [:namespace] = namespace [:metric_name] = metric_name [:dimensions] = dimensions unless dimensions.empty? [:start_time] = start.respond_to?(:iso8601) ? start.iso8601 : start [:end_time] = stop.respond_to?(:iso8601) ? stop.iso8601 : stop [:period] ||= 60 resp = client.get_metric_statistics() MetricStatistics.new(self, resp[:label], resp[:datapoints]) end |