Bugsnag for Ruby

repository·master·Indexed 42 days ago

https://github.com/bugsnag/bugsnag-ruby

An error monitoring and exception reporting library for Ruby. It provides automatic and manual reporting of uncaught exceptions for various environments including Rails, Sinatra, Rack, Sidekiq, Que, Resque, and Shoryuken.

Tokens
9.9K
Snippets
55
Records
65
Agent score
50%

What's inside Bugsnag

  1. Overview of Bugsnag Ruby features

    master

    Bugsnag for Ruby provides error monitoring and exception reporting for Rails, Sinatra, Rack, or plain Ruby applications. Key capabilities include:

    • Automatic Reporting: Automatically captures and reports unhandled exceptions and crashes.
    • Manual Reporting: Ability to report handled exceptions.
    • User Context: Attach user information to error reports to track how many unique users are affected by a crash.
    • Custom Diagnostics: Send customized diagnostic data alongside error reports.
    • Breadcrumbs: Track events that occur leading up to a crash to aid debugging.
  2. Migrate from Bugsnag 5.x to 6.x

    master
    When upgrading from version 5.x to 6.x, several breaking changes were introduced regarding configuration, notification syntax, and logging. Key changes include the removal of use_ssl (use protocol in endpoint instead), the renaming of params_filters to meta_data_filters, and a change in how notify is called (block syntax only).
  3. Run the Shoryuken Bugsnag example

    master

    To run this specific example, ensure you have installed dependencies and configured your AWS credentials as required by Shoryuken.

    1. Install dependencies:
      bundle install
    2. Start the Shoryuken processor:
      bundle exec shoryuken -r ./shoryuken.rb
    3. In a separate terminal, open an IRB session to queue messages:
      bundle exec irb -r ./shoryuken.rb
    4. Queue a message to trigger processing:
      BugsnagTest.perform_async "Hello world"
    bundle exec shoryuken -r ./shoryuken.rb
  4. Run Resque workers for Bugsnag testing

    master

    To test Bugsnag error reporting within a Resque environment, you must have a running Redis instance. You can run Resque workers to automatically execute tasks that trigger errors by setting the QUEUE environment variable and using the resque:work rake task.

    To run workers for specific queues (e.g., crash and metadata) used in the demo, use the following command:

    QUEUE=crash,metadata bundle exec rake resque:work
  5. Configure Bugsnag in a Rails application

    master

    You can configure Bugsnag in a Rails application using one of two methods:

    1. Environment Variable: Export your API key as the BUGSNAG_API_KEY environment variable.
    2. Configuration File: Generate a dedicated initializer file at config/initializers/bugsnag.rb using the Rails generator. This file can be populated with various configuration options.

    Once configured, Bugsnag will automatically start reporting unhandled exceptions.

    # Method 1: Environment Variable
    export BUGSNAG_API_KEY=YOUR_API_KEY_HERE
    
    # Method 2: Configuration File Generator
    rails generate bugsnag YOUR_API_KEY_HERE
  6. Configure Bugsnag in Rails v5.1

    master

    To start reporting unhandled exceptions to Bugsnag in a Rails application, use one of the following two methods:

    1. Environment Variable: Export your API key as BUGSNAG_API_KEY.
    2. Configuration File: Generate a dedicated initializer file at config/initializers/bugsnag.rb using the Rails generator:
      bundle exec bin/rails generate bugsnag YOUR_API_KEY_HERE

    Once configured, Bugsnag will automatically report unhandled exceptions.

    bundle exec bin/rails generate bugsnag YOUR_API_KEY_HERE
  7. Report errors from Resque tasks

    master

    When working with Resque, you can trigger Bugsnag reports in two primary ways:

    1. Framework-level errors: Raising an error directly within the Resque framework will generate a report in the Bugsnag dashboard.
    2. Manual notification with metadata: You can use a block to attach additional diagnostic data to a notification. This allows you to populate specific tabs in the Bugsnag dashboard, such as a queue tab for queue information or a diagnostics tab for custom metadata.
  8. Integrate Bugsnag with Padrino

    master

    To use Bugsnag in a Padrino application, you must configure the Bugsnag client and register the Bugsnag::Rack middleware within the Padrino boot process.

    1. Configure Bugsnag: In config/boot.rb, use the Padrino.before_load block to set your API key.
    2. Register Middleware: In config/boot.rb, use the Padrino.after_load block to add Bugsnag::Rack to the middleware stack.
    3. Development Mode: By default, automatic notifications are enabled in production. To enable them in development, you must configure your app to raise errors and hide standard exception pages.
    # 1. In config/boot.rb
    Padrino.before_load do
      Bugsnag.configure do |config|
        config.api_key = 'YOUR_API_KEY'
      end
    end
    
    # 2. In config/boot.rb
    Padrino.after_load do
      Padrino.use Bugsnag::Rack
    end
    
    # 3. In app/app.rb (for development mode)
    set :raise_errors, true
    set :show_exceptions, false
  9. Configure Bugsnag in Rails

    master

    To start reporting unhandled exceptions to Bugsnag in a Rails application, you can use one of two methods:

    1. Environment Variable: Export your API key as BUGSNAG_API_KEY.
    2. Configuration File: Generate a dedicated configuration file at config/initializers/bugsnag.rb using the Rails generator. This file can be further customized with specific configuration options.
    # Method 1: Environment Variable
    export BUGSNAG_API_KEY=YOUR_API_KEY_HERE
    
    # Method 2: Configuration File Generator
    bundle exec bin/rails generate bugsnag YOUR_API_KEY_HERE
  10. Configure Bugsnag for Sinatra

    master

    To use Bugsnag in a Sinatra application, you must perform three steps: provide an API key, activate the Bugsnag Rack middleware, and configure Sinatra's error handling to allow Bugsnag to capture exceptions.

    1. Provide an API Key

    You can provide your API key in one of two ways:

    • Environment Variable: Export BUGSNAG_API_KEY in your environment.
    • Configuration Block: Use Bugsnag.configure to set the api_key manually.

    2. Activate Middleware

    Add the Bugsnag Rack middleware to your Sinatra application stack using use Bugsnag::Rack.

    3. Configure Sinatra Error Handling

    To ensure exceptions are automatically captured and notified during development or testing, set Sinatra's :raise_errors to true and :show_exceptions to false.

    # 1. Configure API Key
    Bugsnag.configure do |configuration|
      configuration.api_key = "YOUR_API_KEY"
    end
    
    # 2. Activate Middleware
    use Bugsnag::Rack
    
    # 3. Configure Sinatra error handling
    set :raise_errors, true
    set :show_exceptions, false