Skip to content

Log Ruby and Rails deprecations

Matthias Käppler requested to merge 333330-log-deprecation-warnings into master

What does this MR do?

We are working on Ruby 3 compatibility, and a common source of problems has been deprecations that were warnings in Ruby 2.7 and have turned into errors in Ruby 3.

We already detect some of these in CI, but for anything that is not covered by tests, we also want to make sure we catch it in production before switching to the new VM.

To that end this MR introduces a new logger, Gitlab::DeprecationJsonLogger, which logs to a new file log/deprecation_json.log. The logger uses a two-pronged approach:

  • For Ruby(-gem)s deprecation warnings, it utilizes the ruby-warning gem to log deprecation warnings emitted through Kernel#warn to our logger. This will still output them to stderr as well.
  • For Rails deprecation warnings, it hooks into Rails' deprecation.rails warning via an ActiveSupport::Notifications subscriber.

This is done in a new initializer that runs early on in the initializer chain (though there might be code that executes in front of it, that should be OK, since we're mostly looking for unknown issues elsewhere in the code base here.)

NOTE that this behavior is enabled only in development by default. Other environments needs to opt-in via the GITLAB_LOG_DEPRECATIONS env var. This seemed like the safest approach to me. I tried using FFs but failed, since some rake tasks that were emitting warnings this way were crashing due to the database not being up.

Sample output:

{
  "severity": "INFO",
  "time": "2021-07-26T11:41:35.056Z",
  "correlation_id": "cec7a2ef0e3bd1ace12433c505a660b5",
  "message": "DEPRECATION WARNING: ABC will be removed (called from block (3 levels) in <main> at /home/git/gitlab/spec/initializers/0_log_deprecations_spec.rb:31)"
  "source": "rails"
}

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Related to #333330 (closed)

Edited by Matthias Käppler

Merge request reports