Class: DynamoAutoscale::Metrics

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dynamo-autoscale/metrics.rb

Constant Summary collapse

DEFAULT_OPTS =
{
  namespace:   'AWS/DynamoDB',
  period:      300,
  # metric_name: metric,
  # start_time:  (NOW - 3600).iso8601,
  # end_time:    NOW.iso8601,
  # dimensions:  [{
  #   name: "TableName", value: TABLE_NAME,
  # }],
}

Class Method Summary collapse

Methods included from Logger

included, logger, #logger, logger=

Class Method Details

.all_metrics(table_name, opts = {}) ⇒ Object

Returns a hash of timeseries data. Looks a bit like this:

{
  provisioned_reads:  { date => value... },
  provisioned_writes: { date => value... },
  consumed_reads:     { date => value... },
  consumed_writes:    { date => value... },
}


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dynamo-autoscale/metrics.rb', line 40

def self.all_metrics table_name, opts = {}
  data = Hash.new { |h, k| h[k] = {} }

  pr = provisioned_reads(table_name, opts).sort_by do |datum|
    datum[:timestamp]
  end

  pr.each do |timeslice|
    data[:provisioned_reads][timeslice[:timestamp]] = timeslice[:average]
  end

  cr = consumed_reads(table_name, opts).sort_by do |datum|
    datum[:timestamp]
  end

  cr.each do |timeslice|
    data[:consumed_reads][timeslice[:timestamp]] = timeslice[:sum]
  end

  pw = provisioned_writes(table_name, opts).sort_by do |datum|
    datum[:timestamp]
  end

  pw.each do |timeslice|
    data[:provisioned_writes][timeslice[:timestamp]] = timeslice[:average]
  end

  cw = consumed_writes(table_name, opts).sort_by do |datum|
    datum[:timestamp]
  end

  cw.each do |timeslice|
    data[:consumed_writes][timeslice[:timestamp]] = timeslice[:sum]
  end

  data
end

.client(region = nil) ⇒ Object

Returns a CloudWatch client object for a given region. If no region exists, the region defaults to whatever is in DynamoAutoscale::DEFAULT_AWS_REGION.

CloudWatch client documentation:

https://github.com/aws/aws-sdk-ruby/blob/master/lib/aws/cloud_watch/client.rb


22
23
24
25
26
27
28
29
30
# File 'lib/dynamo-autoscale/metrics.rb', line 22

def self.client region = nil
  @client ||= Hash.new do |hash, _region|
    hash[_region] = AWS::CloudWatch.new({
      cloud_watch_endpoint: "monitoring.#{_region}.amazonaws.com",
    }).client
  end

  @client[region || DEFAULT_AWS_REGION]
end

.consumed_reads(table_name, opts = {}) ⇒ Object

Returns consumed througput reads for a table in DynamoDB. Works on moving averages.

Example:

pp DynamoAutoscale::Metrics.consumed_reads("table_name")
#=> [{:timestamp=>2013-06-18 15:53:00 UTC,
      :unit=>"Count",
      :average=>1.2111202996546189},
     {:timestamp=>2013-06-18 15:18:00 UTC,
      :unit=>"Count",
      :average=>1.5604973943552964},
      ...
    ]


122
123
124
125
126
# File 'lib/dynamo-autoscale/metrics.rb', line 122

def self.consumed_reads table_name, opts = {}
  opts[:metric_name] = "ConsumedReadCapacityUnits"
  opts[:statistics]  = ["Sum"]
  consumed_metric_statistics(table_name, opts)
end

.consumed_writes(table_name, opts = {}) ⇒ Object

Returns consumed througput writes for a table in DynamoDB. Works on moving averages.

Example:

pp DynamoAutoscale::Metrics.consumed_writes("table_name")
#=> [{:timestamp=>2013-06-18 15:39:00 UTC,
      :unit=>"Count",
      :average=>1.6882725354235755},
     {:timestamp=>2013-06-18 15:24:00 UTC,
      :unit=>"Count",
      :average=>1.7701510393300435},
      ...
    ]


142
143
144
145
146
# File 'lib/dynamo-autoscale/metrics.rb', line 142

def self.consumed_writes table_name, opts = {}
  opts[:metric_name] = "ConsumedWriteCapacityUnits"
  opts[:statistics]  = ["Sum"]
  consumed_metric_statistics(table_name, opts)
end

.provisioned_reads(table_name, opts = {}) ⇒ Object

Returns provisioned througput reads for a table in DynamoDB. Works on moving averages.

Example:

pp DynamoAutoscale::Metrics.provisioned_reads("table_name")
#=> [{:timestamp=>2013-06-18 15:25:00 UTC, :unit=>"Count", :average=>800.0},
     {:timestamp=>2013-06-18 15:05:00 UTC, :unit=>"Count", :average=>800.0},
     ...
    ]


88
89
90
91
# File 'lib/dynamo-autoscale/metrics.rb', line 88

def self.provisioned_reads table_name, opts = {}
  opts[:metric_name] = "ProvisionedReadCapacityUnits"
  provisioned_metric_statistics(table_name, opts)
end

.provisioned_writes(table_name, opts = {}) ⇒ Object

Returns provisioned througput writes for a table in DynamoDB. Works on moving averages.

Example:

pp DynamoAutoscale::Metrics.provisioned_writes("table_name")
#=> [{:timestamp=>2013-06-18 15:25:00 UTC, :unit=>"Count", :average=>600.0},
     {:timestamp=>2013-06-18 15:05:00 UTC, :unit=>"Count", :average=>600.0},
     ...
    ]


103
104
105
106
# File 'lib/dynamo-autoscale/metrics.rb', line 103

def self.provisioned_writes table_name, opts = {}
  opts[:metric_name] = "ProvisionedWriteCapacityUnits"
  provisioned_metric_statistics(table_name, opts)
end