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

fix: ensure instrumentation durations are unique during threaded executions #658

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
6 changes: 5 additions & 1 deletion lib/honeybadger/backend/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ def check_ins
self.class.check_ins
end

def events
self.class.events
end

def notify(feature, payload)
notifications[feature] << payload
super
end

def event(payload)
events << payload
events.concat(payload.dup)
super
end

Expand Down
7 changes: 3 additions & 4 deletions lib/honeybadger/notification_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ class NotificationSubscriber
include Honeybadger::InstrumentationHelper

def start(name, id, payload)
@start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
payload[:_start_time] = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end

def finish(name, id, payload)
@finish_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)

finish_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
return unless process?(name, payload)

payload = {
instrumenter_id: id,
duration: ((@finish_time - @start_time) * 1000).round(2)
duration: ((finish_time - payload.delete(:_start_time)) * 1000).round(2)
}.merge(format_payload(payload).compact)

record(name, payload)
Expand Down
43 changes: 43 additions & 0 deletions spec/integration/rails/notification_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative "../rails_helper"
require "honeybadger/notification_subscriber"

class TestSubscriber < Honeybadger::NotificationSubscriber; end

describe "Rails Insights Notification Subscribers", if: RAILS_PRESENT, type: :request do
load_rails_hooks(self)

before do
Honeybadger.config[:"insights.enabled"] = true
Honeybadger.config[:"events.batch_size"] = 0

Honeybadger::Backend::Test.events.clear
end

it "records correct durations for concurrent notifications" do
mutex, sequence = Mutex.new, 1
allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC) do
if caller_locations.any? { |loc| loc.base_label == "finish" }
10
else
mutex.synchronize { sequence += 1 }
end
end

ActiveSupport::Notifications.subscribe("test.timing", TestSubscriber.new)

# Create multiple threads that will fire notifications
threads = 5.times.map do
Thread.new do
ActiveSupport::Notifications.instrument("test.timing") do
sleep(0.1)
end
end
end

threads.each(&:join)
# Wait for all events to be processed
sleep(0.2)

expect(Honeybadger::Backend::Test.events.map { |e| e[:duration] }.uniq.length).to eq(5)
end
end
Loading