Skip to content

Commit cad575c

Browse files
committed
updating ConfigHelper to be a singleton
1 parent a4de955 commit cad575c

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

utils/config-utils/src/main/java/datadog/trace/config/inversion/ConfigHelper.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,45 @@ public String toString() {
3434

3535
private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
3636

37-
private static StrictnessPolicy configInversionStrict = StrictnessPolicy.WARNING;
37+
private static final ConfigHelper INSTANCE = new ConfigHelper();
38+
39+
private StrictnessPolicy configInversionStrict = StrictnessPolicy.WARNING;
3840

3941
// Cache for configs, init value is null
40-
private static Map<String, String> configs;
42+
private Map<String, String> configs;
4143

4244
// Default to production source
43-
private static SupportedConfigurationSource configSource = new SupportedConfigurationSource();
45+
private SupportedConfigurationSource configSource = new SupportedConfigurationSource();
46+
47+
public static ConfigHelper get() {
48+
return INSTANCE;
49+
}
4450

45-
public static void setConfigInversionStrict(StrictnessPolicy configInversionStrict) {
46-
ConfigHelper.configInversionStrict = configInversionStrict;
51+
public void setConfigInversionStrict(StrictnessPolicy configInversionStrict) {
52+
this.configInversionStrict = configInversionStrict;
4753
}
4854

49-
public static StrictnessPolicy configInversionStrictFlag() {
55+
public StrictnessPolicy configInversionStrictFlag() {
5056
return configInversionStrict;
5157
}
5258

5359
// Used only for testing purposes
54-
static void setConfigurationSource(SupportedConfigurationSource testSource) {
60+
void setConfigurationSource(SupportedConfigurationSource testSource) {
5561
configSource = testSource;
5662
}
5763

5864
/** Resetting config cache. Useful for cleaning up after tests. */
59-
static void resetCache() {
65+
void resetCache() {
6066
configs = null;
6167
}
6268

6369
/** Reset all configuration data to the generated defaults. Useful for cleaning up after tests. */
64-
static void resetToDefaults() {
70+
void resetToDefaults() {
6571
configSource = new SupportedConfigurationSource();
66-
configInversionStrict = StrictnessPolicy.WARNING;
72+
this.configInversionStrict = StrictnessPolicy.WARNING;
6773
}
6874

69-
public static Map<String, String> getEnvironmentVariables() {
75+
public Map<String, String> getEnvironmentVariables() {
7076
if (configs != null) {
7177
return configs;
7278
}
@@ -111,7 +117,7 @@ public static Map<String, String> getEnvironmentVariables() {
111117
return configs;
112118
}
113119

114-
public static String getEnvironmentVariable(String name) {
120+
public String getEnvironmentVariable(String name) {
115121
if (configs != null && configs.containsKey(name)) {
116122
return configs.get(name);
117123
}

utils/config-utils/src/test/java/datadog/trace/config/inversion/ConfigHelperTest.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ static void setUp() {
5959
testSource =
6060
new TestSupportedConfigurationSource(
6161
testSupported, testAliases, testAliasMapping, new HashMap<>());
62-
ConfigHelper.setConfigurationSource(testSource);
63-
strictness = ConfigHelper.configInversionStrictFlag();
64-
ConfigHelper.setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
62+
ConfigHelper.get().setConfigurationSource(testSource);
63+
strictness = ConfigHelper.get().configInversionStrictFlag();
64+
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
6565
}
6666

6767
@AfterAll
6868
static void tearDown() {
69-
ConfigHelper.resetToDefaults();
70-
ConfigHelper.setConfigInversionStrict(strictness);
69+
ConfigHelper.get().resetToDefaults();
70+
ConfigHelper.get().setConfigInversionStrict(strictness);
7171
}
7272

7373
@AfterEach
7474
void reset() {
75-
ConfigHelper.resetCache();
75+
ConfigHelper.get().resetCache();
7676
}
7777

7878
@Test
@@ -81,11 +81,11 @@ void testBasicConfigHelper() {
8181
setEnvVar(TEST_OTEL_VAR, TEST_OTEL_VAR_VAL);
8282
setEnvVar(TEST_REGULAR_VAR, TEST_REGULAR_VAR_VAL);
8383

84-
assertEquals(TEST_DD_VAR_VAL, ConfigHelper.getEnvironmentVariable(TEST_DD_VAR));
85-
assertEquals(TEST_OTEL_VAR_VAL, ConfigHelper.getEnvironmentVariable(TEST_OTEL_VAR));
86-
assertEquals(TEST_REGULAR_VAR_VAL, ConfigHelper.getEnvironmentVariable(TEST_REGULAR_VAR));
84+
assertEquals(TEST_DD_VAR_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_DD_VAR));
85+
assertEquals(TEST_OTEL_VAR_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_OTEL_VAR));
86+
assertEquals(TEST_REGULAR_VAR_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_REGULAR_VAR));
8787

88-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
88+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
8989
assertEquals(TEST_DD_VAR_VAL, result.get(TEST_DD_VAR));
9090
assertEquals(TEST_OTEL_VAR_VAL, result.get(TEST_OTEL_VAR));
9191
assertEquals(TEST_REGULAR_VAR_VAL, result.get(TEST_REGULAR_VAR));
@@ -100,8 +100,8 @@ void testBasicConfigHelper() {
100100
void testAliasSupport() {
101101
setEnvVar(ALIAS_DD_VAR, ALIAS_DD_VAL);
102102

103-
assertEquals(ALIAS_DD_VAL, ConfigHelper.getEnvironmentVariable(TEST_DD_VAR));
104-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
103+
assertEquals(ALIAS_DD_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_DD_VAR));
104+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
105105
assertEquals(ALIAS_DD_VAL, result.get(TEST_DD_VAR));
106106
assertFalse(result.containsKey(ALIAS_DD_VAR));
107107

@@ -115,8 +115,8 @@ void testMainConfigPrecedence() {
115115
setEnvVar(TEST_DD_VAR, TEST_DD_VAR_VAL);
116116
setEnvVar(ALIAS_DD_VAR, ALIAS_DD_VAL);
117117

118-
assertEquals(TEST_DD_VAR_VAL, ConfigHelper.getEnvironmentVariable(TEST_DD_VAR));
119-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
118+
assertEquals(TEST_DD_VAR_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_DD_VAR));
119+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
120120
assertEquals(TEST_DD_VAR_VAL, result.get(TEST_DD_VAR));
121121
assertFalse(result.containsKey(ALIAS_DD_VAR));
122122

@@ -129,8 +129,8 @@ void testMainConfigPrecedence() {
129129
void testNonDDAliases() {
130130
setEnvVar(NON_DD_ALIAS_VAR, NON_DD_ALIAS_VAL);
131131

132-
assertEquals(NON_DD_ALIAS_VAL, ConfigHelper.getEnvironmentVariable(TEST_DD_VAR));
133-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
132+
assertEquals(NON_DD_ALIAS_VAL, ConfigHelper.get().getEnvironmentVariable(TEST_DD_VAR));
133+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
134134
assertEquals(NON_DD_ALIAS_VAL, result.get(TEST_DD_VAR));
135135
assertFalse(result.containsKey(NON_DD_ALIAS_VAR));
136136

@@ -140,7 +140,7 @@ void testNonDDAliases() {
140140

141141
@Test
142142
void testAliasesWithoutPresentAliases() {
143-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
143+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
144144
assertFalse(result.containsKey(ALIAS_DD_VAR));
145145
}
146146

@@ -149,22 +149,23 @@ void testAliasWithEmptyList() {
149149
Map<String, List<String>> aliasMap = new HashMap<>();
150150
aliasMap.put("EMPTY_ALIAS_CONFIG", new ArrayList<>());
151151

152-
ConfigHelper.setConfigurationSource(
153-
new TestSupportedConfigurationSource(
154-
new HashSet<>(), aliasMap, new HashMap<>(), new HashMap<>()));
152+
ConfigHelper.get()
153+
.setConfigurationSource(
154+
new TestSupportedConfigurationSource(
155+
new HashSet<>(), aliasMap, new HashMap<>(), new HashMap<>()));
155156

156-
assertNull(ConfigHelper.getEnvironmentVariable("EMPTY_ALIAS_CONFIG"));
157+
assertNull(ConfigHelper.get().getEnvironmentVariable("EMPTY_ALIAS_CONFIG"));
157158

158159
// Cleanup
159-
ConfigHelper.setConfigurationSource(testSource);
160+
ConfigHelper.get().setConfigurationSource(testSource);
160161
}
161162

162163
@Test
163164
void testAliasSkippedWhenBaseAlreadyPresent() {
164165
setEnvVar(TEST_DD_VAR, TEST_DD_VAR_VAL);
165166
setEnvVar(NON_DD_ALIAS_VAR, NON_DD_ALIAS_VAL);
166167

167-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
168+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
168169
assertEquals(TEST_DD_VAR_VAL, result.get(TEST_DD_VAR));
169170
assertFalse(result.containsKey(NON_DD_ALIAS_VAR));
170171

@@ -177,7 +178,7 @@ void testAliasSkippedWhenBaseAlreadyPresent() {
177178
void testInconsistentAliasesAndAliasMapping() {
178179
setEnvVar(NEW_ALIAS_KEY_2, "some_value");
179180

180-
Map<String, String> result = ConfigHelper.getEnvironmentVariables();
181+
Map<String, String> result = ConfigHelper.get().getEnvironmentVariables();
181182

182183
assertFalse(result.containsKey(NEW_ALIAS_KEY_2));
183184
assertFalse(result.containsKey(NEW_ALIAS_TARGET));
@@ -189,16 +190,16 @@ void testInconsistentAliasesAndAliasMapping() {
189190
// TODO: Update to verify telemetry when implemented
190191
@Test
191192
void testUnsupportedEnvWarningNotInTestMode() {
192-
ConfigHelper.setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST);
193+
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST);
193194

194195
setEnvVar("DD_FAKE_VAR", "banana");
195196

196197
// Should allow unsupported variable in TEST mode
197-
assertEquals("banana", ConfigHelper.getEnvironmentVariable("DD_FAKE_VAR"));
198+
assertEquals("banana", ConfigHelper.get().getEnvironmentVariable("DD_FAKE_VAR"));
198199

199200
// Cleanup
200201
setEnvVar("DD_FAKE_VAR", null);
201-
ConfigHelper.setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
202+
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.STRICT);
202203
}
203204

204205
// Copied from utils.TestHelper

0 commit comments

Comments
 (0)