Add GoodJob metrics integration and update Prometheus configuration
- Introduced a new `GoodJobMetricsWithQueues` class for tracking GoodJob queue counts using Prometheus. - Updated `application.rb` to include Prometheus exporter dependencies and configuration. - Added a new `prometheus_exporter.yml` file to manage Prometheus settings across environments. - Modified `puma.rb` to remove redundant Prometheus instrumentation code. - Enabled eager loading in the development environment for better performance. - Updated GoodJob initializer to start the new metrics tracking. These changes enhance monitoring capabilities for job processing and improve application performance.
This commit is contained in:
47
app/lib/scraper/metrics/good_job_metrics_with_queues.rb
Normal file
47
app/lib/scraper/metrics/good_job_metrics_with_queues.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
14
config/prometheus_exporter.yml
Normal file
14
config/prometheus_exporter.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
test:
|
||||
enabled: true
|
||||
|
||||
development:
|
||||
enabled: true
|
||||
|
||||
staging:
|
||||
enabled: true
|
||||
|
||||
production:
|
||||
enabled: true
|
||||
|
||||
worker:
|
||||
enabled: true
|
||||
@@ -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
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user