Skip to content

Commit

Permalink
Merge pull request #12494 from vpereira/instrument_event_subscription…
Browse files Browse the repository at this point in the history
…_enabled_field

Add instrumentation to EventSubscription#enabled
  • Loading branch information
Dany Marcoux committed Apr 27, 2022
2 parents f275cb5 + 36e7639 commit 60b438b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/app/models/event_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class EventSubscription < ApplicationRecord
end
}

after_save :track_event_subscription_enabled, if: :saved_change_to_enabled?

def self.receiver_roles_to_display(user)
roles = RECEIVER_ROLE_TEXTS.keys
# We have to show the new_watchlist subscription options to the admin in order to allow the global
Expand Down Expand Up @@ -100,6 +102,11 @@ def parameters_for_notification
def self.without_disabled_or_internal_channels
channels.keys.reject { |channel| channel == 'disabled' || channel.in?(INTERNAL_ONLY_CHANNELS) }
end

def track_event_subscription_enabled
RabbitmqBus.send_to_bus('metrics',
"event_subscription.enabled,event_type=#{eventtype},channel=#{channel} value=1")
end
end

# == Schema Information
Expand Down
41 changes: 41 additions & 0 deletions src/api/spec/models/event_subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

RSpec.describe EventSubscription, type: :model do
describe 'Instrumentation' do
let(:user) { create(:confirmed_user, :with_home, login: 'cameron') }
let(:token) { create(:workflow_token, user: user) }

let!(:target_project) { create(:project, name: 'test-target-project:openSUSE:open-build-service:PR-4', maintainer: user) }
let!(:target_package) { create(:package, name: 'test-target-package', project: target_project) }

let(:event_type) { 'Event::BuildFail' }

let(:event_subscription) { EventSubscription.create!(channel: 'scm', token: token, receiver_role: 'maintainer', eventtype: event_type, package: target_package) }

before do
allow(RabbitmqBus).to receive(:send_to_bus).with('metrics', "event_subscription.enabled,event_type=#{event_type},channel=scm value=1")
end

describe '#enabled was changed' do
before do
event_subscription.enabled = true
end

it do
event_subscription.save
expect(RabbitmqBus).to have_received(:send_to_bus).with('metrics', "event_subscription.enabled,event_type=#{event_type},channel=scm value=1")
end
end

describe '#enabled wasnt changed' do
before do
event_subscription.channel = 'disabled'
end

it do
event_subscription.save
expect(RabbitmqBus).not_to have_received(:send_to_bus).with('metrics', "event_subscription.enabled,event_type=#{event_type},channel=scm value=1")
end
end
end
end

0 comments on commit 60b438b

Please sign in to comment.