Skip to content

Commit

Permalink
explicit strings to source_app errors instead of blank strings (#3706)
Browse files Browse the repository at this point in the history
  • Loading branch information
omgitsbillryan authored Dec 27, 2019
1 parent 7606772 commit 0ac4cc2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/statsd_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class StatsdMiddleware
veteran-id-card
veteran-representative
vic-v2
undefined
].freeze

def initialize(app)
Expand Down Expand Up @@ -94,14 +95,14 @@ def call(env)
def get_source_app(env)
source_app = env['HTTP_SOURCE_APP_NAME']

return nil if source_app.nil?
return 'not_provided' if source_app.nil?
return source_app if SOURCE_APP_NAMES.include?(source_app)

# TODO: - Use sentry to notify us instead. It must be done in a rate-limited way
# so as not to allow for a malicious client to overflow worker queues
Rails.logger.warn "Unrecognized value for HTTP_SOURCE_APP_NAME request header... [#{source_app}]"

''
'not_in_whitelist'
end

def instrument_statsd(status, duration, controller, action, source_app)
Expand Down
20 changes: 14 additions & 6 deletions spec/request/statsd_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@

it 'sends status data to statsd' do
stub_varx_request(:get, 'mhv-api/patient/v1/prescription/gethistoryrx', history_rxs, status_code: 200)
tags = %w[controller:v0/prescriptions action:index source_app: status:200]
tags = %w[controller:v0/prescriptions action:index source_app:not_provided status:200]
expect do
get '/v0/prescriptions'
end.to trigger_statsd_increment(StatsdMiddleware::STATUS_KEY, tags: tags, times: 1, value: 1)
end

it 'sends duration data to statsd' do
stub_varx_request(:get, 'mhv-api/patient/v1/prescription/gethistoryrx', history_rxs, status_code: 200)
tags = %w[controller:v0/prescriptions action:index source_app:]
tags = %w[controller:v0/prescriptions action:index source_app:not_provided]
expect do
get '/v0/prescriptions'
end.to trigger_statsd_measure(StatsdMiddleware::DURATION_KEY, tags: tags, times: 1, value: 0.0)
Expand All @@ -63,14 +63,14 @@
end

it 'handles a missing route correctly' do
tags = %w[controller:application action:routing_error source_app: status:404]
tags = %w[controller:application action:routing_error source_app:not_provided status:404]
expect do
get '/v0/blahblah'
end.to trigger_statsd_increment(StatsdMiddleware::STATUS_KEY, tags: tags, times: 1, value: 1)
end

it 'provides duration for missing routes' do
tags = %w[controller:application action:routing_error source_app:]
tags = %w[controller:application action:routing_error source_app:not_provided]
expect do
get '/v0/blahblah'
end.to trigger_statsd_measure(StatsdMiddleware::DURATION_KEY, tags: tags, times: 1, value: 0.0)
Expand All @@ -84,9 +84,17 @@
end.to trigger_statsd_increment(StatsdMiddleware::STATUS_KEY, tags: tags, times: 1)
end

it 'uses a blank string for source_app when the value is not in white list' do
it 'sends undefined to statsd when source_app is undefined' do
stub_varx_request(:get, 'mhv-api/patient/v1/prescription/gethistoryrx', history_rxs, status_code: 200)
tags = %w[controller:v0/prescriptions action:index source_app: status:200]
tags = %w[controller:v0/prescriptions action:index source_app:undefined status:200]
expect do
get '/v0/prescriptions', headers: { 'Source-App-Name' => 'undefined' }
end.to trigger_statsd_increment(StatsdMiddleware::STATUS_KEY, tags: tags, times: 1)
end

it 'uses not_in_whitelist for source_app when the value is not in white list' do
stub_varx_request(:get, 'mhv-api/patient/v1/prescription/gethistoryrx', history_rxs, status_code: 200)
tags = %w[controller:v0/prescriptions action:index source_app:not_in_whitelist status:200]
expect do
get '/v0/prescriptions', headers: { 'Source-App-Name' => 'foo' }
end.to trigger_statsd_increment(StatsdMiddleware::STATUS_KEY, tags: tags, times: 1)
Expand Down

0 comments on commit 0ac4cc2

Please sign in to comment.