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

aedara/APPEALS-26087 Setup monitoring for deprecation warnings #19078

Merged
merged 16 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
43 changes: 43 additions & 0 deletions config/initializers/deprecation_warning_subscriber.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# @note For use in conjuction with setting `Rails.application.config.active_support.deprecation = :notify`.
# Whenever a “deprecation.rails” notification is published, it will dispatch the event
# (ActiveSupport::Notifications::Event) to method #deprecation.
class DeprecationWarningSubscriber < ActiveSupport::Subscriber
APP_NAME = "caseflow"
SLACK_ALERT_CHANNEL = "#appeals-deprecation-alerts"

attach_to :rails

def deprecation(event)
emit_warning_to_application_logs(event)
emit_warning_to_sentry(event)
emit_warning_to_slack_alerts_channel(event)
end

private

def emit_warning_to_application_logs(event)
Rails.logger.warn(event.payload[:message])
end

def emit_warning_to_sentry(event)
Raven.capture_message(
event.payload[:message],
level: "warning",
extra: {
message: event.payload[:message],
callstack: event.payload[:callstack],
environment: Rails.env
}
)
end

def emit_warning_to_slack_alerts_channel(event)
slack_alert_title = "Deprecation Warning - #{APP_NAME} (#{ENV['DEPLOY_ENV']})"

SlackService
.new(url: ENV["SLACK_DISPATCH_ALERT_URL"])
.send_notification(event.payload[:message], slack_alert_title, SLACK_ALERT_CHANNEL)
end
end
49 changes: 49 additions & 0 deletions spec/initializers/deprecation_warning_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

describe "DeprecationWarningSubscriber" do
let(:rails_logger) { Rails.logger }
let(:slack_service) { SlackService.new(url: "dummy-url") }

before do
allow(Rails).to receive(:logger).and_return(rails_logger)
allow(rails_logger).to receive(:warn)

allow(Raven).to receive(:capture_message)

allow(SlackService).to receive(:new).with(url: anything).and_return(slack_service)
allow(slack_service).to receive(:send_notification)
end

context "when a 'deprecation.rails' event is instrumented" do
let(:app_name) { "caseflow" }
let(:deploy_env) { "test" }
let(:payload) { { message: "test message", callstack: "test callstack" } }

before { ActiveSupport::Notifications.instrument("deprecation.rails", payload) }

it "emits a warning to the application logs" do
expect(rails_logger).to have_received(:warn).with(payload[:message])
end

it "emits a warning to Sentry" do
expect(Raven).to have_received(:capture_message).with(
payload[:message],
level: "warning",
extra: {
message: payload[:message],
callstack: payload[:callstack],
environment: Rails.env
}
)
end

it "emits a warning to Slack channel" do
slack_alert_title = "Deprecation Warning - #{app_name} (#{deploy_env})"
expect(slack_service).to have_received(:send_notification).with(
payload[:message],
slack_alert_title,
"#appeals-deprecation-alerts"
)
end
end
end
Loading