Skip to content

Commit

Permalink
Merge pull request #12476 from vpereira/instrument_notification
Browse files Browse the repository at this point in the history
Add instrumentation to track changes on Notification#delivered
  • Loading branch information
saraycp committed Apr 22, 2022
2 parents e0a1d52 + de424d5 commit 80edd61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Notification < ApplicationRecord

after_create :track_notification_creation

after_save :track_notification_delivered, if: :saved_change_to_delivered?

scope :for_web, -> { where(web: true) }
scope :for_rss, -> { where(rss: true) }

Expand Down Expand Up @@ -51,6 +53,11 @@ def track_notification_creation
RabbitmqBus.send_to_bus('metrics',
"notification.create,notifiable_type=#{notifiable_type},web=#{web},rss=#{rss} value=1")
end

def track_notification_delivered
RabbitmqBus.send_to_bus('metrics',
"notification.delivered,notifiable_type=#{notifiable_type},web=#{web},rss=#{rss} value=1")
end
end

# == Schema Information
Expand Down
31 changes: 31 additions & 0 deletions src/api/spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,35 @@
it { expect(subject).to be_truthy }
end
end

describe 'Instrumentation' do
let!(:test_user) { create(:confirmed_user, login: 'foo') }
let!(:rss_notification) { create(:rss_notification, subscriber: test_user) }

before do
allow(RabbitmqBus).to receive(:send_to_bus).with('metrics', 'notification.delivered,notifiable_type=,web=false,rss=true value=1')
end

context 'if delivered change, we should track it' do
before do
rss_notification.delivered = true
end

it do
rss_notification.save
expect(RabbitmqBus).to have_received(:send_to_bus).with('metrics', 'notification.delivered,notifiable_type=,web=false,rss=true value=1')
end
end

context 'if delivered doe not change, we should not track it' do
before do
rss_notification.title = 'FOO FOO'
end

it do
rss_notification.save
expect(RabbitmqBus).not_to have_received(:send_to_bus).with('metrics', 'notification.delivered,notifiable_type=,web=false,rss=true value=1')
end
end
end
end

0 comments on commit 80edd61

Please sign in to comment.