Class: Honeybadger::Plugin
- Inherits:
-
Object
- Object
- Honeybadger::Plugin
- Defined in:
- lib/honeybadger/plugin.rb
Overview
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
Defined Under Namespace
Classes: CollectorExecution, Execution
Constant Summary collapse
- CALLER_FILE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regexp.new('\A(?:\w:)?([^:]+)(?=(:\d+))').freeze
- @@instances =
This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.
{}
Instance Attribute Summary collapse
- #executions ⇒ Object readonly private
- #name ⇒ Object readonly private
- #requirements ⇒ Object readonly private
Class Method Summary collapse
- .instances ⇒ Object private
- .load!(config) ⇒ Object private
- .name_from_caller(caller) ⇒ Object private
-
.register(name = nil, &block) ⇒ Object
Register a new plugin with Honeybadger.
Instance Method Summary collapse
-
#collect(options = {}, &block) ⇒ Object
Define an collect block.
- #collectors ⇒ Object private
-
#execution(&block) ⇒ Object
Define an execution block.
-
#initialize(name) ⇒ Plugin
constructor
private
A new instance of Plugin.
- #load!(config) ⇒ Object private
- #loaded? ⇒ Boolean private
- #ok?(config) ⇒ Boolean private
-
#requirement(&block) ⇒ Object
Define a requirement.
-
#reset! ⇒ Object
Used for testing only; don’t normally call this.
Constructor Details
#initialize(name) ⇒ Plugin
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Plugin.
159 160 161 162 163 164 165 |
# File 'lib/honeybadger/plugin.rb', line 159 def initialize(name) @name = name @loaded = false @requirements = [] @executions = [] @collectors = [] end |
Instance Attribute Details
#executions ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
284 285 286 |
# File 'lib/honeybadger/plugin.rb', line 284 def executions @executions end |
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
284 285 286 |
# File 'lib/honeybadger/plugin.rb', line 284 def name @name end |
#requirements ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
284 285 286 |
# File 'lib/honeybadger/plugin.rb', line 284 def requirements @requirements end |
Class Method Details
.instances ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
66 67 68 |
# File 'lib/honeybadger/plugin.rb', line 66 def instances @@instances end |
.load!(config) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
93 94 95 96 97 98 99 100 101 |
# File 'lib/honeybadger/plugin.rb', line 93 def load!(config) instances.each_pair do |name, plugin| if config.load_plugin?(name) plugin.load!(config) else config.logger.debug(sprintf('skip plugin name=%s reason=disabled', name)) end end end |
.name_from_caller(caller) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
104 105 106 107 108 |
# File 'lib/honeybadger/plugin.rb', line 104 def name_from_caller(caller) caller && caller[0].match(CALLER_FILE) or fail("Unable to determine name from caller: #{caller.inspect}") File.basename($1)[/[^\.]+/] end |
.register(name = nil, &block) ⇒ Object
Register a new plugin with Honeybadger. See #requirement, #execution, and #collect..
85 86 87 88 89 90 |
# File 'lib/honeybadger/plugin.rb', line 85 def register(name = nil, &block) name ||= name_from_caller(caller) or raise(ArgumentError, 'Plugin name is required, but was nil.') instances[key = name.to_sym] and fail("Already registered: #{name}") instances[key] = new(name).tap { |d| d.instance_eval(&block) } end |
Instance Method Details
#collect(options = {}, &block) ⇒ Object
Define an collect block. Collect blocks will be added to an execution queue if requirement blocks return true
. The block will be called as frequently as once per second, but can be configured to increase it’s interval.
234 235 236 |
# File 'lib/honeybadger/plugin.rb', line 234 def collect(={}, &block) @collectors << [, block] end |
#collectors ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
268 269 270 |
# File 'lib/honeybadger/plugin.rb', line 268 def collectors @collectors end |
#execution(&block) ⇒ Object
Define an execution block. Execution blocks will be executed if all requirement blocks return true
.
209 210 211 |
# File 'lib/honeybadger/plugin.rb', line 209 def execution(&block) @executions << block end |
#load!(config) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/honeybadger/plugin.rb', line 247 def load!(config) if @loaded config.logger.debug(sprintf('skip plugin name=%s reason=loaded', name)) return false elsif ok?(config) config.logger.debug(sprintf('load plugin name=%s', name)) @executions.each {|e| Execution.new(config, &e).call } @collectors.each {|o,b| CollectorExecution.new(name, config, o, &b).register! } @loaded = true else config.logger.debug(sprintf('skip plugin name=%s reason=requirement', name)) end @loaded rescue => e config.logger.error(sprintf("plugin error name=%s class=%s message=%s\n\t%s", name, e.class, e..dump, Array(e.backtrace).join("\n\t"))) @loaded = true false end |
#loaded? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
273 274 275 |
# File 'lib/honeybadger/plugin.rb', line 273 def loaded? @loaded end |
#ok?(config) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
239 240 241 242 243 244 |
# File 'lib/honeybadger/plugin.rb', line 239 def ok?(config) @requirements.all? {|r| Execution.new(config, &r).call } rescue => e config.logger.error(sprintf("plugin error name=%s class=%s message=%s\n\t%s", name, e.class, e..dump, Array(e.backtrace).join("\n\t"))) false end |
#requirement(&block) ⇒ Object
Define a requirement. All requirement blocks must return true
for the plugin to be executed.
184 185 186 |
# File 'lib/honeybadger/plugin.rb', line 184 def requirement(&block) @requirements << block end |
#reset! ⇒ Object
Used for testing only; don’t normally call this. :)
279 280 281 |
# File 'lib/honeybadger/plugin.rb', line 279 def reset! @loaded = false end |