-
Notifications
You must be signed in to change notification settings - Fork 375
/
agent_settings_resolver.rb
353 lines (295 loc) · 11.8 KB
/
agent_settings_resolver.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# frozen_string_literal: true
require 'uri'
require_relative 'settings'
require_relative 'ext'
require_relative '../transport/ext'
module Datadog
module Core
module Configuration
# This class unifies all the different ways that users can configure how we talk to the agent.
#
# It has quite a lot of complexity, but this complexity just reflects the actual complexity we have around our
# configuration today. E.g., this is just all of the complexity regarding agent settings gathered together in a
# single place. As we deprecate more and more of the different ways that these things can be configured,
# this class will reflect that simplification as well.
#
# Whenever there is a conflict (different configurations are provided in different orders), it MUST warn the users
# about it and pick a value based on the following priority: code > environment variable > defaults.
class AgentSettingsResolver
AgentSettings = Struct.new(
:adapter,
:ssl,
:hostname,
:port,
:uds_path,
:timeout_seconds,
keyword_init: true
) do
def initialize(*)
super
freeze
end
end
def self.call(settings, logger: Datadog.logger)
new(settings, logger: logger).send(:call)
end
private
attr_reader \
:logger,
:settings
def initialize(settings, logger: Datadog.logger)
@settings = settings
@logger = logger
end
def call
AgentSettings.new(
adapter: adapter,
ssl: ssl?,
hostname: hostname,
port: port,
uds_path: uds_path,
timeout_seconds: timeout_seconds,
)
end
def adapter
if should_use_uds?
Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
else
Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
end
end
def configured_hostname
return @configured_hostname if defined?(@configured_hostname)
@configured_hostname = pick_from(
DetectedConfiguration.new(
friendly_name: "'c.agent.host'",
value: settings.agent.host
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
value: parsed_http_url&.hostname
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_HOST} environment variable",
value: ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_HOST]
)
)
end
def configured_port
return @configured_port if defined?(@configured_port)
@configured_port = pick_from(
try_parsing_as_integer(
friendly_name: '"c.agent.port"',
value: settings.agent.port,
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
value: parsed_http_url&.port,
),
try_parsing_as_integer(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_PORT} environment variable",
value: ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_PORT],
)
)
end
def configured_ssl
return @configured_ssl if defined?(@configured_ssl)
@configured_ssl = pick_from(
DetectedConfiguration.new(
friendly_name: '"c.agent.use_ssl"',
value: settings.agent.use_ssl,
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
value: parsed_url_ssl?,
)
)
end
def configured_timeout_seconds
return @configured_timeout_seconds if defined?(@configured_timeout_seconds)
@configured_timeout_seconds = pick_from(
try_parsing_as_integer(
friendly_name: '"c.agent.timeout_seconds"',
value: settings.agent.timeout_seconds,
),
try_parsing_as_integer(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_TIMEOUT_SECONDS} "\
'environment variable',
value: ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_TIMEOUT_SECONDS],
)
)
end
def configured_uds_path
return @configured_uds_path if defined?(@configured_uds_path)
@configured_uds_path = pick_from(
DetectedConfiguration.new(
friendly_name: "'c.agent.uds_path'",
value: settings.agent.uds_path
),
DetectedConfiguration.new(
friendly_name: "#{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} environment variable",
value: parsed_url_uds_path
)
)
end
def parsed_url_ssl?
return nil if parsed_url.nil?
parsed_url.scheme == 'https'
end
def try_parsing_as_integer(value:, friendly_name:)
value =
begin
Integer(value) if value
rescue ArgumentError, TypeError
log_warning("Invalid value for #{friendly_name} (#{value.inspect}). Ignoring this configuration.")
nil
end
DetectedConfiguration.new(friendly_name: friendly_name, value: value)
end
def ssl?
if should_use_uds?
false
else
configured_ssl || Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_USE_SSL
end
end
def hostname
configured_hostname || (should_use_uds? ? nil : Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_HOST)
end
def port
configured_port || (should_use_uds? ? nil : Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_PORT)
end
def timeout_seconds
return configured_timeout_seconds unless configured_timeout_seconds.nil?
if should_use_uds?
Datadog::Core::Configuration::Ext::Agent::UnixSocket::DEFAULT_TIMEOUT_SECONDS
else
Datadog::Core::Configuration::Ext::Agent::HTTP::DEFAULT_TIMEOUT_SECONDS
end
end
def parsed_url_uds_path
return nil unless parsed_url && unix_scheme?(parsed_url)
path = parsed_url.to_s
# Some versions of the built-in uri gem leave the original url untouched, and others remove the //, so this
# supports both
if path.start_with?('unix://')
path.sub('unix://', '')
else
path.sub('unix:', '')
end
end
# Unix socket path in the file system
def uds_path
return nil unless should_use_uds?
configured_uds_path || uds_fallback
end
# We only use the default unix socket if it is already present.
# This is by design, as we still want to use the default host:port if no unix socket is present.
def uds_fallback
return @uds_fallback if defined?(@uds_fallback)
@uds_fallback =
if configured_hostname.nil? &&
configured_port.nil? &&
File.exist?(Datadog::Core::Configuration::Ext::Agent::UnixSocket::DEFAULT_PATH)
Datadog::Core::Configuration::Ext::Agent::UnixSocket::DEFAULT_PATH
end
end
def should_use_uds?
# When we have mixed settings for http/https and uds, we print a warning
# and use the uds settings.
mixed_http_and_uds
can_use_uds?
end
def mixed_http_and_uds
return @mixed_http_and_uds if defined?(@mixed_http_and_uds)
@mixed_http_and_uds = (configured_hostname || configured_port) && can_use_uds?
if @mixed_http_and_uds
warn_if_configuration_mismatch(
[
DetectedConfiguration.new(
friendly_name: 'configuration for unix domain socket',
value: parsed_url.to_s,
),
DetectedConfiguration.new(
friendly_name: 'configuration of hostname/port for http/https use',
value: "hostname: '#{hostname}', port: '#{port}'",
),
]
)
end
@mixed_http_and_uds
end
def can_use_uds?
!configured_uds_path.nil? ||
# If no agent settings have been provided, we try to connect using a local unix socket.
# We only do so if the socket is present when `datadog` runs.
!uds_fallback.nil?
end
def parsed_url
return @parsed_url if defined?(@parsed_url)
unparsed_url_from_env = ENV[Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL]
@parsed_url =
if unparsed_url_from_env
parsed = URI.parse(unparsed_url_from_env)
if http_scheme?(parsed) || unix_scheme?(parsed)
parsed
else
# rubocop:disable Layout/LineLength
log_warning(
"Invalid URI scheme '#{parsed.scheme}' for #{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL} " \
"environment variable ('#{unparsed_url_from_env}'). " \
"Ignoring the contents of #{Datadog::Core::Configuration::Ext::Agent::ENV_DEFAULT_URL}."
)
# rubocop:enable Layout/LineLength
nil
end
end
end
def pick_from(*configurations_in_priority_order)
detected_configurations_in_priority_order = configurations_in_priority_order.select(&:value?)
if detected_configurations_in_priority_order.any?
warn_if_configuration_mismatch(detected_configurations_in_priority_order)
# The configurations are listed in priority, so we only need to look at the first; if there's more than
# one, we emit a warning above
detected_configurations_in_priority_order.first.value
end
end
def warn_if_configuration_mismatch(detected_configurations_in_priority_order)
return unless detected_configurations_in_priority_order.map(&:value).uniq.size > 1
log_warning(
'Configuration mismatch: values differ between ' \
"#{detected_configurations_in_priority_order
.map { |config| "#{config.friendly_name} (#{config.value.inspect})" }.join(' and ')}" \
". Using #{detected_configurations_in_priority_order.first.value.inspect} and ignoring other configuration."
)
end
def log_warning(message)
logger.warn(message) if logger
end
def http_scheme?(uri)
['http', 'https'].include?(uri.scheme)
end
# Expected to return nil (not false!) when it's not http
def parsed_http_url
parsed_url if parsed_url && http_scheme?(parsed_url)
end
def unix_scheme?(uri)
uri.scheme == 'unix'
end
# Represents a given configuration value and where we got it from
class DetectedConfiguration
attr_reader :friendly_name, :value
def initialize(friendly_name:, value:)
@friendly_name = friendly_name
@value = value
freeze
end
def value?
!value.nil?
end
end
private_constant :DetectedConfiguration
end
end
end
end