Skip to content

Commit

Permalink
Fix incorrect configuration mismatch warning when port is specified a…
Browse files Browse the repository at this point in the history
…s String

I noticed a Ruby app where the configuration mismatch warning was
popping up:

```
W, [2021-10-15T11:44:30.952540 #1]  WARN -- ddtrace: [ddtrace]
Configuration mismatch: values differ between "c.tracer.port" ('8126')
and DD_TRACE_AGENT_PORT environment variable ('8126'). Using '8126'.
```

The warning was being triggered because we convert the values read from
environment variables (`DD_TRACE_AGENT_PORT` or `DD_TRACE_AGENT_URL`)
into integers, but this app had `c.tracer.port` specified as a
string.

Because or that, since we collect all values, call `#uniq` on them
and see if we get more than one, the warning was being triggered
since the `8126` integer is not the same as the `'8126'` string.
  • Loading branch information
ivoanjo committed Oct 15, 2021
1 parent 4794149 commit 297df0a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
39 changes: 25 additions & 14 deletions lib/ddtrace/configuration/agent_settings_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,46 @@ def configured_hostname
def configured_port
return @configured_port if defined?(@configured_port)

port_from_env = ENV[Datadog::Ext::Transport::HTTP::ENV_DEFAULT_PORT]
parsed_port_from_env =
if port_from_env
begin
Integer(port_from_env)
rescue ArgumentError
log_warning(
"Invalid value for #{Datadog::Ext::Transport::HTTP::ENV_DEFAULT_PORT} environment variable " \
"('#{port_from_env}'). Ignoring this configuration."
)
end
end
try_parsing_as_integer(
friendly_name: "#{Datadog::Ext::Transport::HTTP::ENV_DEFAULT_PORT} environment variable",
value: ENV[Datadog::Ext::Transport::HTTP::ENV_DEFAULT_PORT],
)

parsed_settings_tracer_port =
try_parsing_as_integer(
friendly_name: '"c.tracer.port"',
value: settings.tracer.port,
)

@configured_port = pick_from(
DetectedConfiguration.new(
friendly_name: '"c.tracer.port"',
value: settings.tracer.port
value: parsed_settings_tracer_port,
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Ext::Transport::HTTP::ENV_DEFAULT_URL} environment variable",
value: parsed_url && parsed_url.port
value: parsed_url && parsed_url.port,
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Ext::Transport::HTTP::ENV_DEFAULT_PORT} environment variable",
value: parsed_port_from_env
value: parsed_port_from_env,
)
)
end

def try_parsing_as_integer(value:, friendly_name:)
return unless value

begin
Integer(value)
rescue ArgumentError
log_warning("Invalid value for #{friendly_name} (#{value.inspect}). Ignoring this configuration.")

nil
end
end

def ssl?
!parsed_url.nil? && parsed_url.scheme == 'https'
end
Expand Down
28 changes: 28 additions & 0 deletions spec/ddtrace/configuration/agent_settings_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,34 @@
resolver
end
end

context 'when the port is specified as a string instead of a number' do
before do
ddtrace_settings.tracer.port = '1234'
end

it 'contacts the agent using the http adapter, using the custom port' do
expect(resolver).to have_attributes(**settings, port: 1234)
end
end

context 'when the port is an invalid value' do
before do
ddtrace_settings.tracer.port = 'kaboom'

allow(logger).to receive(:warn)
end

it 'logs a warning' do
expect(logger).to receive(:warn).with(/Invalid value/)

resolver
end

it 'falls back to the defaults' do
expect(resolver).to have_attributes settings
end
end
end

context 'when a custom port is specified via code using "tracer(port: ...)"' do
Expand Down

0 comments on commit 297df0a

Please sign in to comment.