googleapis / google-cloud-ruby

Google Cloud Client Library for Ruby
https://googleapis.github.io/google-cloud-ruby/
Apache License 2.0
1.35k stars 548 forks source link

Enable on_error when using with rails #4830

Open vascoosx opened 4 years ago

vascoosx commented 4 years ago

Is your feature request related to a problem? Please describe. I would like to know whenever logging fails. It seems that there is a way to do it when using it without the rails setup but it's unclear if it is possible to use it otherwise.

Describe the solution you'd like I would like to be able to set it like the following:

 config.google_cloud.logging.on_error = ->(err) {
    Google::Cloud::ErrorReporting.report err
  }

Describe alternatives you've considered just use it as it is used for non-rails apps

Additional context https://github.com/googleapis/google-cloud-ruby/blob/26b6ee9a39af7097e3109b4a964892bd44114fe9/google-cloud-logging/lib/google/cloud/logging/async_writer.rb#L322-L325

thagomizer commented 4 years ago

Are you getting an error when you try this or does it just appear unsupported from the docs? What specific google gems are you using so I can try to reproduce the issue here.

vascoosx commented 4 years ago

@thagomizer I am using the stackdriver gem (version 0.16.1). It just appears unsupported. I have no problem logging, though some logs seem to be missing. I thought maybe it's supported but was not sure because I didn't know how to test it.

naingtk commented 2 years ago

Observed in latest stackdriver gem.

If on_error is used in Google::Cloud.configure, it can catch the exceptions raised in the application.

Google::Cloud.configure do |config|
  config.project_id       = "<PROJECT_ID>"
  config.credentials    = "key.json"
  config.on_error        = lambda do |error| puts(error) end
end

But if it is used inside Rails config as follows, it is not catching exceptions. Rails’s environment/*.rb

Rails.application.configure do |config|
  config.google_cloud.project_id    = "<PROJECT_ID>"
  config.google_cloud.credentials  = "key.json"
  config.google_cloud.on_error      = lambda do |error| puts(error) end
  end
end

I have noticed the source codes here [1] and found that on_error is not copied in ErrorReporting (Cloud Trace and Cloud Logging also) for Rails. on_error is then added to the default callback [2] but it is not copied for Rails.

If this behavior is intended and on_error cannot be used in Rails config, please kindly add a note or warning to the READEME [3] as some developers are having trouble with which configuration can be used for Rails config.

[1] https://github.com/googleapis/google-cloud-ruby/blob/257524a891623db3da8daa29b8677a53fc174113/google-cloud-error_reporting/lib/google/cloud/error_reporting/rails.rb#L108 [2] https://github.com/googleapis/google-cloud-ruby/blob/26b6ee9a39af7097e3109b4a964892bd44114fe9/google-cloud-error_reporting/lib/google/cloud/error_reporting/async_error_reporter.rb#L187 [3] https://github.com/googleapis/google-cloud-ruby/blob/main/google-cloud-trace/README.md#authentication