Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/datadog/statsd/serialization/tag_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Comment on lines +88 to +89

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve spaces in existing single-tag DD_TAGS values

For an existing configuration containing just one tag with spaces, such as DD_TAGS="team:my team", the absence of a comma now selects whitespace parsing. This produces ["team:my", "team"], and to_tags_hash overwrites the first entry with the second, so the serializer emits only team instead of the previous team:my team. Upgrading therefore silently removes the tag value from metrics, events, and service checks using this configuration. Preserve the legacy single-tag case or make the ambiguous delimiter choice explicit; the new compatibility test only exercises values containing a comma.

Useful? React with 👍 / 👎.


to_tags_hash(tags)
end

def default_tags(env = ENV)
Expand Down
28 changes: 28 additions & 0 deletions spec/statsd/serialization/tag_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down