class Resque::CloudWatch::Metrics

Constants

DEFAULT_CW_NAMESPACE
MAX_METRIC_DATA_PER_PUT

docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html

VERSION

Public Class Methods

new(redis: nil, redis_namespace: nil, cw_namespace: DEFAULT_CW_NAMESPACE, interval: nil, metric: {}, dryrun: false) click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 61
def initialize(redis: nil,
               redis_namespace: nil,
               cw_namespace: DEFAULT_CW_NAMESPACE,
               interval: nil,
               metric: {},
               dryrun: false)
  Resque.redis = redis if redis
  @redis_namespace = redis_namespace
  @interval = interval
  @cw_namespace = cw_namespace
  @metric_options = metric
  @dryrun = dryrun
end
parse_arguments(args) click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 22
def parse_arguments(args)
  options = {}
  redis = {}
  skip = []
  extra = []

  opt = OptionParser.new
  opt.on('-h', '--host <host>')           { |v| redis[:host] = v }
  opt.on('-p', '--port <port>')           { |v| redis[:port] = v }
  opt.on('-s', '--socket <socket>')       { |v| redis[:path] = v }
  opt.on('-a', '--password <password>')   { |v| redis[:password] = v }
  opt.on('-n', '--db <db>')               { |v| redis[:db] = v }
  opt.on('--url <url>')                   { |v| options[:redis] = v }
  opt.on('--redis-namespace <namespace>') { |v| options[:redis_namespace] = v }
  opt.on('--cw-namespace <namespace>')    { |v| options[:cw_namespace] = v }
  opt.on('-i', '--interval <interval>')   { |v| options[:interval] = v.to_f }
  opt.on('--skip-pending')                { skip << :pending }
  opt.on('--skip-processed')              { skip << :processed }
  opt.on('--skip-failed')                 { skip << :failed }
  opt.on('--skip-queues')                 { skip << :queues }
  opt.on('--skip-workers')                { skip << :workers }
  opt.on('--skip-working')                { skip << :working }
  opt.on('--skip-pending-per-queue')      { skip << :pending_per_queue }
  opt.on('--not-working')                 { extra << :not_working }
  opt.on('--processing')                  { extra << :processing }
  opt.on('--dryrun')                      { options[:dryrun] = true }
  opt.parse(args)

  options[:redis] ||= redis unless redis.empty?

  metric = {}
  metric[:skip] = skip unless skip.empty?
  metric[:extra] = extra unless extra.empty?
  options[:metric] = metric unless metric.empty?

  options
end
run(args) click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 18
def run(args)
  new(parse_arguments(args)).run
end

Public Instance Methods

run() click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 75
def run
  if @interval
    loop do
      thread = Thread.start { run_once }
      thread.abort_on_exception = true
      sleep @interval
    end
  else
    run_once
  end
end

Private Instance Methods

cloudwatch() click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 137
def cloudwatch
  @_cloudwatch ||= Aws::CloudWatch::Client.new
end
dump_metric_data(metric_data) click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 128
def dump_metric_data(metric_data)
  puts metric_data.to_json(
    indent:    ' ' * 2,
    space:     ' ',
    object_nl: "\n",
    array_nl:  "\n",
  )
end
put_metric_data(metric_data) click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 112
def put_metric_data(metric_data)
  if @dryrun
    dump_metric_data(metric_data)
    return
  end

  metric_data.each_slice(MAX_METRIC_DATA_PER_PUT).map do |data|
    Thread.start(data, cloudwatch) do |data_, cloudwatch_|
      cloudwatch_.put_metric_data(
        namespace: @cw_namespace,
        metric_data: data_,
      )
    end
  end.each(&:join)
end
redis_namespaces() click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 97
def redis_namespaces
  if @redis_namespace
    if @redis_namespace.include?('*')
      suffix = ':queues'
      Resque.redis.redis.keys(@redis_namespace + suffix).map do |key|
        key[0 ... - suffix.length].to_sym
      end
    else
      [@redis_namespace.to_sym]
    end
  else
    [Resque.redis.namespace]
  end
end
run_once() click to toggle source
# File lib/resque/cloudwatch/metrics.rb, line 89
def run_once
  put_metric_data(
    redis_namespaces
      .map { |redis_namespace| Metric.create(redis_namespace, @metric_options) }
      .flat_map(&:to_cloudwatch_metric_data)
  )
end