lib/honeybadger/agent.rb in honeybadger-5.8.0 vs lib/honeybadger/agent.rb in honeybadger-5.11.0
- old
+ new
@@ -6,11 +6,14 @@
require 'honeybadger/notice'
require 'honeybadger/plugin'
require 'honeybadger/logging'
require 'honeybadger/worker'
require 'honeybadger/events_worker'
+require 'honeybadger/metrics_worker'
require 'honeybadger/breadcrumbs'
+require 'honeybadger/registry'
+require 'honeybadger/registry_execution'
module Honeybadger
# 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
@@ -256,13 +259,15 @@
# @option context [String] :user_email The user email address (optional).
# @option context [String] :tags The comma-separated list of tags.
# When present, tags will be applied to errors with this context
# (optional).
#
- # @return [self] so that method calls can be chained.
- def context(context = nil)
- context_manager.set_context(context) unless context.nil?
+ # @return [Object, self] value of the block if passed, otherwise self
+ def context(context = nil, &block)
+ block_result = context_manager.set_context(context, &block) unless context.nil?
+ return block_result if block_given?
+
self
end
# Clear all transaction scoped data.
def clear!
@@ -354,19 +359,21 @@
return true unless block_given?
yield
ensure
worker.flush
events_worker&.flush
+ metrics_worker&.flush
end
# Stops the Honeybadger service.
#
# @example
# Honeybadger.stop # => nil
def stop(force = false)
worker.shutdown(force)
events_worker&.shutdown(force)
+ metrics_worker&.shutdown(force)
true
end
# Sends event to events backend
#
@@ -383,25 +390,51 @@
#
# @return [void]
def event(event_type, payload = {})
init_events_worker
- ts = DateTime.now.new_offset(0).rfc3339
+ ts = Time.now.utc.strftime("%FT%T.%LZ")
merged = {ts: ts}
if event_type.is_a?(String)
- merged.merge!(event_type: event_type)
+ merged[:event_type] = event_type
else
merged.merge!(Hash(event_type))
end
+ if (request_id = context_manager.get_request_id)
+ merged[:request_id] = request_id
+ end
+
+ if config[:'events.attach_hostname']
+ merged[:hostname] = config[:hostname].to_s
+ end
+
merged.merge!(Hash(payload))
+ return if config.ignored_events.any? { |check| merged[:event_type]&.match?(check) }
+
events_worker.push(merged)
end
# @api private
+ def collect(collector)
+ return unless config.insights_enabled?
+
+ init_metrics_worker
+ metrics_worker.push(collector)
+ end
+
+ # @api private
+ def registry
+ return @registry if defined?(@registry)
+ @registry = Honeybadger::Registry.new.tap do |r|
+ collect(Honeybadger::RegistryExecution.new(r, config, {}))
+ end
+ end
+
+ # @api private
attr_reader :config
# Configure the Honeybadger agent via Ruby.
#
# @example
@@ -463,17 +496,19 @@
def_delegator :config, :backtrace_filter
# @api private
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
# @api private
- attr_reader :worker, :events_worker
+ attr_reader :worker, :events_worker, :metrics_worker
# @api private
# @!method init!(...)
# @see Config#init!
def_delegators :config, :init!
@@ -481,10 +516,39 @@
# @api private
# @!method backend
# @see Config#backend
def_delegators :config, :backend
+ # @api private
+ # @!method time
+ # @see Honeybadger::Instrumentation#time
+ def_delegator :instrumentation, :time
+
+ # @api private
+ # @!method histogram
+ # @see Honeybadger::Instrumentation#histogram
+ def_delegator :instrumentation, :histogram
+
+ # @api private
+ # @!method gauge
+ # @see Honeybadger::Instrumentation#gauge
+ def_delegator :instrumentation, :gauge
+
+ # @api private
+ # @!method increment_counter
+ # @see Honeybadger::Instrumentation#increment_counter
+ def_delegator :instrumentation, :increment_counter
+
+ # @api private
+ # @!method decrement_counter
+ # @see Honeybadger::Instrumentation#decrement_counter
+ def_delegator :instrumentation, :decrement_counter
+
+ def instrumentation
+ @instrumentation ||= Honeybadger::Instrumentation.new(self)
+ end
+
private
def validate_notify_opts!(opts)
return if opts.has_key?(:exception)
return if opts.has_key?(:error_message)
@@ -514,9 +578,14 @@
end
def init_events_worker
return if @events_worker
@events_worker = EventsWorker.new(config)
+ end
+
+ def init_metrics_worker
+ return if @metrics_worker
+ @metrics_worker = MetricsWorker.new(config)
end
def with_error_handling
yield
rescue => ex