Commit 88add409 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Fix missing active constant error

When we refer to a constant from initializers which is supposed to be
loaded by `ActiveSupport::Dependencies`, it's possible to get an error
in the development environment if Rails tries to load/reload a missing
constant under the namespace of that constant.

The reason is since the constant is loaded in initializers, it stays in
the memory and conflicts with the one reloaded by Rails.

This commit fixes only the `Gitlab::Metrics::Subscribers::ActiveRecord`
case which has been used in lograge configuration.
parent f751d0f8
# Only use Lograge for Rails # Only use Lograge for Rails
unless Gitlab::Runtime.sidekiq? unless Gitlab::Runtime.sidekiq?
filename = File.join(Rails.root, 'log', "#{Rails.env}_json.log") Rails.application.reloader.to_prepare do
filename = File.join(Rails.root, 'log', "#{Rails.env}_json.log")
db_counter = Gitlab::Metrics::Subscribers::ActiveRecord
Rails.application.configure do Rails.application.configure do
config.lograge.enabled = true config.lograge.enabled = true
# Store the lograge JSON files in a separate file # Store the lograge JSON files in a separate file
config.lograge.keep_original_rails_log = Gitlab::Utils.to_boolean(ENV.fetch('UNSTRUCTURED_RAILS_LOG', 'true')) config.lograge.keep_original_rails_log = Gitlab::Utils.to_boolean(ENV.fetch('UNSTRUCTURED_RAILS_LOG', 'true'))
# Don't use the Logstash formatter since this requires logstash-event, an # Don't use the Logstash formatter since this requires logstash-event, an
# unmaintained gem that monkey patches `Time` # unmaintained gem that monkey patches `Time`
config.lograge.formatter = Lograge::Formatters::Json.new config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.logger = ActiveSupport::Logger.new(filename) config.lograge.logger = ActiveSupport::Logger.new(filename)
config.lograge.before_format = lambda do |data, payload| config.lograge.before_format = lambda do |data, payload|
data.delete(:error) data.delete(:error)
data[:db_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:db)) if data[:db] data[:db_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:db)) if data[:db]
data[:view_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:view)) if data[:view] data[:view_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:view)) if data[:view]
data[:duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:duration)) if data[:duration] data[:duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:duration)) if data[:duration]
data.merge!(::Gitlab::Metrics::Subscribers::ActiveRecord.db_counter_payload) data.merge!(db_counter.db_counter_payload)
# Remove empty hashes to prevent type mismatches # Remove empty hashes to prevent type mismatches
# These are set to empty hashes in Lograge's ActionCable subscriber # These are set to empty hashes in Lograge's ActionCable subscriber
# https://github.com/roidrage/lograge/blob/v0.11.2/lib/lograge/log_subscribers/action_cable.rb#L14-L16 # https://github.com/roidrage/lograge/blob/v0.11.2/lib/lograge/log_subscribers/action_cable.rb#L14-L16
%i(method path format).each do |key| %i(method path format).each do |key|
data[key] = nil if data[key] == {} data[key] = nil if data[key] == {}
end end
data data
end end
# This isn't a user-reachable controller; we use it to check for a # This isn't a user-reachable controller; we use it to check for a
# valid CSRF token in the API # valid CSRF token in the API
config.lograge.ignore_actions = ['Gitlab::RequestForgeryProtection::Controller#index'] config.lograge.ignore_actions = ['Gitlab::RequestForgeryProtection::Controller#index']
# Add request parameters to log output # Add request parameters to log output
config.lograge.custom_options = Gitlab::Lograge::CustomOptions config.lograge.custom_options = Gitlab::Lograge::CustomOptions
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment