Instantly share code, notes, and snippets.

@Envek /sidekiq_prometheus.rb Secret
Last active Oct 19, 2018

Embed
What would you like to do?
Examples of Sidekiq middlewares and exporter to collect and report essential Sidekiq metrics
class SidekiqMetricsServerMiddleware
def call(worker, job, queue)
prometheus = Prometheus::Client.registry
labels = { queue: queue, worker: worker.class }
start = Time.now
begin
yield
prometheus.get(:sidekiq_jobs_success_total).increment(labels)
rescue Exception # rubocop: disable Lint/RescueException
prometheus.get(:sidekiq_jobs_failed_total).increment(labels)
raise
ensure
prometheus.get(:sidekiq_job_runtime_seconds) \
.observe(labels, (Time.now - start).round(3))
prometheus.get(:sidekiq_jobs_executed_total).increment(labels)
end
end
class SidekiqMetricsClientMiddleware
def call(worker, job, queue, _redis_pool)
prometheus = Prometheus::Client.registry
labels = { queue: queue, worker: worker.class }
prometheus.get(:sidekiq_jobs_enqueued_total).increment(labels)
yield
end
end
class SidekiqExporter < Prometheus::Middleware::Exporter
def call(env)
refresh_sidekiq_stats if env["REQUEST_PATH"].start_with?(path)
super
end
def refresh_sidekiq_stats
r = Prometheus.registry
stats = ::Sidekiq::Stats.new
stats.queues.each do |k, v|
r.get(:sidekiq_jobs_waiting_count).set({ queue: k }, v)
end
r.get(:sidekiq_active_workers_count).set({}, stats.workers_size)
r.get(:sidekiq_jobs_scheduled_count).set({}, stats.scheduled_size)
r.get(:sidekiq_jobs_dead_count).set({}, stats.dead_size)
r.get(:sidekiq_active_processes).set({}, stats.processes_size)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment