ruby-on-railsrubystackdrivergoogle-cloud-logging

How to make Google Stackdriver works with Rails 7.1+


I just spent a few hours looking over the internet to find out how to resolve that issue. But, for the life of me, I have no idea what to do...

I followed the documentation provided by Google github.com/googleapis/google-cloud-ruby.

# Gemfile
gem 'stackdriver', '~> 0.21.1'
# development.rb
config.google_cloud.project_id  = ENV.fetch('GOOGLE_CLOUD_PROJECT_ID')
config.google_cloud.keyfile     = ENV.fetch('GOOGLE_CLOUD_KEYFILE')
config.google_cloud.use_logging = true # Explicitly enable or disable Logging middleware. Default: Rails.env.production?

When I run my rails app using bin/dev, below is what I get.

4/02/24 5:17:16 ➜  Linked git:(main) ✗ bin/dev
17:17:18 web.1  | started with pid 94783
17:17:18 css.1  | started with pid 94784
17:17:20 web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/dp/z539166d4s505qs2tpt4dhdc0000gn/T/ruby-debug-sock-501/ruby-debug-username-94783)
17:17:21 web.1  | => Booting Puma
17:17:21 web.1  | => Rails 7.1.1 application starting in development 
17:17:21 web.1  | => Run `bin/rails server --help` for more startup options
17:17:22 web.1  | Exiting
17:17:22 web.1  | /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/commands/server/server_command.rb:83:in `log_to_stdout': undefined method `broadcast_to' for #<Google::Cloud::Logging::Logger:0x0000000107d3e020 @writer=#<Google::Cloud::Logging::AsyncWriter:0x0000000107d3e3e0 @mon_data=#<Monitor:0x0000000107d3e278>, @mon_data_owner_object_id=1480, @logging=#<Google::Cloud::Logging::Project:0x0000000107d3f790 @service=Google::Cloud::Logging::Service(project-id), @shared_async_writer=#<Google::Cloud::Logging::AsyncWriter:0x0000000107d3e3e0 ...>>, @max_count=10000, @max_bytes=10000000, @max_queue=100, @interval=5, @threads=10, @partial_success=false, @error_callbacks=[], @cond=#<MonitorMixin::ConditionVariable:0x0000000107d3e160 @monitor=#<Monitor:0x0000000107d3e278>, @cond=#<Thread::ConditionVariable:0x0000000107d3e110>>, @stopped=true>, @log_name="ruby_app_log", @resource=#<Google::Cloud::Logging::Resource:0x0000000107d3e610 @labels={}, @type="global">, @labels={}, @level=0, @request_info_var=#<Concurrent::ThreadLocalVar:0x0000000107d3df58 @default_block=nil, @default=nil, @index=31>, @closed=false, @formatter=#<Logger::Formatter:0x0000000107d3ddf0 @datetime_format=nil>, @datetime_format="", @silencer=true, @project="project-id"> (NoMethodError)
17:17:22 web.1  | 
17:17:22 web.1  |           Rails.logger.broadcast_to(console)
17:17:22 web.1  |                       ^^^^^^^^^^^^^
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/commands/server/server_command.rb:36:in `start'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/commands/server/server_command.rb:145:in `block in perform'
17:17:22 web.1  |       from <internal:kernel>:90:in `tap'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/commands/server/server_command.rb:136:in `perform'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/command/base.rb:178:in `invoke_command'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/command/base.rb:73:in `perform'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/command.rb:71:in `block in invoke'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/command.rb:149:in `with_argv'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/command.rb:69:in `invoke'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.1/lib/rails/commands.rb:18:in `<main>'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
17:17:22 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
17:17:22 web.1  |       from bin/rails:4:in `<main>'
17:17:22 web.1  | exited with code 1
17:17:22 system | sending SIGTERM to all processes
17:17:22 css.1  | bin/rails aborted!
17:17:22 css.1  | SignalException: SIGTERM
17:17:22 css.1  | 
17:17:22 css.1  | Tasks: TOP => tailwindcss:watch
17:17:22 css.1  | (See full trace by running task with --trace)
17:17:22 css.1  | exited with code 1
4/02/24 5:17:22 ➜  Linked git:(main) ✗

I updated my dependencies, but the result if the same:

4/02/24 5:34:19 ➜  Linked git:(main) ✗ bin/dev      
17:34:25 web.1  | started with pid 96200
17:34:25 css.1  | started with pid 96201
17:34:31 web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/dp/z539166d4s505qs2tpt4dhdc0000gn/T/rdbg-501/rdbg-96200)
17:34:35 web.1  | => Booting Puma
17:34:35 web.1  | => Rails 7.1.3 application starting in development 
17:34:35 web.1  | => Run `bin/rails server --help` for more startup options
17:34:37 web.1  | Exiting
17:34:37 web.1  | /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:83:in `log_to_stdout': undefined method `broadcast_to' for #<Google::Cloud::Logging::Logger:0x000000011564d4b0 @writer=#<Google::Cloud::Logging::AsyncWriter:0x000000011564d870 @mon_data=#<Monitor:0x000000011564d708>, @mon_data_owner_object_id=1480, @logging=#<Google::Cloud::Logging::Project:0x000000011564ec20 @service=Google::Cloud::Logging::Service(project-id), @shared_async_writer=#<Google::Cloud::Logging::AsyncWriter:0x000000011564d870 ...>>, @max_count=10000, @max_bytes=10000000, @max_queue=100, @interval=5, @threads=10, @partial_success=false, @error_callbacks=[], @cond=#<MonitorMixin::ConditionVariable:0x000000011564d5f0 @monitor=#<Monitor:0x000000011564d708>, @cond=#<Thread::ConditionVariable:0x000000011564d5a0>>, @stopped=true>, @log_name="ruby_app_log", @resource=#<Google::Cloud::Logging::Resource:0x000000011564daa0 @labels={}, @type="global">, @labels={}, @level=0, @request_info_var=#<Concurrent::ThreadLocalVar:0x000000011564d3e8 @default_block=nil, @default=nil, @index=31>, @closed=false, @formatter=#<Logger::Formatter:0x000000011564d280 @datetime_format=nil>, @datetime_format="", @silencer=true, @project="project-id"> (NoMethodError)
17:34:37 web.1  | 
17:34:37 web.1  |           Rails.logger.broadcast_to(console)
17:34:37 web.1  |                       ^^^^^^^^^^^^^
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:36:in `start'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:145:in `block in perform'
17:34:37 web.1  |       from <internal:kernel>:90:in `tap'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:136:in `perform'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/command/base.rb:178:in `invoke_command'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/command/base.rb:73:in `perform'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/command.rb:71:in `block in invoke'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/command.rb:149:in `with_argv'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/command.rb:69:in `invoke'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/railties-7.1.3/lib/rails/commands.rb:18:in `<main>'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
17:34:37 web.1  |       from /Users/username/.rvm/rubies/ruby-3.1.4/bin/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
17:34:37 web.1  |       from bin/rails:4:in `<main>'
17:34:37 web.1  | exited with code 1
17:34:37 system | sending SIGTERM to all processes
17:34:37 css.1  | bin/rails aborted!
17:34:37 css.1  | SignalException: SIGTERM
17:34:37 css.1  | 
17:34:37 css.1  | Tasks: TOP => tailwindcss:watch
17:34:37 css.1  | (See full trace by running task with --trace)
17:34:38 css.1  | exited with code 1
4/02/24 5:34:38 ➜  Linked git:(main) ✗

What should I do to make it work?


Solution

  • This is a known issue regarding Google Cloud Ruby support for Rails 7.1. See the the Github issue on their repo for details.

    Google::Cloud::Logging::Logger#broadcast_to needs to be implemented for Rails 7.1 to be supported. A PR was submitted here, but it wasn't yet merged.

    Until these changes are released, the workaround suggested here is to configure Rails logger according to the following in your application's config/environment.rb:

    unless Rails.logger.respond_to?(:broadcast_to)
      Rails.logger = ActiveSupport::BroadcastLogger.new(Rails.logger)
    end