Skip to content

Commit 0c67365

Browse files
committed
Update Statsd tag serializer to allow falsy values
This instead now ensures that we skip including the tag value only if: - it is equal to 'nil' - it when stringified is equal to 'nil' The reasoning for this was to make it so falsy values (e.g. false) will now be included in our tag content, instead of skipped.
1 parent 694f421 commit 0c67365

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/datadog/statsd/serialization/tag_serializer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def to_tags_list(tags)
5959
case tags
6060
when Hash
6161
tags.map do |name, value|
62-
if value
63-
escape_tag_content("#{name}:#{value}")
64-
else
62+
if value&.to_s.nil?
6563
escape_tag_content(name)
64+
else
65+
escape_tag_content("#{name}:#{value}")
6666
end
6767
end
6868
when Array
@@ -75,7 +75,7 @@ def to_tags_list(tags)
7575
def escape_tag_content(tag)
7676
tag = tag.to_s
7777
return tag unless tag.include?('|')
78-
tag.delete('|,')
78+
tag.delete('|,')
7979
end
8080

8181
def dd_tags(env = ENV)

spec/statsd/serialization/tag_serializer_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@
139139
expect(subject.format([tag])).to eq 'node:storage'
140140
end
141141

142+
it 'ignores nil values and formats all other falsy values correctly' do
143+
message_tags_hash = {
144+
empty_array: [],
145+
missing: nil,
146+
'empty_hash' => {},
147+
blank: '',
148+
:false_value => false,
149+
'results in blank': double('some tag', to_s: ''),
150+
'results in nil': double('some tag', to_s: nil),
151+
}
152+
153+
expect(subject.format(message_tags_hash)).to eq 'empty_array:[],missing,empty_hash:{},blank:,false_value:false,results in blank:,results in nil'
154+
end
155+
142156
it 'formats frozen tags correctly' do
143157
expect(subject.format(['name:foobarfoo'.freeze])).to eq 'name:foobarfoo'
144158
end

0 commit comments

Comments
 (0)