class Google::Cloud::ErrorReporting::AsyncErrorReporter
@private Used by {Google::Cloud::ErrorReporting} and {Google::Cloud::ErrorReporting::Middleware} to asynchronously submit error events to Stackdriver Error Reporting service when used in Ruby applications.
Attributes
@private Implementation accessors
@private Implementation accessors
@private Implementation accessors
@private Implementation accessors
Public Class Methods
@private Creates a new AsyncErrorReporter
instance.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 75 def initialize error_reporting, max_queue: 1000, threads: 10 @error_reporting = error_reporting @max_queue = max_queue @threads = threads @thread_pool = Concurrent::ThreadPoolExecutor.new \ max_threads: @threads, max_queue: @max_queue @error_callbacks = [] # Make sure all queued calls are completed when process exits. at_exit { stop! } end
Public Instance Methods
Register to be notified of errors when raised.
If an unhandled error has occurred the reporter will attempt to recover from the error and resume reporting error_events.
Multiple error handlers can be added.
@yield [callback] The block to be called when an error is raised. @yieldparam [Exception] error The error raised.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 171 def on_error &block @error_callbacks << block end
Add the error event to the queue. This will raise if there are no resources available to make the API call.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 93 def report error_event Concurrent::Promises.future_on @thread_pool, error_event do |error| report_sync error end rescue Concurrent::RejectedExecutionError => e raise AsyncErrorReporterError, "Error reporting error_event asynchronously: #{e.message}", error_event end
Whether the reporter has been started.
@return [boolean] `true` when started, `false` otherwise.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 147 def started? @thread_pool&.running? end
Begins the process of stopping the reporter. ErrorEvents already in the queue will be published, but no new ErrorEvent
can be added. Use {#wait!} to block until the reporter is fully stopped and all pending error_events have been pushed to the API.
@return [AsyncErrorReporter] returns self so calls can be chained.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 110 def stop @thread_pool&.shutdown self end
Stop this asynchronous reporter and block until it has been stopped.
@param [Number] timeout Timeout in seconds.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 121 def stop! timeout = nil stop wait! timeout end
Whether the reporter has been stopped.
@return [boolean] `true` when stopped, `false` otherwise.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 156 def stopped? !started? end
Blocks until the reporter is fully stopped, all pending error_events have been published, and all callbacks have completed. Does not stop the reporter. To stop the reporter, first call {#stop} and then call {#wait!} to block until the reporter is stopped.
@return [AsyncErrorReporter] returns self so calls can be chained.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 133 def wait! timeout = nil if @thread_pool @thread_pool.shutdown @thread_pool.wait_for_termination timeout end self end
Protected Instance Methods
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 184 def default_error_callbacks # This is memoized to reduce calls to the configuration. @default_error_callbacks ||= begin error_cb = Google::Cloud::ErrorReporting.configure.on_error error_cb ||= Google::Cloud.configure.on_error if error_cb [error_cb] else [] end end end
Calls all error callbacks.
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 178 def error! error error_callbacks = @error_callbacks error_callbacks = default_error_callbacks if error_callbacks.empty? error_callbacks.each { |error_callback| error_callback.call error } end
# File lib/google/cloud/error_reporting/async_error_reporter.rb, line 197 def report_sync error_event error_reporting.report error_event rescue StandardError => e sync_error = ErrorReporterError.new( "Error reporting error_event: #{e.message}", error_event ) # Manually set backtrace so we don't have to raise sync_error.set_backtrace caller error! sync_error end