-
Notifications
You must be signed in to change notification settings - Fork 62
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
91828: Create general monitor classes #19078
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Logging | ||
class Monitor < ::ZeroSilentFailures::Monitor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably should not inherit the ZSF monitor. try and keep this a module/include/mixin with standalone functions |
||
def initialize(service) | ||
@service = service | ||
super(@service) | ||
end | ||
|
||
## | ||
# log GET request | ||
# | ||
# @param message [String] | ||
# @param metric [String] | ||
# @param form_type [String] | ||
# @param user_account_uuid [User] | ||
# | ||
def track_request(message, metric, form_type = 'Unknown Form Type', user_account_uuid = nil, call_location: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of |
||
function, file, line = parse_caller(call_location) | ||
|
||
StatsD.increment(metric) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we leave the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For these functions I would let the metric be passed to the function. Also, possibly the 'log level' [info, warn, error] unless you want to split out or specify that it will log as an error in the function name. |
||
Rails.logger.error("#{form_type} #{message}", | ||
{ | ||
statsd: metric, | ||
user_account_uuid:, | ||
function:, | ||
file:, | ||
line: | ||
}) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module Logging | ||
class Sidekiq < Monitor | ||
def initialize(service) | ||
@service = service | ||
super(@service) | ||
end | ||
|
||
# ## | ||
# log info request | ||
# | ||
# @param message [String] | ||
# @param metric [String] | ||
# @param claim [SavedClaim] | ||
# @param benefits_intake_uuid [FormSubmissions] | ||
# @param user_account_uuid [User] | ||
# @param additional_context [Hash] | ||
# | ||
# rubocop:disable Metrics/ParameterLists | ||
def track_claim_submission(message, metric, claim, benefits_intake_uuid, | ||
user_account_uuid, additional_context, call_location: nil) | ||
function, file, line = parse_caller(call_location) | ||
|
||
StatsD.increment(metric) | ||
Rails.logger.info(message.to_s, | ||
{ | ||
statsd: metric, | ||
user_account_uuid:, | ||
claim_id: claim&.id, | ||
benefits_intake_uuid: benefits_intake_uuid, | ||
confirmation_number: claim&.confirmation_number, | ||
additional_context:, | ||
function:, | ||
file:, | ||
line: | ||
}) | ||
end | ||
|
||
# ## | ||
# log warn request | ||
# | ||
# @param message [String] | ||
# @param metric [String] | ||
# @param claim [SavedClaim] | ||
# @param benefits_intake_uuid [FormSubmissions] | ||
# @param user_account_uuid [User] | ||
# @param additional_context [Hash] | ||
# | ||
def track_claim_submission_warn(message, metric, claim, benefits_intake_uuid, | ||
user_account_uuid, additional_context, call_location: nil) | ||
function, file, line = parse_caller(call_location) | ||
|
||
StatsD.increment(metric) | ||
Rails.logger.warn(message.to_s, | ||
{ | ||
statsd: metric, | ||
user_account_uuid:, | ||
claim_id: claim&.id, | ||
benefits_intake_uuid: benefits_intake_uuid, | ||
confirmation_number: claim&.confirmation_number, | ||
additional_context:, | ||
function:, | ||
file:, | ||
line: | ||
}) | ||
end | ||
|
||
# ## | ||
# log error request | ||
# | ||
# @param message [String] | ||
# @param metric [String] | ||
# @param claim [SavedClaim] | ||
# @param benefits_intake_uuid [FormSubmissions] | ||
# @param user_account_uuid [User] | ||
# @param additional_context [Hash] | ||
# | ||
def track_claim_submission_error(message, metric, claim, benefits_intake_uuid, | ||
user_account_uuid, additional_context, call_location: nil) | ||
function, file, line = parse_caller(call_location) | ||
|
||
StatsD.increment(metric) | ||
Rails.logger.error(message.to_s, | ||
{ | ||
statsd: metric, | ||
user_account_uuid:, | ||
claim_id: claim&.id, | ||
benefits_intake_uuid: benefits_intake_uuid, | ||
confirmation_number: claim&.confirmation_number, | ||
additional_context:, | ||
function:, | ||
file:, | ||
line: | ||
}) | ||
end | ||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,10 +63,6 @@ def log_silent_failure_avoided(additional_context, user_account_uuid = nil, call | |
}) | ||
end | ||
|
||
private | ||
|
||
attr_reader :service | ||
|
||
# parse information from the `caller` | ||
# | ||
# @see https://alextaylor.ca/read/caller-tricks/ | ||
|
@@ -78,5 +74,9 @@ def parse_caller(call_location) | |
call_location ||= caller_locations.second # default to location calling 'log_silent_failure...' | ||
[call_location.base_label, call_location.path, call_location.lineno] | ||
end | ||
|
||
private | ||
|
||
attr_reader :service | ||
Comment on lines
+78
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inherited private functions should be callable from the subclass |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'zero_silent_failures/monitor' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this needed here? |
||
require 'logging/monitor' | ||
|
||
RSpec.describe Logging::Monitor do | ||
let(:service) { 'test-application' } | ||
let(:monitor) { described_class.new(service) } | ||
let(:call_location) { double('Location', base_label: 'method_name', path: '/path/to/file.rb', lineno: 42) } | ||
let(:metric) { 'api.monitor.404' } | ||
let(:form_type) { '21P-50EZ' } | ||
let(:user_account_uuid) { '123-test-uuid' } | ||
let(:payload) do | ||
{ | ||
statsd: 'OVERRIDE', | ||
user_account_uuid:, | ||
function: call_location.base_label, | ||
file: call_location.path, | ||
line: call_location.lineno | ||
} | ||
end | ||
|
||
context 'with a call location provided' do | ||
describe '#track_request' do | ||
it 'logs a request with call location' do | ||
payload[:statsd] = 'api.monitor.404' | ||
|
||
expect(StatsD).to receive(:increment).with('api.monitor.404') | ||
expect(Rails.logger).to receive(:error).with('21P-50EZ 404 Not Found!', payload) | ||
|
||
monitor.track_request('404 Not Found!', metric, form_type, user_account_uuid, call_location:) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'zero_silent_failures/monitor' | ||
require 'logging/monitor' | ||
require 'logging/sidekiq' | ||
|
||
RSpec.describe Logging::Sidekiq do | ||
let(:service) { 'test-application' } | ||
let(:monitor) { described_class.new(service) } | ||
let(:call_location) { double('Location', base_label: 'method_name', path: '/path/to/file.rb', lineno: 42) } | ||
let(:metric) { 'api.monitor.sidekiq' } | ||
let(:user_account_uuid) { '123-test-uuid' } | ||
let(:benefits_intake_uuid) { '123-test-uuid' } | ||
let(:additional_context) { { file: 'foobar.pdf', attachments: ['file.txt', 'file2.txt'] } } | ||
let(:claim) do | ||
FactoryBot.create( | ||
:pensions_module_pension_claim, | ||
form: { | ||
veteranFullName: { | ||
first: 'Test', | ||
last: 'User' | ||
}, | ||
email: 'foo@foo.com', | ||
veteranDateOfBirth: '1989-12-13', | ||
veteranSocialSecurityNumber: '111223333', | ||
veteranAddress: { | ||
country: 'USA', | ||
state: 'CA', | ||
postalCode: '90210', | ||
street: '123 Main St', | ||
city: 'Anytown' | ||
}, | ||
statementOfTruthCertified: true, | ||
statementOfTruthSignature: 'Test User' | ||
}.to_json | ||
) | ||
Comment on lines
+17
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to avoid using a specific form to test (i recently added a basic generic SaveClaim factory in my pr) |
||
end | ||
let(:payload) do | ||
{ | ||
statsd: 'OVERRIDE', | ||
user_account_uuid: user_account_uuid, | ||
claim_id: claim&.id, | ||
benefits_intake_uuid: benefits_intake_uuid, | ||
confirmation_number: claim&.confirmation_number, | ||
additional_context: additional_context, | ||
function: call_location.base_label, | ||
file: call_location.path, | ||
line: call_location.lineno | ||
} | ||
end | ||
|
||
context 'with a call location provided' do | ||
describe '#track_claim_submission' do | ||
it 'logs a request with call location' do | ||
payload[:statsd] = 'api.monitor.sidekiq' | ||
|
||
expect(StatsD).to receive(:increment).with('api.monitor.sidekiq') | ||
expect(Rails.logger).to receive(:info).with('Lighthouse::Job submission to LH attempted', payload) | ||
|
||
monitor.track_claim_submission('Lighthouse::Job submission to LH attempted', metric, claim, | ||
benefits_intake_uuid, user_account_uuid, additional_context, call_location:) | ||
end | ||
end | ||
|
||
describe '#track_claim_submission_warn' do | ||
it 'logs a request with call location' do | ||
payload[:statsd] = 'api.monitor.sidekiq' | ||
|
||
expect(StatsD).to receive(:increment).with('api.monitor.sidekiq') | ||
expect(Rails.logger).to receive(:warn).with('Lighthouse::Job submission to LH failure', payload) | ||
|
||
monitor.track_claim_submission_warn('Lighthouse::Job submission to LH failure', metric, claim, | ||
benefits_intake_uuid, user_account_uuid, additional_context, call_location:) | ||
end | ||
end | ||
|
||
describe '#track_claim_submission_error' do | ||
it 'logs a request with call location' do | ||
payload[:statsd] = 'api.monitor.sidekiq' | ||
|
||
expect(StatsD).to receive(:increment).with('api.monitor.sidekiq') | ||
expect(Rails.logger).to receive(:error).with('Lighthouse::Job submission to LH exhausted!', payload) | ||
|
||
monitor.track_claim_submission_error('Lighthouse::Job submission to LH exhausted!', metric, claim, | ||
benefits_intake_uuid, user_account_uuid, | ||
additional_context, call_location:) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this cause issues since there would now be 2 constructors? and also a double include/inherit