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

Payload enhancements #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions lib/dry/monitor/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ def stop(event_id, payload)
end

def instrument(event_id, payload = EMPTY_HASH)
result, time = @clock.measure { yield } if block_given?

result, time = @clock.measure { yield payload } if block_given?
# We should always try to instrument, even the system-level exceptions
rescue Exception => e # rubocop:disable Lint/RescueException
payload = {} if payload.equal?(EMPTY_HASH)
payload[:exception] = e
raise
ensure
process(event_id, payload) do |event, listener|
if time
listener.(event.payload(payload.merge(time: time)))
Expand Down
13 changes: 6 additions & 7 deletions lib/dry/monitor/rack/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ class Logger
QUERY_MSG = %( Query parameters )
FILTERED = "[FILTERED]"

attr_reader :logger

attr_reader :config
attr_reader :logger, :config

def initialize(logger, config = self.class.config)
@logger = logger
@config = config
end

def attach(rack_monitor)
rack_monitor.on(:start) do |env:|
log_start_request(env)
rack_monitor.on(:start) do |event|
log_start_request(event.payload[:env])
end

rack_monitor.on(:stop) do |env:, status:, time:|
log_stop_request(env, status, time)
rack_monitor.on(:stop) do |event|
request, status, time = event.payload.values_at(:env, :status, :time)
log_stop_request(request, status, time)
end

rack_monitor.on(:error) do |event|
Expand Down
10 changes: 4 additions & 6 deletions lib/dry/monitor/sql/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def colorizer
setting :theme, nil
setting :message_template, %( Loaded %s in %sms %s)

attr_reader :config
attr_reader :logger
attr_reader :template
attr_reader :config, :logger, :template

load_extensions(:default_colorizer)

Expand All @@ -53,12 +51,12 @@ def initialize(logger, config = self.class.config)
end

def subscribe(notifications)
notifications.subscribe(:sql) do |time:, name:, query:|
log_query(time, name, query)
notifications.subscribe(:sql) do |event|
log_query(**event.payload)
end
end

def log_query(time, name, query)
def log_query(time:, name:, query:)
logger.info template % [name.inspect, time, colorizer.call(query)]
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/integration/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,44 @@

expect(captured).to eql([[:sql]])
end

it 'yields the payload to the instrumented block' do
captured = []

notifications.subscribe(:sql) do |event|
captured << event
end

notifications.instrument(:sql, outside_block: true) do |payload|
payload[:inside_block] = true
end

expect(captured[0].payload).to match hash_including(
outside_block: true, inside_block: true
)
end

MyError = Class.new(StandardError)
it 'still notifies when the instrumented block raises an exception' do
captured = []

notifications.subscribe(:sql) do |event|
captured << event
end

expect {
notifications.instrument(:sql) do
@line = __LINE__ + 1
raise MyError
end
}.to raise_error(MyError)

expect(captured).to_not be_empty
exception = captured.first.payload[:exception]
expect(exception).to match kind_of(MyError)
# verify the exception backtrace comes from the instrumented code, not
# the `#instrument` method
expect(exception.backtrace.first).to start_with("#{__FILE__}:#{@line}")
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "dry-monitor"
Dry::Monitor.load_extensions(:sql, :rack)

require 'pathname'
SPEC_ROOT = Pathname(__dir__)

Dir[SPEC_ROOT.join("shared/**/*.rb")].each(&method(:require))
Expand Down