Skip to content

Commit

Permalink
Support spaces in environment variable lists (#2011)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed May 12, 2022
1 parent e0b729d commit cf3cbc5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/datadog/core/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def initialize(*_)
tags = {}

# Parse tags from environment
env_to_list(Core::Environment::Ext::ENV_TAGS).each do |tag|
env_to_list(Core::Environment::Ext::ENV_TAGS, comma_separated_only: false).each do |tag|
pair = tag.split(':')
tags[pair.first] = pair.last if pair.length == 2
end
Expand Down 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:)
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(' ') # rubocop:disable Style/RedundantArgument
end

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

v.empty? ? nil : v
end

values.compact!
values
else
default
end
Expand Down
44 changes: 39 additions & 5 deletions spec/datadog/core/environment/variable_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,23 @@
end

describe '::env_to_list' do
subject(:env_to_list) { variable_helpers.env_to_list(var) }
subject(:env_to_list) { variable_helpers.env_to_list(var, comma_separated_only: false) }

context 'when env var is not defined' do
context 'and default is not defined' do
it { is_expected.to eq([]) }
end

context 'and default is defined' do
subject(:env_to_list) { variable_helpers.env_to_list(var, default) }
subject(:env_to_list) { variable_helpers.env_to_list(var, default, comma_separated_only: false) }

let(:default) { double }

it { is_expected.to be default }
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 cf3cbc5

Please sign in to comment.