Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support spaces in environment variable lists #2011

Merged
merged 2 commits into from
May 12, 2022
Merged
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
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
Comment on lines +216 to +220
Copy link
Member

@ivoanjo ivoanjo May 11, 2022

Choose a reason for hiding this comment

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

To be honest, I'm not sure how useful it is to support this, rather than just doing split by space and comma at the same time and be done with it. But it is true that the Python implementation also does some of these hijinks so...

(We're not actually matching the exact hijinks with spaces in python, e.g. (" key: val,key2:val2", {" key": " val", "key2": "val2"}, None),, but again, I'd just go for splitting on both at the same time and not supporting weird mixes).

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm following Go's implementation, Java's implementation, and Python's original implementation document (internal doc):

  • Looking for commas, and using comma-separated if so.

I'm trying to match their implementations, to keep consistency, instead of doing what seems more right here.


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