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

Update Statsd tag serializer to allow falsy values #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/datadog/statsd/serialization/tag_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def to_tags_list(tags)
case tags
when Hash
tags.map do |name, value|
if value
escape_tag_content("#{name}:#{value}")
else
if value&.to_s.nil?
escape_tag_content(name)
else
escape_tag_content("#{name}:#{value}")
end
end
when Array
Expand All @@ -75,7 +75,7 @@ def to_tags_list(tags)
def escape_tag_content(tag)
tag = tag.to_s
return tag unless tag.include?('|')
tag.delete('|,')
tag.delete('|,')
Copy link
Author

Choose a reason for hiding this comment

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

Looks like my editor chose to remove some whitespace here!

end

def dd_tags(env = ENV)
Expand Down
14 changes: 14 additions & 0 deletions spec/statsd/serialization/tag_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@
expect(subject.format([tag])).to eq 'node:storage'
end

it 'ignores nil values and formats all other falsy values correctly' do
message_tags_hash = {
empty_array: [],
missing: nil,
'empty_hash' => {},
blank: '',
:false_value => false,
'results in blank': double('some tag', to_s: ''),
'results in nil': double('some tag', to_s: nil),
}

expect(subject.format(message_tags_hash)).to eq 'empty_array:[],missing,empty_hash:{},blank:,false_value:false,results in blank:,results in nil'
end

it 'formats frozen tags correctly' do
expect(subject.format(['name:foobarfoo'.freeze])).to eq 'name:foobarfoo'
end
Expand Down