Skip to content

Always use rails mode for Oj

Sean McGivern requested to merge always-use-rails-mode-for-oj into master

Oj's object mode is the default, and serialises symbols as ":foo" (so the result of calling #inspect on the symbol). The rails mode, which is what we want, serialises them as the result of calling #to_s, so just "foo".

We set Oj's default mode to rails in an initialiser. However, this happens after we've already made a call that uses Oj - in active_record_lifecycle.rb, we add a hook to log when the ActiveRecord connection was established.

For Puma, that hook will run after the worker start, which is also after the initialiser. However, running a console or a Rake task (like migrations) will run that hook immediately, so it's before we've set the default options, and uses object mode. Moving the initialiser to those that run before autoloading lets us sidestep this and always use rails mode.

To test this, here's the before and after of me doing bundle exec rails c:

$ tail -f log/application_json.log | grep -F 'ActiveRecord connection established'
{":severity":"DEBUG",":time":"2020-11-05T10:26:11.132Z","correlation_id":null,":message":"ActiveRecord connection established"}
{":severity":"DEBUG",":time":"2020-11-05T10:31:12.865Z","correlation_id":null,":message":"ActiveRecord connection established"}
{":severity":"DEBUG",":time":"2020-11-05T10:38:25.740Z","correlation_id":null,":message":"ActiveRecord connection established"}
{":severity":"DEBUG",":time":"2020-11-05T10:40:01.761Z","correlation_id":null,":message":"ActiveRecord connection established"}
{"severity":"DEBUG","time":"2020-11-05T10:43:15.304Z","correlation_id":null,"message":"ActiveRecord connection established"}

You can see the problem log entries at https://log.gprd.gitlab.net/goto/77eda292dd0dcacee00cf32c957bfe51. These are all from the deploy hosts, which run migrations.

Edited by Sean McGivern

Merge request reports

Loading