Class: Honeybadger::Agent
- Inherits:
-
Object
- Object
- Honeybadger::Agent
- Extended by:
- Forwardable
- Includes:
- Logging::Helper
- Defined in:
- lib/honeybadger/agent.rb
Overview
The Honeybadger agent contains all the methods for interacting with the Honeybadger service. It can be used to send notifications to multiple projects in large apps. The global agent instance (Agent.instance) should always be accessed through the Honeybadger singleton.
Context
Context is global by default, meaning agents created via Honeybadger::Agent.new
will share context (added via Honeybadger.context
or #context) with other agents. This also includes the Rack environment when using the Rack::ErrorNotifier middleware. To localize context for a custom agent, use the local_context: true option when initializing.
Instance Attribute Summary collapse
- #config ⇒ Object readonly private
- #events_worker ⇒ Object readonly private
- #metrics_worker ⇒ Object readonly private
- #worker ⇒ Object readonly private
Class Method Summary collapse
- .instance ⇒ Object private
- .instance=(instance) ⇒ Object private
Instance Method Summary collapse
-
#add_breadcrumb(message, metadata: {}, category: "custom") ⇒ Object
Appends a breadcrumb to the trace.
- #backend ⇒ Object private
-
#backtrace_filter {|line| ... } ⇒ Object
DEPRECATED: Callback to filter backtrace lines.
-
#breadcrumbs ⇒ Object
private
Direct access to the Breadcrumbs::Collector instance.
-
#check_in(id) ⇒ Boolean
Perform a synchronous check_in.
-
#clear! ⇒ Object
Clear all transaction scoped data.
- #collect(collector) ⇒ Object private
-
#configure {|Config::Ruby| ... } ⇒ Object
Configure the Honeybadger agent via Ruby.
-
#context(context = nil, &block) ⇒ Object, self
Save global context for the current request.
- #decrement_counter ⇒ Object private
-
#event(event_type, payload = {}) ⇒ void
Sends event to events backend.
-
#event_context(context = nil, &block) ⇒ Object, self
Save event-specific context for the current request.
-
#exception_filter ⇒ Object
DEPRECATED: Callback to ignore exceptions.
-
#exception_fingerprint ⇒ Object
DEPRECATED: Callback to add a custom grouping strategy for exceptions.
-
#flush { ... } ⇒ Object, Boolean
Flushes all data from workers before returning.
- #gauge ⇒ Object private
-
#get_context ⇒ Hash?
Get global context for the current request.
-
#get_event_context ⇒ Hash?
Get event-specific context for the current request.
- #histogram ⇒ Object private
- #increment_counter ⇒ Object private
- #init!(...) ⇒ Object private
-
#initialize(opts = {}) ⇒ Agent
constructor
A new instance of Agent.
- #instrumentation ⇒ Object
-
#notify(exception_or_opts = nil, **opts) ⇒ String, false
Sends an exception to Honeybadger.
- #registry ⇒ Object private
-
#stop(force = false) ⇒ Object
Stops the Honeybadger service.
-
#stop_insights(force = false) ⇒ Object
Stops the Honeybadger Insights related services.
- #time ⇒ Object private
-
#track_deployment(environment: nil, revision: nil, local_username: nil, repository: nil) ⇒ Boolean
Track a new deployment.
- #with_rack_env(rack_env, &block) ⇒ Object private
Constructor Details
#initialize(opts = {}) ⇒ Agent
Returns a new instance of Agent.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/honeybadger/agent.rb', line 65 def initialize(opts = {}) if opts.is_a?(Config) @config = opts opts = {} end @context = opts.delete(:context) local_context = opts.delete(:local_context) @config ||= Config.new(opts) if local_context @context ||= ContextManager.new @breadcrumbs = Breadcrumbs::Collector.new(config) else @breadcrumbs = nil end init_worker end |
Instance Attribute Details
#config ⇒ 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.
512 513 514 |
# File 'lib/honeybadger/agent.rb', line 512 def config @config end |
#events_worker ⇒ 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.
585 586 587 |
# File 'lib/honeybadger/agent.rb', line 585 def events_worker @events_worker end |
#metrics_worker ⇒ 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.
585 586 587 |
# File 'lib/honeybadger/agent.rb', line 585 def metrics_worker @metrics_worker end |
#worker ⇒ 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.
585 586 587 |
# File 'lib/honeybadger/agent.rb', line 585 def worker @worker end |
Class Method Details
.instance ⇒ 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.
56 57 58 |
# File 'lib/honeybadger/agent.rb', line 56 def self.instance @instance end |
.instance=(instance) ⇒ 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.
61 62 63 |
# File 'lib/honeybadger/agent.rb', line 61 def self.instance=(instance) @instance = instance end |
Instance Method Details
#add_breadcrumb(message, metadata: {}, category: "custom") ⇒ Object
Appends a breadcrumb to the trace. Use this when you want to add some custom data to your breadcrumb trace in effort to help debugging. If a notice is reported to Honeybadger, all breadcrumbs within the execution path will be appended to the notice. You will be able to view the breadcrumb trace in the Honeybadger interface to see what events led up to the notice.
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/honeybadger/agent.rb', line 326 def (, metadata: {}, category: "custom") params = Util::Sanitizer.new(max_depth: 2).sanitize({ category: category, message: , metadata: }) .add!(Breadcrumbs::Breadcrumb.new(**params)) self end |
#backend ⇒ 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.
595 |
# File 'lib/honeybadger/agent.rb', line 595 def_delegators :config, :backend |
#backtrace_filter {|line| ... } ⇒ Object
DEPRECATED: Callback to filter backtrace lines. One use for this is to make additional [PROJECT_ROOT] or [GEM_ROOT] substitutions, which are used by Honeybadger when grouping errors and displaying application traces.
572 |
# File 'lib/honeybadger/agent.rb', line 572 def_delegator :config, :backtrace_filter |
#breadcrumbs ⇒ 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.
Direct access to the Breadcrumbs::Collector instance
300 301 302 303 304 |
# File 'lib/honeybadger/agent.rb', line 300 def return @breadcrumbs if @breadcrumbs Thread.current[:__hb_breadcrumbs] ||= Breadcrumbs::Collector.new(config) end |
#check_in(id) ⇒ Boolean
Perform a synchronous check_in.
207 208 209 210 211 212 |
# File 'lib/honeybadger/agent.rb', line 207 def check_in(id) # this is to allow check ins even if a url is passed check_in_id = id.to_s.strip.gsub(/\/$/, "").split("/").last response = backend.check_in(check_in_id) response.success? end |
#clear! ⇒ Object
Clear all transaction scoped data.
282 283 284 285 |
# File 'lib/honeybadger/agent.rb', line 282 def clear! context_manager.clear! .clear! end |
#collect(collector) ⇒ 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.
496 497 498 499 500 501 |
# File 'lib/honeybadger/agent.rb', line 496 def collect(collector) return unless config.insights_enabled? init_metrics_worker metrics_worker.push(collector) end |
#configure {|Config::Ruby| ... } ⇒ Object
Configure the Honeybadger agent via Ruby.
524 |
# File 'lib/honeybadger/agent.rb', line 524 def_delegator :config, :configure |
#context(context = nil, &block) ⇒ Object, self
Save global context for the current request.
274 275 276 277 278 279 |
# File 'lib/honeybadger/agent.rb', line 274 def context(context = nil, &block) block_result = context_manager.set_context(context, &block) unless context.nil? return block_result if block_given? self end |
#decrement_counter ⇒ 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.
620 |
# File 'lib/honeybadger/agent.rb', line 620 def_delegator :instrumentation, :decrement_counter |
#event(event_type, payload = {}) ⇒ void
This method returns an undefined value.
Sends event to events backend
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/honeybadger/agent.rb', line 411 def event(event_type, payload = {}) init_events_worker extra_payload = {}.tap do |p| p[:request_id] = context_manager.get_request_id if context_manager.get_request_id p[:hostname] = config[:hostname].to_s if config[:"events.attach_hostname"] p.update(context_manager.get_event_context || {}) end event = Event.new(event_type, extra_payload.merge(payload)) config.before_event_hooks.each do |hook| with_error_handling { hook.call(event) } end return if config.ignored_events.any? do |check| with_error_handling do check.all? do |keys, value| if keys == [:event_type] event.event_type&.match?(value) elsif event.dig(*keys) event.dig(*keys).to_s.match?(value) end end end end return if event.halted? return unless sample_event?(event) (event) events_worker.push(event.as_json) end |
#event_context(context = nil, &block) ⇒ Object, self
Save event-specific context for the current request.
477 478 479 480 481 482 |
# File 'lib/honeybadger/agent.rb', line 477 def event_context(context = nil, &block) block_result = context_manager.set_event_context(context, &block) unless context.nil? return block_result if block_given? self end |
#exception_filter ⇒ Object
DEPRECATED: Callback to ignore exceptions.
See public API documentation for Notice for available attributes.
543 |
# File 'lib/honeybadger/agent.rb', line 543 def_delegator :config, :exception_filter |
#exception_fingerprint ⇒ Object
DEPRECATED: Callback to add a custom grouping strategy for exceptions. The return value is hashed and sent to Honeybadger. Errors with the same fingerprint will be grouped.
See public API documentation for Notice for available attributes.
558 |
# File 'lib/honeybadger/agent.rb', line 558 def_delegator :config, :exception_fingerprint |
#flush { ... } ⇒ Object, Boolean
Flushes all data from workers before returning. This is most useful in tests when using the test backend, where normally the asynchronous nature of this library could create race conditions.
367 368 369 370 371 372 373 374 |
# File 'lib/honeybadger/agent.rb', line 367 def flush return true unless block_given? yield ensure worker.flush events_worker&.flush metrics_worker&.flush end |
#gauge ⇒ 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.
610 |
# File 'lib/honeybadger/agent.rb', line 610 def_delegator :instrumentation, :gauge |
#get_context ⇒ Hash?
Get global context for the current request.
294 295 296 |
# File 'lib/honeybadger/agent.rb', line 294 def get_context context_manager.get_context end |
#get_event_context ⇒ Hash?
Get event-specific context for the current request.
491 492 493 |
# File 'lib/honeybadger/agent.rb', line 491 def get_event_context context_manager.get_event_context end |
#histogram ⇒ 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.
605 |
# File 'lib/honeybadger/agent.rb', line 605 def_delegator :instrumentation, :histogram |
#increment_counter ⇒ 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.
615 |
# File 'lib/honeybadger/agent.rb', line 615 def_delegator :instrumentation, :increment_counter |
#init!(...) ⇒ 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.
590 |
# File 'lib/honeybadger/agent.rb', line 590 def_delegators :config, :init! |
#instrumentation ⇒ Object
622 623 624 |
# File 'lib/honeybadger/agent.rb', line 622 def instrumentation @instrumentation ||= Honeybadger::Instrumentation.new(self) end |
#notify(exception_or_opts = nil, **opts) ⇒ String, false
Sends an exception to Honeybadger. Does not report ignored exceptions by default.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/honeybadger/agent.rb', line 128 def notify(exception_or_opts = nil, **opts) if !config[:"exceptions.enabled"] debug { "disabled feature=notices" } return false end opts = opts.dup if exception_or_opts.is_a?(Exception) already_reported_notice_id = exception_or_opts.instance_variable_get(:@__hb_notice_id) return already_reported_notice_id if already_reported_notice_id opts[:exception] = exception_or_opts elsif exception_or_opts.respond_to?(:to_hash) opts.merge!(exception_or_opts.to_hash) elsif !exception_or_opts.nil? opts[:error_message] = exception_or_opts.to_s end validate_notify_opts!(opts) if config[:"breadcrumbs.enabled"] ( "Honeybadger Notice", metadata: opts, category: "notice" ) end opts[:rack_env] ||= context_manager.get_rack_env opts[:global_context] ||= context_manager.get_context opts[:breadcrumbs] ||= .dup opts[:request_id] ||= context_manager.get_request_id notice = Notice.new(config, opts) config.before_notify_hooks.each do |hook| break if notice.halted? with_error_handling { hook.call(notice) } end unless NOT_BLANK.match?(notice.api_key) error { sprintf("Unable to send error report: API key is missing. id=%s", notice.id) } return false end if !opts[:force] && notice.ignore? debug { sprintf("ignore notice feature=notices id=%s", notice.id) } return false end if notice.halted? debug { "halted notice feature=notices" } return false end info { sprintf("Reporting error id=%s", notice.id) } if opts[:sync] || config[:sync] send_now(notice) else push(notice) end if exception_or_opts.is_a?(Exception) exception_or_opts.instance_variable_set(:@__hb_notice_id, notice.id) unless exception_or_opts.frozen? end notice.id end |
#registry ⇒ 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.
504 505 506 507 508 509 |
# File 'lib/honeybadger/agent.rb', line 504 def registry return @registry if defined?(@registry) @registry = Honeybadger::Registry.new.tap do |r| collect(Honeybadger::RegistryExecution.new(r, config, {})) end end |
#stop(force = false) ⇒ Object
Stops the Honeybadger service.
380 381 382 383 384 385 |
# File 'lib/honeybadger/agent.rb', line 380 def stop(force = false) worker.shutdown(force) events_worker&.shutdown(force) metrics_worker&.shutdown(force) true end |
#stop_insights(force = false) ⇒ Object
Stops the Honeybadger Insights related services.
391 392 393 394 395 |
# File 'lib/honeybadger/agent.rb', line 391 def stop_insights(force = false) events_worker&.shutdown(force) metrics_worker&.shutdown(force) true end |
#time ⇒ 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.
600 |
# File 'lib/honeybadger/agent.rb', line 600 def_delegator :instrumentation, :time |
#track_deployment(environment: nil, revision: nil, local_username: nil, repository: nil) ⇒ Boolean
Track a new deployment
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/honeybadger/agent.rb', line 226 def track_deployment(environment: nil, revision: nil, local_username: nil, repository: nil) opts = { environment: environment || config[:env], revision: revision || config[:revision], local_username: local_username, repository: repository } response = backend.track_deployment(opts) response.success? end |
#with_rack_env(rack_env, &block) ⇒ 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.
575 576 577 578 579 580 581 582 |
# File 'lib/honeybadger/agent.rb', line 575 def with_rack_env(rack_env, &block) context_manager.set_rack_env(rack_env) context_manager.set_request_id(rack_env["action_dispatch.request_id"] || SecureRandom.uuid) yield ensure context_manager.set_rack_env(nil) context_manager.set_request_id(nil) end |