Skip to content

Commit

Permalink
Add config option to suppress automatically starting the reporter (#185)
Browse files Browse the repository at this point in the history
* Add config option to suppress automatically starting the reporter

This can avoid issues in some applications where additional setup is needed before safely starting the reporter. Specifically, we had a customer who was configuring their Sidekiq Redis connection in `after_initialize`, and our reporter was starting (and attempting to fetch Sidekiq metrics from Redis) before Sidekiq was fully configured.

* Create config_test.rb
  • Loading branch information
adamlogic authored Sep 26, 2023
1 parent c62e3c8 commit c194b93
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
16 changes: 16 additions & 0 deletions judoscale-rails/lib/judoscale/rails/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "judoscale/config"

module Judoscale
module Rails
module Config
attr_accessor :start_reporter_after_initialize

def reset
super
@start_reporter_after_initialize = true
end
end

::Judoscale::Config.prepend Config
end
end
6 changes: 3 additions & 3 deletions judoscale-rails/lib/judoscale/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails"
require "rails/railtie"
require "judoscale/request_middleware"
require "judoscale/config"
require "judoscale/rails/config"
require "judoscale/logger"
require "judoscale/reporter"

Expand All @@ -21,7 +21,7 @@ def in_rake_task?
end

initializer "Judoscale.logger" do |app|
Config.instance.logger = ::Rails.logger
::Judoscale::Config.instance.logger = ::Rails.logger
end

initializer "Judoscale.request_middleware" do |app|
Expand All @@ -33,7 +33,7 @@ def in_rake_task?

config.after_initialize do
# Don't suppress this in Rake tasks since some job adapters use Rake tasks to run jobs.
Reporter.start unless in_rails_console?
Reporter.start if !in_rails_console? && ::Judoscale::Config.instance.start_reporter_after_initialize
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions judoscale-rails/test/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "test_helper"
require "judoscale/rails/config"

module Judoscale
describe Judoscale::Rails::Config do
it "adds the start_reporter_after_initialize config option" do
_(::Judoscale::Config.instance.start_reporter_after_initialize).must_equal true
end
end
end
1 change: 1 addition & 0 deletions sample-apps/rails-sample/config/initializers/judoscale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Judoscale.configure do |config|
# Open https://requestinspector.com/p/judoscale-ruby in a browser to monitor requests
config.api_base_url = ENV["JUDOSCALE_URL"] || "https://requestinspector.com/inspect/judoscale-ruby"
# config.start_reporter_after_initialize = false
end

0 comments on commit c194b93

Please sign in to comment.