From ad053c0b0d131d7dc93913db65f2273b66952c33 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 --- lib/datadog/statsd/serialization/tag_serializer.rb | 6 +++--- spec/statsd/serialization/tag_serializer_spec.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/datadog/statsd/serialization/tag_serializer.rb b/lib/datadog/statsd/serialization/tag_serializer.rb index 95860df..4d66bbf 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.nil? escape_tag_content(name) + else + escape_tag_content("#{name}:#{value}") end end when Array diff --git a/spec/statsd/serialization/tag_serializer_spec.rb b/spec/statsd/serialization/tag_serializer_spec.rb index d45be5f..a6c0e8d 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 hash tag values which are not present when called with to_s' do + message_tags_hash = { + request: [], + missing: nil, + 'another' => {}, + blank: '', + :yet_another => 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 'request:[],another:{},yet_another:false' + end + it 'formats frozen tags correctly' do expect(subject.format(['name:foobarfoo'.freeze])).to eq 'name:foobarfoo' end