Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitor worker job runtime #13

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ end
- Number of jobs in dead set (“morgue”): `sidekiq_jobs_dead_count`
- Active workers count: `sidekiq_active_processes`
- Active processes count: `sidekiq_active_workers_count`
- Runtime of the actual running worker jobs `sidekiq_worker_runtime`

## Custom tags

Expand Down
22 changes: 22 additions & 0 deletions lib/yabeda/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Sidekiq
gauge :jobs_dead_count, tags: [], comment: "The number of jobs exceeded their retry count."
gauge :active_processes, tags: [], comment: "The number of active Sidekiq worker processes."
gauge :queue_latency, tags: %i[queue], comment: "The queue latency, the difference in seconds since the oldest job in the queue was enqueued"
gauge :job_max_runtime, tags: %i[queue worker], comment: "The actual job runtime"

histogram :job_latency, comment: "The job latency, the difference in seconds between enqueued and running time",
unit: :seconds, per: :job,
Expand All @@ -59,6 +60,7 @@ module Sidekiq
sidekiq_queue_latency.set({ queue: queue.name }, queue.latency)
end

track_job_max_runtime
Envek marked this conversation as resolved.
Show resolved Hide resolved
# That is quite slow if your retry set is large
# I don't want to enable it by default
# retries_by_queues =
Expand Down Expand Up @@ -87,6 +89,8 @@ module Sidekiq
end

class << self
att_accessor :job_tags
Envek marked this conversation as resolved.
Show resolved Hide resolved

def labelize(worker, job, queue)
{ queue: queue, worker: worker_class(worker, job), **custom_tags(worker, job).to_h }
end
Expand All @@ -104,5 +108,23 @@ def custom_tags(worker, job)
worker.method(:yabeda_tags).arity.zero? ? worker.yabeda_tags : worker.yabeda_tags(*job["args"])
end
end

self.job_tags = []
Envek marked this conversation as resolved.
Show resolved Hide resolved

def track_job_max_runtime
now = Time.now.utc
workers_runtime = ::Sidekiq::Workers.new.each_with_object do |result, (process, thread, msg)|
Envek marked this conversation as resolved.
Show resolved Hide resolved
payload = msg['payload']
Envek marked this conversation as resolved.
Show resolved Hide resolved
tags = {queue: payload['queue'], worker: payload['class']}
job_tags << tags unless job_tags.include?(tags)

duration = now - Time.at(msg['run_at'])
result[tags] = duration if !result[tags] || result[tags] < duration
end

job_tags.each_with_object({}).each do |result, tags|
job_max_runtime.set(tags, workers_runtime[tags].to_i)
Envek marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end