Skip to content

Commit

Permalink
Support spaces in environment variable lists
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed May 11, 2022
1 parent 73ef77c commit 3a557ae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/datadog/core/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def initialize(*_)
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG,
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3,
Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER
]
], comma_separated_only: true
)
end

Expand All @@ -430,7 +430,7 @@ def initialize(*_)
o.default do
env_to_list(
Tracing::Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE_INJECT,
[Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG] # Only inject Datadog headers by default
[Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG], comma_separated_only: true # Only inject Datadog headers by default
)
end

Expand Down
28 changes: 26 additions & 2 deletions lib/datadog/core/environment/variable_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,34 @@ def env_to_float(var, default = nil)
var && ENV.key?(var) ? ENV[var].to_f : default
end

def env_to_list(var, default = [])
# Parses comma- or space-separated lists.
#
# If a comma is present, then the list is considered comma-separated.
# Otherwise, it is considered space-separated.
#
# After the entries are separated, commas and whitespaces that are
# either trailing or leading are trimmed.
#
# Empty entries, after trimmed, are also removed from the result.
def env_to_list(var, default = [], comma_separated_only: false)
var = decode_array(var)
if var && ENV.key?(var)
ENV[var].split(',').map(&:strip)
value = ENV[var]

values = if value.include?(',') || comma_separated_only
value.split(',')
else
value.split # space splitting is the default
end

values.map! do |v|
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')

v.empty? ? nil : v
end

values.compact!
values
else
default
end
Expand Down
40 changes: 37 additions & 3 deletions spec/datadog/core/environment/variable_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
end
end

context 'when env var is set as' do
context 'when env var is set' do
include_context 'env var'

context '\'\'' do
Expand All @@ -201,11 +201,45 @@
it { is_expected.to eq(%w[1 2]) }
end

context ' 1 , 2 , 3 ' do
let(:env_value) { ' 1 , 2 , 3 ' }
context ' 1 , 2 , 3 ' do
let(:env_value) { ' 1 , 2 , 3 ' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1 2 3' do
let(:env_value) { '1 2 3' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1,2 3' do
let(:env_value) { '1,2 3' }

it { is_expected.to eq(['1', '2 3']) }
end

context ' 1 2 3 ' do
let(:env_value) { ' 1 2 3 ' }

it { is_expected.to eq(%w[1 2 3]) }
end

context '1,, ,2,3,' do
let(:env_value) { '1,, ,2,3,' }

it { is_expected.to eq(%w[1 2 3]) }
end

context 'and comma_separated_only is set' do
subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: true) }

context 'B3 single header ' do
let(:env_value) { 'B3 single header ' }

it { is_expected.to eq(['B3 single header']) }
end
end
end
end
end

0 comments on commit 3a557ae

Please sign in to comment.