class Honeybadger::Plugin

Honeybadger::Plugin defines the API for registering plugins with Honeybadger. Each plugin has requirements which must be satisfied before executing the plugin’s execution block(s). This allows us to detect optional dependencies and load the plugin for each dependency only if it’s present in the application.

Plugins may also define a collect block that is repeatedly called from within a thread. The MetricsWorker contains a loop that will call all enabled plugins’ collect method, and then sleep for 1 second. This block is useful for collecting and/or sending metrics at regular intervals.

See the plugins/ directory for examples of official plugins. If you’re interested in developing a plugin for Honeybadger, see the Integration Guide: docs.honeybadger.io/ruby/gem-reference/integration.html

@example

require 'honeybadger/plugin'
require 'honeybadger/ruby'

module Honeybadger
  module Plugins
    # Register your plugin with an optional name. If the name (such as
    # "my_framework") is not provided, Honeybadger will try to infer the name
    # from the current file.
    Plugin.register 'my_framework' do
      requirement do
        # Check to see if the thing you're integrating with is loaded. Return true
        # if it is, or false if it isn't. An exception in this block is equivalent
        # to returning false. Multiple requirement blocks are supported.
        defined?(MyFramework)
      end

      execution do
        # Write your integration. This code will be executed only if all requirement
        # blocks return true. An exception in this block will disable the plugin.
        # Multiple execution blocks are supported.
        MyFramework.on_exception do |exception|
          Honeybadger.notify(exception)
        end
      end

      collect do
        # This block will be periodically called at regular intervals. Here you can
        # gather metrics or inspect services. See the Honeybadger::InstrumentationHelper
        # module to see availble methods for metric collection.
        gauge 'scheduled_jobs', -> { MyFramework.stats.scheduled_jobs.count }
        gauge 'latency', -> { MyFramework.stats.latency }
      end
    end
  end
end