Skip to content

Instantly share code, notes, and snippets.

@jwicks
Last active November 21, 2019 07:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwicks/46abb7feef33f13d3de9 to your computer and use it in GitHub Desktop.
Save jwicks/46abb7feef33f13d3de9 to your computer and use it in GitHub Desktop.
Sidekiq worker for publishing queue size metric to AWS CloudWatch
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',
dimensions: [{
name: 'Environment',
value: Rails.env
}],
value: queue_size,
unit: 'Count'
}]
)
end
def queue_size
stats = Sidekiq::Stats.new
stats.queues.values.inject 0, :+
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment