Skip to content

Commit f8005b9

Browse files
authored
Merge pull request #4530 from DataDog/munir/update-ddtags
fix(global_tags): update ddtags to align with other sdks
2 parents 535244c + 3a97563 commit f8005b9

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

lib/datadog/core/configuration/settings.rb

+23-30
Original file line numberDiff line numberDiff line change
@@ -619,38 +619,31 @@ def initialize(*_)
619619
o.type :hash, nilable: true
620620
o.env [Core::Environment::Ext::ENV_TAGS, Core::Environment::Ext::ENV_OTEL_RESOURCE_ATTRIBUTES]
621621
o.env_parser do |env_value|
622-
values = if env_value.include?(',')
623-
env_value.split(',')
624-
else
625-
env_value.split(' ') # rubocop:disable Style/RedundantArgument
626-
end
627-
values.map! do |v|
628-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
629-
630-
v.empty? ? nil : v
631-
end
632-
633-
values.compact!
634-
values.each_with_object({}) do |tag, tags|
635-
key, value = tag.split(':', 2)
636-
if value.nil?
637-
# support tags/attributes delimited by the OpenTelemetry separator (`=`)
638-
key, value = tag.split('=', 2)
639-
end
640-
next if value.nil? || value.empty?
641-
642-
# maps OpenTelemetry semantic attributes to Datadog tags
643-
case key.downcase
644-
when 'deployment.environment'
645-
tags['env'] = value
646-
when 'service.version'
647-
tags['version'] = value
648-
when 'service.name'
649-
tags['service'] = value
650-
else
651-
tags[key] = value
622+
# Parses a string containing key-value pairs and returns a hash.
623+
# Key-value pairs are delimited by ':' OR `=`, and pairs are separated by whitespace, comma, OR BOTH.
624+
result = {}
625+
unless env_value.nil? || env_value.empty?
626+
# falling back to comma as separator
627+
sep = env_value.include?(',') ? ',' : ' '
628+
# split by separator
629+
env_value.split(sep).each do |tag|
630+
tag.strip!
631+
next if tag.empty?
632+
633+
# tag by : or = (for OpenTelemetry)
634+
key, val = tag.split(/[:=]/, 2).map(&:strip)
635+
val ||= ''
636+
# maps OpenTelemetry semantic attributes to Datadog tags
637+
key = case key.downcase
638+
when 'deployment.environment' then 'env'
639+
when 'service.version' then 'version'
640+
when 'service.name' then 'service'
641+
else key
642+
end
643+
result[key] = val unless key.empty?
652644
end
653645
end
646+
result
654647
end
655648
o.setter do |new_value, old_value|
656649
raw_tags = new_value || {}

spec/datadog/core/configuration/settings_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@
10951095
it { is_expected.to include('a' => '1', 'b' => '2') }
10961096

10971097
context 'with an invalid tag' do
1098-
['', 'a', ':', ',', 'a:'].each do |invalid_tag|
1098+
['', ':', ','].each do |invalid_tag|
10991099
context "when tag is #{invalid_tag.inspect}" do
11001100
let(:env_tags) { invalid_tag }
11011101

@@ -1104,6 +1104,15 @@
11041104
end
11051105
end
11061106

1107+
context 'with no seperator' do
1108+
['key', 'key:', 'key: '].each do |tag|
1109+
context "when tag is #{tag.inspect}" do
1110+
let(:env_tags) { tag }
1111+
it { is_expected.to eq({ 'key' => '' }) }
1112+
end
1113+
end
1114+
end
1115+
11071116
context 'with multiple colons' do
11081117
let(:env_tags) { 'key:va:lue' }
11091118

0 commit comments

Comments
 (0)