Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implements honeybadger.event by synchronous log call #512

Merged
merged 14 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
rubygems: latest

- name: Build and test regular ruby
run: |
Expand Down
39 changes: 36 additions & 3 deletions lib/honeybadger/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'honeybadger/plugin'
require 'honeybadger/logging'
require 'honeybadger/worker'
require 'honeybadger/events_worker'
require 'honeybadger/breadcrumbs'

module Honeybadger
Expand Down Expand Up @@ -74,7 +75,7 @@ def initialize(opts = {})
@breadcrumbs = nil
end

init_worker
init_workers
end

# Sends an exception to Honeybadger. Does not report ignored exceptions by
Expand Down Expand Up @@ -354,6 +355,7 @@ def flush
yield
ensure
worker.flush
events_worker.flush
end

# Stops the Honeybadger service.
Expand All @@ -362,9 +364,39 @@ def flush
# 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 = {})
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

Expand Down Expand Up @@ -437,7 +469,7 @@ def with_rack_env(rack_env, &block)
end

# @api private
attr_reader :worker
attr_reader :worker, :events_worker

# @api private
# @!method init!(...)
Expand Down Expand Up @@ -474,8 +506,9 @@ def send_now(object)
true
end

def init_worker
def init_workers
@worker = Worker.new(config)
@events_worker = EventsWorker.new(config)
end

def with_error_handling
Expand Down
10 changes: 10 additions & 0 deletions lib/honeybadger/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def track_deployment(payload)
notify(:deploys, payload)
end

# Send event
# @example
# backend.event([{event_type: "email_received", ts: "2023-03-04T12:12:00+1:00", subject: 'Re: Aquisition' }})
#
# @param [Array] payload array of event hashes to send
# @raise NotImplementedError
def event(payload)
raise NotImplementedError, "must define #event on subclass"
end

private

attr_reader :config
Expand Down
6 changes: 6 additions & 0 deletions lib/honeybadger/backend/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def check_in(id)
return Response.new(ENV['DEBUG_BACKEND_STATUS'].to_i, nil) if ENV['DEBUG_BACKEND_STATUS']
super
end

def event(payload)
logger.unknown("sending event to debug backend with event=#{payload.to_json}")
return Response.new(ENV['DEBUG_BACKEND_STATUS'].to_i, nil) if ENV['DEBUG_BACKEND_STATUS']
super
end
end
end
end
4 changes: 4 additions & 0 deletions lib/honeybadger/backend/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def notify(feature, payload)
def check_in(id)
StubbedResponse.new
end

def event(payload)
StubbedResponse.new
end
end
end
end
17 changes: 14 additions & 3 deletions lib/honeybadger/backend/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ module Backend
class Server < Base
ENDPOINTS = {
notices: '/v1/notices'.freeze,
deploys: '/v1/deploys'.freeze
deploys: '/v1/deploys'.freeze,
}.freeze

CHECK_IN_ENDPOINT = '/v1/check_in'.freeze

EVENTS_ENDPOINT = '/v1/events'.freeze

HTTP_ERRORS = Util::HTTP::ERRORS

Expand Down Expand Up @@ -48,6 +47,18 @@ def check_in(id)
Response.new(:error, nil, "HTTP Error: #{e.class}")
end

# Send event
# @example
# backend.event([{event_type: "email_received", ts: "2023-03-04T12:12:00+1:00", subject: 'Re: Aquisition' }})
#
# @param [Array] payload array of event hashes to send
# @return [Response]
def event(payload)
Response.new(@http.post_newline_delimited(EVENTS_ENDPOINT, payload))
rescue *HTTP_ERRORS => e
Response.new(:error, nil, "HTTP Error: #{e.class}")
end

private

def payload_headers(payload)
Expand Down
10 changes: 10 additions & 0 deletions lib/honeybadger/config/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ class Boolean; end
default: 100,
type: Integer
},
:'events.batch_size' => {
description: 'Send events batch if n events have accumulated',
default: 100,
type: Integer
},
:'events.timeout' => {
description: 'Timeout after which the events batch will be sent regardless (in milliseconds)',
default: 30_000,
type: Integer
},
plugins: {
description: 'An optional list of plugins to load. Default is to load all plugins.',
default: nil,
Expand Down
Loading