diff --git a/app/lib/scraper/metrics/good_job_metrics_with_queues.rb b/app/lib/scraper/metrics/good_job_metrics_with_queues.rb new file mode 100644 index 00000000..9a3fc69e --- /dev/null +++ b/app/lib/scraper/metrics/good_job_metrics_with_queues.rb @@ -0,0 +1,47 @@ +# typed: strict +class Scraper::Metrics::GoodJobMetricsWithQueues < PrometheusExporter::Instrumentation::PeriodicStats + extend T::Sig + + sig do + params( + client: T.nilable(PrometheusExporter::Client), + frequency: Numeric, + ).void + end + def self.start(client: nil, frequency: 5) + client ||= PrometheusExporter::Client.default + + gauge = + T.cast( + client.register( + :gauge, + "good_job_queue_counts", + "GoodJob queue counts", + ), + PrometheusExporter::Client::RemoteMetric, + ) + + worker_loop do + SCOPES.each do |scope| + GoodJob::Job + .send(scope) + .group(:queue_name, :job_class) + .count + .each do |(queue_name, job_class), count| + gauge.observe( + count, + { queue_name: queue_name, job_class: job_class }, + ) + end + end + end + + super + end + + SCOPES = %i[all scheduled retried queued running finished succeeded discarded] + + sig { params(client: PrometheusExporter::Client).void } + def collect(client) + end +end diff --git a/app/models/redux_application_record.rb b/app/models/redux_application_record.rb index 5bec836e..7f49ee38 100644 --- a/app/models/redux_application_record.rb +++ b/app/models/redux_application_record.rb @@ -25,8 +25,7 @@ class ReduxApplicationRecord < ActiveRecord::Base sig { params(action: Symbol).void } def observe(action) - ACTIVE_RECORD_COUNTER.observe( - 1, + ACTIVE_RECORD_COUNTER.increment( { method: action, class_name: self.class.name }, ) end diff --git a/config/application.rb b/config/application.rb index f3d69937..a5c645e5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -3,6 +3,13 @@ require_relative "boot" require "rails/all" require "sorbet-runtime" +require "prometheus_exporter" +require "prometheus_exporter/client" +require "prometheus_exporter/metric" +require "prometheus_exporter/metric/counter" +require "prometheus_exporter/instrumentation" +require "prometheus_exporter/middleware" + module ReduxScraper class Application < Rails::Application config.active_support.deprecation = :raise @@ -32,5 +39,7 @@ module ReduxScraper # in config/environments, which are processed later. # config.time_zone = "Pacific Time (US & Canada)" + config.x.prometheus_exporter = + Rails.application.config_for("prometheus_exporter") end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 124def24..1f7ba53d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -11,7 +11,7 @@ Rails.application.configure do config.cache_classes = false # Do not eager load code on boot. - config.eager_load = false + config.eager_load = true # Show full error reports. config.consider_all_requests_local = true diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb index ec2090e2..e70239e8 100644 --- a/config/initializers/good_job.rb +++ b/config/initializers/good_job.rb @@ -32,6 +32,8 @@ ActiveSupport.on_load(:good_job_application_controller) do policy.style_src :self, :https, "cdnjs.cloudflare.com" policy.style_src_elem :self, :https, "cdnjs.cloudflare.com" end + + Scraper::Metrics::GoodJobMetricsWithQueues.start end ActiveSupport.on_load(:good_job_base_record) do @@ -44,8 +46,3 @@ ActiveSupport.on_load(:good_job_base_record) do after_create { Scraper::JobBase.last_good_job_execution = self } end end - -require "prometheus_exporter/instrumentation" -ActiveSupport.on_load(:good_job_application_controller) do - PrometheusExporter::Instrumentation::GoodJob.start -end diff --git a/config/initializers/prometheus_exporter.rb b/config/initializers/prometheus_exporter.rb index 3543bf1e..314bbbd0 100644 --- a/config/initializers/prometheus_exporter.rb +++ b/config/initializers/prometheus_exporter.rb @@ -1,12 +1,12 @@ -# typed: true -require "prometheus_exporter" -require "prometheus_exporter/client" -require "prometheus_exporter/metric" -require "prometheus_exporter/metric/counter" - -unless Rails.env.test? - require "prometheus_exporter/middleware" +# typed: strict +if Rails.configuration.x.prometheus_exporter[:enabled] # This reports stats per request like HTTP status and timings Rails.application.middleware.unshift PrometheusExporter::Middleware + PrometheusExporter::Instrumentation::ActiveRecord.start( + custom_labels: { + type: "puma_single_mode", + }, #optional params + config_labels: %i[database host], #optional params + ) end diff --git a/config/prometheus_exporter.yml b/config/prometheus_exporter.yml new file mode 100644 index 00000000..8c56e572 --- /dev/null +++ b/config/prometheus_exporter.yml @@ -0,0 +1,14 @@ +test: + enabled: true + +development: + enabled: true + +staging: + enabled: true + +production: + enabled: true + +worker: + enabled: true diff --git a/config/puma.rb b/config/puma.rb index cc7993db..2e8a307e 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -42,9 +42,3 @@ pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } # Allow puma to be restarted by `bin/rails restart` command. plugin :tmp_restart - -require 'prometheus_exporter/instrumentation' -PrometheusExporter::Instrumentation::ActiveRecord.start( - custom_labels: { type: "puma_single_mode" }, #optional params - config_labels: [:database, :host] #optional params -)