diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb index dae673a6..257b8b7e 100644 --- a/app/controllers/metrics_controller.rb +++ b/app/controllers/metrics_controller.rb @@ -6,12 +6,34 @@ class MetricsController < ActionController::API include ActionController::MimeResponds def show + fetch_sidekiq_metrics + render plain: metrics end 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 - Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry) + Prometheus::Client::Formats::Text.marshal(Retrospring::Metrics::PROMETHEUS) end end diff --git a/lib/retrospring/metrics.rb b/lib/retrospring/metrics.rb index 7259f33f..7c52dcb6 100644 --- a/lib/retrospring/metrics.rb +++ b/lib/retrospring/metrics.rb @@ -42,5 +42,38 @@ module Retrospring :retrospring_comments_created_total, 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 diff --git a/spec/controllers/metrics_controller_spec.rb b/spec/controllers/metrics_controller_spec.rb index c278e308..89f4316e 100644 --- a/spec/controllers/metrics_controller_spec.rb +++ b/spec/controllers/metrics_controller_spec.rb @@ -7,9 +7,6 @@ describe MetricsController, type: :controller do subject { get :show } 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" end end