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 1 commit
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Would it perhaps make sense to make this a required argument, to make callers explicitly think about if they need commas or spaces for separation?

Suggested change
def env_to_list(var, default = [], comma_separated_only: false)
def env_to_list(var, default = [], comma_separated_only:)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

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
Copy link
Member

Choose a reason for hiding this comment

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

Minor: In the spirit of self-documenting code,

Suggested change
value.split # space splitting is the default
value.split(' ')

is faster to read than reading the comment ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Rubocop yelled at me for having the parameter that matches the default, so I removed it.
I'll add it back.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh Rubocop... That extremely opinionated colleague that we know nothing about :)

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
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