add metrics for sidekiq

This commit is contained in:
Georg Gadinger 2023-02-14 05:30:40 +01:00
parent b937a10096
commit 8a055341c8
3 changed files with 56 additions and 4 deletions

View File

@ -6,12 +6,34 @@ class MetricsController < ActionController::API
include ActionController::MimeResponds include ActionController::MimeResponds
def show def show
fetch_sidekiq_metrics
render plain: metrics render plain: metrics
end end
private private
SIDEKIQ_STATS_METHODS = %i[
processed
failed
scheduled_size
retry_size
dead_size
processes_size
].freeze
def fetch_sidekiq_metrics
stats = Sidekiq::Stats.new
SIDEKIQ_STATS_METHODS.each do |key|
Retrospring::Metrics::SIDEKIQ[key].set stats.public_send(key)
end
stats.queues.each do |queue, value|
Retrospring::Metrics::SIDEKIQ[:queue_enqueued].set value, labels: { queue: }
end
end
def metrics def metrics
Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry) Prometheus::Client::Formats::Text.marshal(Retrospring::Metrics::PROMETHEUS)
end end
end end

View File

@ -42,5 +42,38 @@ module Retrospring
:retrospring_comments_created_total, :retrospring_comments_created_total,
docstring: "How many comments got created" docstring: "How many comments got created"
) )
# metrics from Sidekiq::Stats.new
SIDEKIQ = {
processed: gauge(
:sidekiq_processed,
docstring: "Number of jobs processed by Sidekiq"
),
failed: gauge(
:sidekiq_failed,
docstring: "Number of jobs that failed"
),
scheduled_size: gauge(
:sidekiq_scheduled_jobs,
docstring: "Number of jobs that are enqueued"
),
retry_size: gauge(
:sidekiq_retried_jobs,
docstring: "Number of jobs that are being retried"
),
dead_size: gauge(
:sidekiq_dead_jobs,
docstring: "Number of jobs that are dead"
),
processes_size: gauge(
:sidekiq_processes,
docstring: "Number of active Sidekiq processes"
),
queue_enqueued: gauge(
:sidekiq_queues_enqueued,
docstring: "Number of enqueued jobs per queue",
labels: %i[queue]
),
}.freeze
end end
end end

View File

@ -7,9 +7,6 @@ describe MetricsController, type: :controller do
subject { get :show } subject { get :show }
it "returns the metrics" do it "returns the metrics" do
# ensure we have at least a metric set
Retrospring::Metrics::VERSION_INFO.set 1
expect(subject.body).to include "retrospring_version_info" expect(subject.body).to include "retrospring_version_info"
end end
end end