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

[VANotify] - Update VANotify::DefaultCallback to handle hash or array statsd_tags #20235

Merged
merged 8 commits into from
Jan 16, 2025
3 changes: 3 additions & 0 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,9 @@ features:
va_notify_custom_errors:
actor_type: user
description: Custom error classes instead of the generic Common::Exceptions::BackendServiceException
va_notify_metadata_statsd_tags:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized I wanted this behind a flipper toggle, so I added a new one. The spec diff is larger since I have a context for on/off, and we'll be able to delete the off side after we validate everything is fine in prod for it.

actor_type: user
description: Allow statsd tags in callback metadata to be a hash or array
va_online_scheduling:
actor_type: user
description: Allows veterans to view their VA and Community Care appointments
Expand Down
39 changes: 35 additions & 4 deletions modules/va_notify/lib/va_notify/default_callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ def call

def call_with_metadata
notification_type = metadata['notification_type']
statsd_tags = metadata['statsd_tags']
service = statsd_tags['service']
function = statsd_tags['function']
tags = ["service:#{service}", "function:#{function}"]

if Flipper.enabled?(:va_notify_metadata_statsd_tags)
tags = validate_and_normalize_statsd_tags
else
statsd_tags = metadata['statsd_tags']
service = statsd_tags['service']
function = statsd_tags['function']
tags = ["service:#{service}", "function:#{function}"]
end

case notification_record.status
when 'delivered'
Expand Down Expand Up @@ -64,5 +69,31 @@ def delivered_without_metadata
def permanent_failure_without_metadata
StatsD.increment('silent_failure', tags: ['service:none-provided', 'function:none-provided'])
end

def validate_and_normalize_statsd_tags
statsd_tags = metadata['statsd_tags']
required_keys = %w[service function]

tag_keys, tags = case statsd_tags
when Hash
keys = statsd_tags.keys
tags = statsd_tags.map { |key, value| "#{key}:#{value}" }
[keys, tags]
when Array
keys = statsd_tags.map { |tag| tag.split(':').first }
tags = statsd_tags
[keys, tags]
else
raise TypeError, 'Invalid metadata statsd_tags format: must be a Hash or Array'
coope93 marked this conversation as resolved.
Show resolved Hide resolved
end

missing_keys = required_keys - tag_keys
if missing_keys.any?
raise KeyError,
"Missing required keys in default_callback metadata statsd_tags: #{missing_keys.join(', ')}"
end

tags
end
end
end
Loading
Loading