lib/honeybadger/agent.rb in honeybadger-5.4.1 vs lib/honeybadger/agent.rb in honeybadger-5.5.0

- old
+ new

@@ -5,10 +5,11 @@ require 'honeybadger/context_manager' require 'honeybadger/notice' require 'honeybadger/plugin' require 'honeybadger/logging' require 'honeybadger/worker' +require 'honeybadger/events_worker' require 'honeybadger/breadcrumbs' module Honeybadger # The Honeybadger agent contains all the methods for interacting with the # Honeybadger service. It can be used to send notifications to multiple @@ -352,21 +353,54 @@ def flush return true unless block_given? yield ensure worker.flush + events_worker&.flush end # Stops the Honeybadger service. # # @example # Honeybadger.stop # => nil def stop(force = false) worker.shutdown(force) + events_worker&.shutdown(force) true end + # Sends event to events backend + # + # @example + # # With event type as first argument (recommended): + # Honeybadger.event("user_signed_up", user_id: 123) + # + # # With just a payload: + # Honeybadger.event(event_type: "user_signed_up", user_id: 123) + # + # @param event_name [String, Hash] a String describing the event or a Hash + # when the second argument is omitted. + # @param payload [Hash] Additional data to be sent with the event as keyword arguments + # + # @return [void] + def event(event_type, payload = {}) + init_events_worker + + ts = DateTime.now.new_offset(0).rfc3339 + merged = {ts: ts} + + if event_type.is_a?(String) + merged.merge!(event_type: event_type) + else + merged.merge!(Hash(event_type)) + end + + merged.merge!(Hash(payload)) + + events_worker.push(merged) + end + # @api private attr_reader :config # Configure the Honeybadger agent via Ruby. # @@ -435,11 +469,11 @@ ensure context_manager.set_rack_env(nil) end # @api private - attr_reader :worker + attr_reader :worker, :events_worker # @api private # @!method init!(...) # @see Config#init! def_delegators :config, :init! @@ -473,10 +507,16 @@ worker.send_now(object) true end def init_worker + return if @worker @worker = Worker.new(config) + end + + def init_events_worker + return if @events_worker + @events_worker = EventsWorker.new(config) end def with_error_handling yield rescue => ex