Skip to content

Commit 51446c7

Browse files
authored
Merge pull request #4024 from DataDog/munir/uds-should-take-precedence-over-http-if-both-defined
fix: ensure uds always takes precedence over http if both configurations are defined
2 parents 11ce2e3 + ebaa0bf commit 51446c7

File tree

2 files changed

+55
-66
lines changed

2 files changed

+55
-66
lines changed

lib/datadog/core/configuration/agent_settings_resolver.rb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,32 @@ def uds_fallback
232232
end
233233

234234
def should_use_uds?
235-
can_use_uds? && !mixed_http_and_uds?
235+
# When we have mixed settings for http/https and uds, we print a warning
236+
# and use the uds settings.
237+
mixed_http_and_uds
238+
can_use_uds?
239+
end
240+
241+
def mixed_http_and_uds
242+
return @mixed_http_and_uds if defined?(@mixed_http_and_uds)
243+
244+
@mixed_http_and_uds = (configured_hostname || configured_port) && can_use_uds?
245+
if @mixed_http_and_uds
246+
warn_if_configuration_mismatch(
247+
[
248+
DetectedConfiguration.new(
249+
friendly_name: 'configuration for unix domain socket',
250+
value: parsed_url.to_s,
251+
),
252+
DetectedConfiguration.new(
253+
friendly_name: 'configuration of hostname/port for http/https use',
254+
value: "hostname: '#{hostname}', port: '#{port}'",
255+
),
256+
]
257+
)
258+
end
259+
260+
@mixed_http_and_uds
236261
end
237262

238263
def can_use_uds?
@@ -307,30 +332,6 @@ def unix_scheme?(uri)
307332
uri.scheme == 'unix'
308333
end
309334

310-
# When we have mixed settings for http/https and uds, we print a warning and ignore the uds settings
311-
def mixed_http_and_uds?
312-
return @mixed_http_and_uds if defined?(@mixed_http_and_uds)
313-
314-
@mixed_http_and_uds = (configured_hostname || configured_port) && can_use_uds?
315-
316-
if @mixed_http_and_uds
317-
warn_if_configuration_mismatch(
318-
[
319-
DetectedConfiguration.new(
320-
friendly_name: 'configuration of hostname/port for http/https use',
321-
value: "hostname: '#{hostname}', port: '#{port}'",
322-
),
323-
DetectedConfiguration.new(
324-
friendly_name: 'configuration for unix domain socket',
325-
value: parsed_url.to_s,
326-
),
327-
]
328-
)
329-
end
330-
331-
@mixed_http_and_uds
332-
end
333-
334335
# Represents a given configuration value and where we got it from
335336
class DetectedConfiguration
336337
attr_reader :friendly_name, :value

spec/datadog/core/configuration/agent_settings_resolver_spec.rb

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,29 @@
170170
context 'when there is a hostname specified along with uds configuration' do
171171
let(:with_agent_host) { 'custom-hostname' }
172172

173-
it 'prioritizes the http configuration' do
174-
expect(resolver).to have_attributes(hostname: 'custom-hostname', adapter: :net_http)
173+
it 'prioritizes the uds configuration' do
174+
expect(resolver).to have_attributes(adapter: :unix)
175175
end
176176

177-
it 'logs a warning including the uds path' do
177+
it 'logs a warning including the mismatching hostname' do
178178
expect(logger).to receive(:warn)
179-
.with(%r{Configuration mismatch.*configuration for unix domain socket \("unix:.*/some/path"\)})
179+
.with(/Configuration mismatch:.*hostname: 'custom-hostname'.*/)
180180

181181
resolver
182182
end
183183

184-
it 'does not include a uds_path in the configuration' do
185-
expect(resolver).to have_attributes(uds_path: nil)
184+
it 'includes a uds_path in the configuration' do
185+
expect(resolver).to have_attributes(uds_path: '/some/path')
186186
end
187187

188188
context 'when there is no port specified' do
189-
it 'prioritizes the http configuration and uses the default port' do
190-
expect(resolver).to have_attributes(port: 8126, hostname: 'custom-hostname', adapter: :net_http)
189+
it 'prioritizes the uds configuration and ignores the default port' do
190+
expect(resolver).to have_attributes(adapter: :unix)
191191
end
192192

193-
it 'logs a warning including the hostname and default port' do
193+
it 'logs a warning including the uds path' do
194194
expect(logger).to receive(:warn)
195-
.with(/
196-
Configuration\ mismatch:\ values\ differ\ between\ configuration.*
197-
Using\ "hostname:\ 'custom-hostname',\ port:\ '8126'".*
198-
/x)
195+
.with(%r{Configuration mismatch.*Using "unix:[\/]{1,3}some/path"}) # rubocop:disable Style/edundantRegexpCharacterClass
199196

200197
resolver
201198
end
@@ -204,51 +201,45 @@
204201
context 'when there is a port specified' do
205202
let(:with_agent_port) { 1234 }
206203

207-
it 'prioritizes the http configuration and uses the specified port' do
208-
expect(resolver).to have_attributes(port: 1234, hostname: 'custom-hostname', adapter: :net_http)
204+
it 'prioritizes the uds path' do
205+
expect(resolver).to have_attributes(adapter: :unix)
209206
end
210207

211-
it 'logs a warning including the hostname and port' do
208+
it 'logs a warning including the uds configuration' do
212209
expect(logger).to receive(:warn)
213-
.with(/
214-
Configuration\ mismatch:\ values\ differ\ between\ configuration.*
215-
Using\ "hostname:\ 'custom-hostname',\ port:\ '1234'".*
216-
/x)
210+
.with(%r{Configuration mismatch.*Using "unix:[\/]{1,3}some/path"}) # rubocop:disable Style/edundantRegexpCharacterClass
217211

218212
resolver
219213
end
220214
end
221215
end
222216

223-
context 'when there is a port specified along with uds configuration' do
217+
context 'when there is a port specified along with a uds configuration' do
224218
let(:with_agent_port) { 5678 }
225219

226-
it 'prioritizes the http configuration' do
227-
expect(resolver).to have_attributes(port: 5678, adapter: :net_http)
220+
it 'prioritizes the uds configuration' do
221+
expect(resolver).to have_attributes(port: 5678, adapter: :unix)
228222
end
229223

230-
it 'logs a warning including the uds path' do
224+
it 'logs a warning including the mismatching port' do
231225
expect(logger).to receive(:warn)
232-
.with(%r{Configuration mismatch.*configuration for unix domain socket \("unix:.*/some/path"\)})
226+
.with(/Configuration mismatch:.*port: '5678'.*/)
233227

234228
resolver
235229
end
236230

237-
it 'does not include a uds_path in the configuration' do
238-
expect(resolver).to have_attributes(uds_path: nil)
231+
it 'includes the uds path in the configuration' do
232+
expect(resolver).to have_attributes(uds_path: '/some/path')
239233
end
240234

241235
context 'when there is no hostname specified' do
242-
it 'prioritizes the http configuration and uses the default hostname' do
243-
expect(resolver).to have_attributes(port: 5678, hostname: '127.0.0.1', adapter: :net_http)
236+
it 'prioritizes the uds configuration' do
237+
expect(resolver).to have_attributes(port: 5678, hostname: nil, adapter: :unix)
244238
end
245239

246-
it 'logs a warning including the default hostname and port' do
240+
it 'logs a warning including the uds configuration' do
247241
expect(logger).to receive(:warn)
248-
.with(/
249-
Configuration\ mismatch:\ values\ differ\ between\ configuration.*
250-
Using\ "hostname:\ '127.0.0.1',\ port:\ '5678'".*
251-
/x)
242+
.with(%r{Configuration mismatch.*Using "unix:[\/]{1,3}some/path"}) # rubocop:disable Style/edundantRegexpCharacterClass
252243

253244
resolver
254245
end
@@ -257,16 +248,13 @@
257248
context 'when there is a hostname specified' do
258249
let(:with_agent_host) { 'custom-hostname' }
259250

260-
it 'prioritizes the http configuration and uses the specified hostname' do
261-
expect(resolver).to have_attributes(port: 5678, hostname: 'custom-hostname', adapter: :net_http)
251+
it 'prioritizes the uds configuration' do
252+
expect(resolver).to have_attributes(adapter: :unix)
262253
end
263254

264-
it 'logs a warning including the hostname and port' do
255+
it 'logs a warning including the uds configuration' do
265256
expect(logger).to receive(:warn)
266-
.with(/
267-
Configuration\ mismatch:\ values\ differ\ between\ configuration.*
268-
Using\ "hostname:\ 'custom-hostname',\ port:\ '5678'".*
269-
/x)
257+
.with(%r{Configuration mismatch.*Using "unix:[\/]{1,3}some/path"}) # rubocop:disable Style/edundantRegexpCharacterClass
270258

271259
resolver
272260
end

0 commit comments

Comments
 (0)