Last active
August 12, 2024 16:39
-
-
Save jwicks/46abb7feef33f13d3de9 to your computer and use it in GitHub Desktop.
Sidekiq worker for publishing queue size metric to AWS CloudWatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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