From ee9f9b7eee63ecb05c7d53618a540b922b30f39c Mon Sep 17 00:00:00 2001 From: theluckystrike <51033404+theluckystrike@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:41:49 +0700 Subject: [PATCH] Accept space separated DD_TAGS The Datadog Agent and the Heroku buildpack document DD_TAGS as a space separated list, but the tag serializer only split it on commas, so DD_TAGS="team:qa env:staging" became one broken tag. Split on commas when the value has one, so existing values (including tag values that contain spaces) keep working, and on whitespace otherwise. Surrounding whitespace and empty entries are dropped. Fixes #325 --- .../statsd/serialization/tag_serializer.rb | 9 +++++- .../serialization/tag_serializer_spec.rb | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) 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(