File tree 3 files changed +51
-5
lines changed
3 files changed +51
-5
lines changed Original file line number Diff line number Diff line change @@ -42,13 +42,17 @@ def check_ins
42
42
self . class . check_ins
43
43
end
44
44
45
+ def events
46
+ self . class . events
47
+ end
48
+
45
49
def notify ( feature , payload )
46
50
notifications [ feature ] << payload
47
51
super
48
52
end
49
53
50
54
def event ( payload )
51
- events << payload
55
+ events . concat ( payload . dup )
52
56
super
53
57
end
54
58
Original file line number Diff line number Diff line change @@ -6,17 +6,16 @@ class NotificationSubscriber
6
6
include Honeybadger ::InstrumentationHelper
7
7
8
8
def start ( name , id , payload )
9
- @start_time = ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
9
+ payload [ :_start_time ] = ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
10
10
end
11
11
12
12
def finish ( name , id , payload )
13
- @finish_time = ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
14
-
13
+ finish_time = ::Process . clock_gettime ( ::Process ::CLOCK_MONOTONIC )
15
14
return unless process? ( name , payload )
16
15
17
16
payload = {
18
17
instrumenter_id : id ,
19
- duration : ( ( @ finish_time - @start_time ) * 1000 ) . round ( 2 )
18
+ duration : ( ( finish_time - payload . delete ( :_start_time ) ) * 1000 ) . round ( 2 )
20
19
} . merge ( format_payload ( payload ) . compact )
21
20
22
21
record ( name , payload )
Original file line number Diff line number Diff line change
1
+ require_relative "../rails_helper"
2
+ require "honeybadger/notification_subscriber"
3
+
4
+ class TestSubscriber < Honeybadger ::NotificationSubscriber ; end
5
+
6
+ describe "Rails Insights Notification Subscribers" , if : RAILS_PRESENT , type : :request do
7
+ load_rails_hooks ( self )
8
+
9
+ before do
10
+ Honeybadger . config [ :"insights.enabled" ] = true
11
+ Honeybadger . config [ :"events.batch_size" ] = 0
12
+
13
+ Honeybadger ::Backend ::Test . events . clear
14
+ end
15
+
16
+ it "records correct durations for concurrent notifications" do
17
+ mutex , sequence = Mutex . new , 1
18
+ allow ( Process ) . to receive ( :clock_gettime ) . with ( Process ::CLOCK_MONOTONIC ) do
19
+ if caller_locations . any? { |loc | loc . base_label == "finish" }
20
+ 10
21
+ else
22
+ mutex . synchronize { sequence += 1 }
23
+ end
24
+ end
25
+
26
+ ActiveSupport ::Notifications . subscribe ( "test.timing" , TestSubscriber . new )
27
+
28
+ # Create multiple threads that will fire notifications
29
+ threads = 5 . times . map do
30
+ Thread . new do
31
+ ActiveSupport ::Notifications . instrument ( "test.timing" ) do
32
+ sleep ( 0.1 )
33
+ end
34
+ end
35
+ end
36
+
37
+ threads . each ( &:join )
38
+ # Wait for all events to be processed
39
+ sleep ( 0.2 )
40
+
41
+ expect ( Honeybadger ::Backend ::Test . events . map { |e | e [ :duration ] } . uniq . length ) . to eq ( 5 )
42
+ end
43
+ end
You can’t perform that action at this time.
0 commit comments