diff --git a/lib/datadog/statsd/serialization/tag_serializer.rb b/lib/datadog/statsd/serialization/tag_serializer.rb index ef49a19..cb5b3f1 100644 --- a/lib/datadog/statsd/serialization/tag_serializer.rb +++ b/lib/datadog/statsd/serialization/tag_serializer.rb @@ -78,10 +78,17 @@ def escape_tag_content(tag) tag.delete('|,') end + # DD_TAGS is comma separated here, but the Datadog Agent and the + # Heroku buildpack document it as space separated. Split on commas + # when there is one (so existing values, including tag values that + # contain spaces, keep working), otherwise split on whitespace. def dd_tags(env = ENV) return {} unless dd_tags = env['DD_TAGS'] - to_tags_hash(dd_tags.split(',')) + separator = dd_tags.include?(',') ? ',' : /\s+/ + tags = dd_tags.split(separator).map(&:strip).reject(&:empty?) + + to_tags_hash(tags) end def default_tags(env = ENV) diff --git a/spec/statsd/serialization/tag_serializer_spec.rb b/spec/statsd/serialization/tag_serializer_spec.rb index 8b72a2f..7dfaab2 100644 --- a/spec/statsd/serialization/tag_serializer_spec.rb +++ b/spec/statsd/serialization/tag_serializer_spec.rb @@ -180,6 +180,34 @@ end end + context 'when testing space separated DD_TAGS' do + around do |example| + ClimateControl.modify( + 'DD_TAGS' => ' ghi team:qa env:staging ' + ) do + example.run + end + end + + it 'correctly adds individual tags' do + expect(subject.format([])).to eq 'ghi,team:qa,env:staging' + end + end + + context 'when testing comma separated DD_TAGS with spaces in a value' do + around do |example| + ClimateControl.modify( + 'DD_TAGS' => 'team:my team, env:qa' + ) do + example.run + end + end + + it 'keeps splitting on commas only' do + expect(subject.format([])).to eq 'team:my team,env:qa' + end + end + context 'when testing DD_ENTITY_ID' do around do |example| ClimateControl.modify(