diff --git a/lib/statsd_middleware.rb b/lib/statsd_middleware.rb index e02d7d1a1cf..4ac72528d91 100644 --- a/lib/statsd_middleware.rb +++ b/lib/statsd_middleware.rb @@ -52,6 +52,7 @@ class StatsdMiddleware veteran-id-card veteran-representative vic-v2 + undefined ].freeze def initialize(app) @@ -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) diff --git a/spec/request/statsd_middleware_spec.rb b/spec/request/statsd_middleware_spec.rb index 41cde81cf7a..095868de179 100644 --- a/spec/request/statsd_middleware_spec.rb +++ b/spec/request/statsd_middleware_spec.rb @@ -32,7 +32,7 @@ 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) @@ -40,7 +40,7 @@ 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) @@ -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) @@ -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)