From 0c673650626e89fafd4799aec618667dc24153e2 Mon Sep 17 00:00:00 2001 From: Joseph Hughes Date: Mon, 5 Feb 2024 15:36:05 +0000 Subject: [PATCH] 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. --- lib/datadog/statsd/serialization/tag_serializer.rb | 8 ++++---- spec/statsd/serialization/tag_serializer_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/datadog/statsd/serialization/tag_serializer.rb b/lib/datadog/statsd/serialization/tag_serializer.rb index 95860df..b6dc9b1 100644 --- a/lib/datadog/statsd/serialization/tag_serializer.rb +++ b/lib/datadog/statsd/serialization/tag_serializer.rb @@ -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 @@ -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('|,') end def dd_tags(env = ENV) diff --git a/spec/statsd/serialization/tag_serializer_spec.rb b/spec/statsd/serialization/tag_serializer_spec.rb index d45be5f..a07b27b 100644 --- a/spec/statsd/serialization/tag_serializer_spec.rb +++ b/spec/statsd/serialization/tag_serializer_spec.rb @@ -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