Skip to content

Commit

Permalink
BGS: Fix bug returning wrong result when downstream BGS returns non-2…
Browse files Browse the repository at this point in the history
…xx response. (#3527)

Fix bug where create_claim_notes and create_veteran_notes are returning result of submitting metrics. Fixed noisy unit tests by mocking classes where necessary.
  • Loading branch information
dfitchett authored Sep 30, 2024
1 parent cf2dec3 commit 568fe72
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
8 changes: 6 additions & 2 deletions svc-bgs-api/src/lib/bgs_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ def create_claim_notes(claim_id:, notes:)
start_time = Time.now
metric_custom_tags = ['bgsNoteType:claim']
@metrics.submit_count_with_default_value(METRIC[:REQUEST_START], metric_custom_tags)
bgs.notes.create_notes(note_hashes)
response = bgs.notes.create_notes(note_hashes)
@metrics.submit_request_duration(start_time, Time.now, metric_custom_tags)
@metrics.submit_count_with_default_value(METRIC[:RESPONSE_COMPLETE], metric_custom_tags)

response
end

def create_veteran_note(note:, claim_id: nil, participant_id: nil)
Expand All @@ -91,8 +93,10 @@ def create_veteran_note(note:, claim_id: nil, participant_id: nil)
start_time = Time.now
metric_custom_tags = ['bgsNoteType:veteran']
@metrics.submit_count_with_default_value(METRIC[:REQUEST_START], metric_custom_tags)
bgs.notes.create_note(participant_id:, txt: note, user_id: vro_participant_id)
response = bgs.notes.create_note(participant_id:, txt: note, user_id: vro_participant_id)
@metrics.submit_request_duration(start_time, Time.now, metric_custom_tags)
@metrics.submit_count_with_default_value(METRIC[:RESPONSE_COMPLETE], metric_custom_tags)

response
end
end
13 changes: 7 additions & 6 deletions svc-bgs-api/src/lib/metric_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def initialize
begin
api_instance = DatadogAPIClient::V1::AuthenticationAPI.new
api_instance.validate
$logger.info('Succeeded Datadog authentication check')
rescue Exception => e
$logger.warn("Failed Datadog authentication check: #{e.message}")
end
Expand Down Expand Up @@ -81,9 +80,11 @@ def submit_count(metric, value, custom_tags)
rescue Exception => e
$logger.warn("Error logging metric: #{metric} (count). Error: #{e.class}, Message: #{e.message}")
end

nil
end

def submit_count_with_default_value(metric, custom_tags)
def submit_count_with_default_value(metric, custom_tags = nil)
submit_count(metric, 1, custom_tags)
end

Expand All @@ -100,15 +101,15 @@ def submit_request_duration(start_time, end_time, custom_tags = nil)
opts = {
content_encoding: DatadogAPIClient::V1::DistributionPointsContentEncoding::DEFLATE
}
payload_result = @distribution_metrics_api.submit_distribution_points(payload, opts)
$logger.info(
"submitted #{payload.series.first.metric} #{payload_result.status}"
)
@distribution_metrics_api.submit_distribution_points(payload, opts)
$logger.info("submitted #{payload.series.first.metric}")
rescue Exception => e
$logger.warn(
"exception submitting request duration #{e.message}"
)
end

nil
end

def generate_distribution_metric(metric, value, custom_tags = nil)
Expand Down
11 changes: 6 additions & 5 deletions svc-bgs-api/src/main_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def initialize_subscriber(bgs_client, metric_logger)
begin
bgs_client.handle_request(json)
rescue => e
begin
metric_logger.submit_count_with_default_value(METRIC[:RESPONSE_ERROR])
rescue => metric_e
$logger.error "Exception submitting metric RESPONSE_ERROR #{metric_e.message}"
end

status_code = e.is_a?(ArgumentError) ? 400 : 500
status_str = e.is_a?(ArgumentError) ? "BAD_REQUEST" : "INTERNAL_SERVER_ERROR"
{
Expand All @@ -58,11 +64,6 @@ def initialize_subscriber(bgs_client, metric_logger)
}
]
}
begin
metric_logger.submit_count_with_default_value(METRIC[:RESPONSE_ERROR], nil)
rescue => metric_e
$logger.error "Exception submitting metric RESPONSE_ERROR #{e.message}"
end
else
{
statusCode: 200,
Expand Down
5 changes: 5 additions & 0 deletions svc-bgs-api/src/spec/bgs_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

describe BgsClient do
let(:client) { BgsClient.new }
let(:metrics) { double("metrics") }
let(:notes_service) { double("notes_service") }

before do
allow(MetricLogger).to receive(:new).and_return(metrics)
allow(metrics).to receive(:submit_count_with_default_value).and_return(202)
allow(metrics).to receive(:submit_request_duration).and_return(202)

allow(BGS::Services).to receive(:new).and_return(double("bgs_services", notes: notes_service))
allow(client).to receive(:vro_participant_id).and_return("12345")
end
Expand Down
10 changes: 10 additions & 0 deletions svc-bgs-api/src/spec/metric_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

describe MetricLogger do
let(:client) { MetricLogger.new }
let(:api_instance) { double("api_instance") }
let(:metrics) { double("metrics") }
let(:distributions) { double("distributions") }

before do
allow(DatadogAPIClient::V1::AuthenticationAPI).to receive(:new).and_return(api_instance)
allow(api_instance).to receive(:validate).and_return(true)
allow(DatadogAPIClient::V1::MetricsAPI).to receive(:new).and_return(metrics)
allow(DatadogAPIClient::V2::MetricsAPI).to receive(:new).and_return(distributions)
end

it 'generates standard tags when no custom tags are specified' do
tags = client.generate_tags
Expand Down

0 comments on commit 568fe72

Please sign in to comment.