Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created February 12, 2019 12:54
Show Gist options
  • Save arthurnn/d99b3d90802c5ece2414f642abfa45e0 to your computer and use it in GitHub Desktop.
Save arthurnn/d99b3d90802c5ece2414f642abfa45e0 to your computer and use it in GitHub Desktop.
Base instrumentation for ActiveJob + Sidekiq
def stats
# TODO
end
def dogtags
# TODO
end
ActiveSupport::Notifications.subscribe("enqueue.active_job") do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
job = event.payload.fetch(:job)
job_name = job.class.name.underscore
queue_name = job.queue_name
depth = Sidekiq::Queue.new(queue_name)
stats.increment("job.enqueued", {
tags: dogtags(job: job_name, queue: queue_name),
})
stats.gauge("job.depth", depth, {
tags: dogtags(queue: queue_name),
})
end
ActiveSupport::Notifications.subscribe("enqueue_at.active_job") do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
job = event.payload.fetch(:job)
job_name = job.class.name.underscore
queue_name = job.queue_name
depth = Sidekiq::Queue.new(queue_name)
stats.increment("job.scheduled", {
tags: dogtags(job: job_name, queue: queue_name),
})
stats.gauge("job.depth", depth, {
tags: dogtags(queue: queue_name),
})
end
ActiveSupport::Notifications.subscribe("perform.active_job") do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
job = event.payload.fetch(:job)
job_name = job.class.name.underscore
queue_name = job.queue_name
depth = Sidekiq::Queue.new(queue_name)
stats.timing("job.time", event.duration, {
tags: dogtags(job: job_name, queue: queue_name),
})
stats.gauge("job.depth", depth, {
tags: dogtags(queue: queue_name),
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment