-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathConfigProvider.java
More file actions
517 lines (463 loc) · 16.8 KB
/
Copy pathConfigProvider.java
File metadata and controls
517 lines (463 loc) · 16.8 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package datadog.trace.bootstrap.config.provider;
import static datadog.trace.api.config.GeneralConfig.CONFIGURATION_FILE;
import datadog.trace.api.ConfigCollector;
import datadog.trace.api.ConfigOrigin;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ConfigProvider {
private static final class Singleton {
private static final ConfigProvider INSTANCE = ConfigProvider.createDefault();
}
private static final Logger log = LoggerFactory.getLogger(ConfigProvider.class);
private final boolean collectConfig;
private final ConfigProvider.Source[] sources;
private ConfigProvider(ConfigProvider.Source... sources) {
this(true, sources);
}
private ConfigProvider(boolean collectConfig, ConfigProvider.Source... sources) {
this.collectConfig = collectConfig;
this.sources = sources;
}
public String getConfigFileStatus() {
for (ConfigProvider.Source source : sources) {
if (source instanceof PropertiesConfigSource) {
String configFileStatus = ((PropertiesConfigSource) source).getConfigFileStatus();
if (null != configFileStatus) {
return configFileStatus;
}
}
}
return "no config file present";
}
public String getString(String key) {
return getString(key, null);
}
public <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T defaultValue) {
String value = getString(key);
if (null != value) {
try {
return Enum.valueOf(enumType, value);
} catch (Exception ignoreAndUseDefault) {
log.debug("failed to parse {} for {}, defaulting to {}", value, key, defaultValue);
}
}
if (collectConfig) {
String valueStr = defaultValue == null ? null : defaultValue.name();
ConfigCollector.get().put(key, valueStr, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
public String getString(String key, String defaultValue, String... aliases) {
for (ConfigProvider.Source source : sources) {
String value = source.get(key, aliases);
if (value != null) {
if (collectConfig) {
ConfigCollector.get().put(key, value, source.origin());
}
return value;
}
}
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
/**
* Like {@link #getString(String, String, String...)} but falls back to next source if a value is
* an empty or blank string.
*/
public String getStringNotEmpty(String key, String defaultValue, String... aliases) {
for (ConfigProvider.Source source : sources) {
String value = source.get(key, aliases);
if (value != null && !value.trim().isEmpty()) {
if (collectConfig) {
ConfigCollector.get().put(key, value, source.origin());
}
return value;
}
}
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
public String getStringExcludingSource(
String key,
String defaultValue,
Class<? extends ConfigProvider.Source> excludedSource,
String... aliases) {
for (ConfigProvider.Source source : sources) {
if (excludedSource.isAssignableFrom(source.getClass())) {
continue;
}
String value = source.get(key, aliases);
if (value != null) {
if (collectConfig) {
ConfigCollector.get().put(key, value, source.origin());
}
return value;
}
}
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
public boolean isSet(String key) {
String value = getString(key);
return value != null && !value.isEmpty();
}
public Boolean getBoolean(String key) {
return get(key, null, Boolean.class);
}
public Boolean getBoolean(String key, String... aliases) {
return get(key, null, Boolean.class, aliases);
}
public boolean getBoolean(String key, boolean defaultValue, String... aliases) {
return get(key, defaultValue, Boolean.class, aliases);
}
public Integer getInteger(String key) {
return get(key, null, Integer.class);
}
public Integer getInteger(String key, String... aliases) {
return get(key, null, Integer.class, aliases);
}
public int getInteger(String key, int defaultValue, String... aliases) {
return get(key, defaultValue, Integer.class, aliases);
}
public Long getLong(String key) {
return get(key, null, Long.class);
}
public Long getLong(String key, String... aliases) {
return get(key, null, Long.class, aliases);
}
public long getLong(String key, long defaultValue, String... aliases) {
return get(key, defaultValue, Long.class, aliases);
}
public Float getFloat(String key, String... aliases) {
return get(key, null, Float.class, aliases);
}
public float getFloat(String key, float defaultValue) {
return get(key, defaultValue, Float.class);
}
public Double getDouble(String key) {
return get(key, null, Double.class);
}
public double getDouble(String key, double defaultValue) {
return get(key, defaultValue, Double.class);
}
private <T> T get(String key, T defaultValue, Class<T> type, String... aliases) {
for (ConfigProvider.Source source : sources) {
try {
String sourceValue = source.get(key, aliases);
T value = ConfigConverter.valueOf(sourceValue, type);
if (value != null) {
if (collectConfig) {
ConfigCollector.get().put(key, sourceValue, source.origin());
}
return value;
}
} catch (NumberFormatException ex) {
// continue
}
}
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
public List<String> getList(String key) {
return ConfigConverter.parseList(getString(key));
}
public List<String> getList(String key, List<String> defaultValue) {
String list = getString(key);
if (null == list) {
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
} else {
return ConfigConverter.parseList(list);
}
}
public Set<String> getSet(String key, Set<String> defaultValue) {
String list = getString(key);
if (null == list) {
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
} else {
return new HashSet(ConfigConverter.parseList(list));
}
}
public List<String> getSpacedList(String key) {
return ConfigConverter.parseList(getString(key), " ");
}
public Map<String, String> getMergedMap(String key, String... aliases) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
// We reverse iterate to allow overrides
for (int i = sources.length - 1; 0 <= i; i--) {
String value = sources[i].get(key, aliases);
Map<String, String> parsedMap = ConfigConverter.parseMap(value, key);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
}
return merged;
}
public Map<String, String> getMergedTagsMap(String key, String... aliases) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
// We reverse iterate to allow overrides
for (int i = sources.length - 1; 0 <= i; i--) {
String value = sources[i].get(key, aliases);
Map<String, String> parsedMap =
ConfigConverter.parseTraceTagsMap(value, ':', Arrays.asList(',', ' '));
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
}
return merged;
}
public Map<String, String> getOrderedMap(String key) {
LinkedHashMap<String, String> merged = new LinkedHashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
// We reverse iterate to allow overrides
for (int i = sources.length - 1; 0 <= i; i--) {
String value = sources[i].get(key);
Map<String, String> parsedMap = ConfigConverter.parseOrderedMap(value, key);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
}
return merged;
}
public Map<String, String> getMergedMapWithOptionalMappings(
String defaultPrefix, boolean lowercaseKeys, String... keys) {
Map<String, String> merged = new HashMap<>();
ConfigOrigin origin = ConfigOrigin.DEFAULT;
// System properties take precedence over env
// prior art:
// https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/html/boot-features-external-config.html
// We reverse iterate to allow overrides
for (String key : keys) {
for (int i = sources.length - 1; 0 <= i; i--) {
String value = sources[i].get(key);
Map<String, String> parsedMap =
ConfigConverter.parseMapWithOptionalMappings(value, key, defaultPrefix, lowercaseKeys);
if (!parsedMap.isEmpty()) {
origin = sources[i].origin();
}
merged.putAll(parsedMap);
}
if (collectConfig) {
ConfigCollector.get().put(key, merged, origin);
}
}
return merged;
}
public BitSet getIntegerRange(final String key, final BitSet defaultValue, String... aliases) {
final String value = getString(key, null, aliases);
try {
if (value != null) {
return ConfigConverter.parseIntegerRangeSet(value, key);
}
} catch (final NumberFormatException e) {
log.warn("Invalid configuration for {}", key, e);
}
if (collectConfig) {
ConfigCollector.get().put(key, defaultValue, ConfigOrigin.DEFAULT);
}
return defaultValue;
}
public boolean isEnabled(
final Iterable<String> settingNames,
final String settingPrefix,
final String settingSuffix,
final boolean defaultEnabled) {
// If default is enabled, we want to disable individually.
// If default is disabled, we want to enable individually.
boolean anyEnabled = defaultEnabled;
for (final String name : settingNames) {
final String configKey = settingPrefix + name + settingSuffix;
final String fullKey = configKey.startsWith("trace.") ? configKey : "trace." + configKey;
final boolean configEnabled = getBoolean(fullKey, defaultEnabled, configKey);
if (defaultEnabled) {
anyEnabled &= configEnabled;
} else {
anyEnabled |= configEnabled;
}
}
return anyEnabled;
}
public static ConfigProvider getInstance() {
return Singleton.INSTANCE;
}
public static ConfigProvider createDefault() {
Properties configProperties =
loadConfigurationFile(
new ConfigProvider(new SystemPropertiesConfigSource(), new EnvironmentConfigSource()));
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
}
}
public static ConfigProvider withoutCollector() {
Properties configProperties =
loadConfigurationFile(
new ConfigProvider(
false, new SystemPropertiesConfigSource(), new EnvironmentConfigSource()));
if (configProperties.isEmpty()) {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
new OtelEnvironmentConfigSource(),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
false,
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
}
}
public static ConfigProvider withPropertiesOverride(Properties properties) {
PropertiesConfigSource providedConfigSource = new PropertiesConfigSource(properties, false);
Properties configProperties =
loadConfigurationFile(
new ConfigProvider(
new SystemPropertiesConfigSource(),
new EnvironmentConfigSource(),
providedConfigSource));
if (configProperties.isEmpty()) {
return new ConfigProvider(
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
providedConfigSource,
new OtelEnvironmentConfigSource(),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
} else {
return new ConfigProvider(
providedConfigSource,
new SystemPropertiesConfigSource(),
StableConfigSource.FLEET,
new EnvironmentConfigSource(),
new PropertiesConfigSource(configProperties, true),
new OtelEnvironmentConfigSource(configProperties),
StableConfigSource.LOCAL,
new CapturedEnvironmentConfigSource());
}
}
/**
* Loads the optional configuration properties file into the global {@link Properties} object.
*
* @return The {@link Properties} object. the returned instance might be empty of file does not
* exist or if it is in a wrong format.
* @param configProvider
*/
@SuppressForbidden
private static Properties loadConfigurationFile(ConfigProvider configProvider) {
final Properties properties = new Properties();
// Reading from system property first and from env after
String configurationFilePath = configProvider.getString(CONFIGURATION_FILE);
if (null == configurationFilePath) {
return properties;
}
// Normalizing tilde (~) paths for unix systems
configurationFilePath =
configurationFilePath.replaceFirst("^~", System.getProperty("user.home"));
// Configuration properties file is optional
final File configurationFile = new File(configurationFilePath);
if (!configurationFile.exists()) {
log.error("Configuration file '{}' not found.", configurationFilePath);
return properties;
}
try (final FileReader fileReader = new FileReader(configurationFile)) {
properties.load(fileReader);
} catch (final FileNotFoundException fnf) {
log.error("Configuration file '{}' not found.", configurationFilePath);
} catch (final IOException ioe) {
log.error(
"Configuration file '{}' cannot be accessed or correctly parsed.", configurationFilePath);
}
properties.setProperty(PropertiesConfigSource.CONFIG_FILE_STATUS, configurationFilePath);
return properties;
}
public abstract static class Source {
public final String get(String key, String... aliases) {
String value = get(key);
if (value != null) {
return value;
}
for (String alias : aliases) {
value = get(alias);
if (value != null) {
return value;
}
}
return null;
}
protected abstract String get(String key);
public abstract ConfigOrigin origin();
}
}