Skip to content

Instantly share code, notes, and snippets.

@jwicks
Last active August 12, 2024 16:39

Revisions

  1. jwicks revised this gist Mar 31, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion sidekiq-worker-cloudwatch-queue-size.rb
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,12 @@ def perform
    namespace: 'Worker',
    metric_data: [{
    metric_name: 'QueueSize',
    value: queue_size
    dimensions: [{
    name: 'Environment',
    value: Rails.env
    }],
    value: queue_size,
    unit: 'Count'
    }]
    )
    end
  2. jwicks renamed this gist Mar 31, 2015. 1 changed file with 0 additions and 0 deletions.
  3. jwicks created this gist Mar 31, 2015.
    27 changes: 27 additions & 0 deletions sidekiq-worker-cloudwatch-queue-size
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    class QueueSizeMetricWorker
    include Sidekiq::Worker
    include Sidetiq::Schedulable

    recurrence { minutely }
    sidekiq_options retry: false

    # Publish a custom metric on CloudWatch with the Sidekiq queue size
    def perform
    cloudwatch = Aws::CloudWatch::Client.new(
    region: 'us-east-1',
    credentials: Aws::Credentials.new(ENV['AWS_KEY'], ENV['AWS_SECRET'])
    )
    cloudwatch.put_metric_data(
    namespace: 'Worker',
    metric_data: [{
    metric_name: 'QueueSize',
    value: queue_size
    }]
    )
    end

    def queue_size
    stats = Sidekiq::Stats.new
    stats.queues.values.inject 0, :+
    end
    end