1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/component.rb
Mike Perham f220897db9
Per-job execution metrics (#5384)
* New execution stats data format, #5283

We store time and counts per-queue, per-class and totals, daily.

* break metrics into separate jobs and queues hashes

* tweak

* Move metrics tracking to middleware, start work on Query API

* Add support for labeled points in time

* Add fetch method for deploy marks

* rejigger metrics file layout

* fix tests

* Remove per-queue metrics, adds a lot of complexity with little value IMO

* store per-minute histograms

* Keep basic stats hardwired as is

The idea being that metrics are optional middleware as they have significantly more overhead in CPU time and Redis space.

* Implement top N metrics dashboard

* Add topN and job-specific metric pages

* Supply histogram data to job metrics page

* cant use local time as CI is in a different tz

* Add basic metrics graph, refactor dashboard JS to make Rickshaw reuseable

* prepare for public beta
2022-07-28 12:21:42 -07:00

65 lines
1.5 KiB
Ruby

module Sidekiq
##
# Sidekiq::Component assumes a config instance is available at @config
module Component # :nodoc:
attr_reader :config
def watchdog(last_words)
yield
rescue Exception => ex
handle_exception(ex, {context: last_words})
raise ex
end
def safe_thread(name, &block)
Thread.new do
Thread.current.name = name
watchdog(name, &block)
end
end
def logger
config.logger
end
def redis(&block)
config.redis(&block)
end
def tid
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end
def hostname
ENV["DYNO"] || Socket.gethostname
end
def process_nonce
@@process_nonce ||= SecureRandom.hex(6)
end
def identity
@@identity ||= "#{hostname}:#{::Process.pid}:#{process_nonce}"
end
def handle_exception(ex, ctx = {})
config.handle_exception(ex, ctx)
end
def fire_event(event, options = {})
oneshot = options.fetch(:oneshot, true)
reverse = options[:reverse]
reraise = options[:reraise]
arr = config[:lifecycle_events][event]
arr.reverse! if reverse
arr.each do |block|
block.call
rescue => ex
handle_exception(ex, {context: "Exception during Sidekiq lifecycle event.", event: event})
raise ex if reraise
end
arr.clear if oneshot # once we've fired an event, we never fire it again
end
end
end