Skip to content

Commit 5ff7d04

Browse files
authored
Fix role name precedence (#1432)
1 parent e69aa8e commit 5ff7d04

File tree

2 files changed

+15
-95
lines changed

2 files changed

+15
-95
lines changed

agent/agent-bootstrap/src/main/java/com/microsoft/applicationinsights/agent/bootstrap/configuration/ConfigurationBuilder.java

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,17 @@ static void overlayEnvVars(Configuration config) throws IOException {
187187
}
188188
}
189189

190-
config.role.name = overlayWithEnvVars(APPLICATIONINSIGHTS_ROLE_NAME, WEBSITE_SITE_NAME, config.role.name);
191-
config.role.instance = overlayWithEnvVars(APPLICATIONINSIGHTS_ROLE_INSTANCE, WEBSITE_INSTANCE_ID, config.role.instance);
190+
if (isTrimEmpty(config.role.name)) {
191+
// only use WEBSITE_SITE_NAME as a fallback
192+
config.role.name = getEnv(WEBSITE_SITE_NAME);
193+
}
194+
config.role.name = overlayWithEnvVar(APPLICATIONINSIGHTS_ROLE_NAME, config.role.name);
195+
196+
if (isTrimEmpty(config.role.instance)) {
197+
// only use WEBSITE_INSTANCE_ID as a fallback
198+
config.role.name = getEnv(WEBSITE_INSTANCE_ID);
199+
}
200+
config.role.instance = overlayWithEnvVar(APPLICATIONINSIGHTS_ROLE_INSTANCE, config.role.instance);
192201

193202
config.sampling.percentage = overlayWithEnvVar(APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE, config.sampling.percentage);
194203

@@ -200,19 +209,6 @@ static void overlayEnvVars(Configuration config) throws IOException {
200209
addDefaultJmxMetricsIfNotPresent(config);
201210
}
202211

203-
// visible for testing
204-
static String overlayWithEnvVars(String name1, String name2, String defaultValue) {
205-
String value = getEnv(name1);
206-
if (value != null && !value.isEmpty()) {
207-
return value;
208-
}
209-
value = getEnv(name2);
210-
if (value != null && !value.isEmpty()) {
211-
return value;
212-
}
213-
return defaultValue;
214-
}
215-
216212
static String overlayWithEnvVar(String name, String defaultValue) {
217213
String value = getEnv(name);
218214
if (value != null && !value.isEmpty()) {
@@ -242,33 +238,6 @@ private static String getEnv(String name) {
242238
return value;
243239
}
244240

245-
// visible for testing
246-
static Map<String, String> overlayWithEnvVars(String name, Map<String, String> defaultValue) {
247-
String value = System.getenv(name);
248-
if (value != null && !value.isEmpty()) {
249-
Moshi moshi = MoshiBuilderFactory.createBasicBuilder();
250-
JsonAdapter<Map> adapter = moshi.adapter(Map.class);
251-
Map<String, String> stringMap = new HashMap<>();
252-
Map<String, Object> objectMap;
253-
try {
254-
objectMap = adapter.fromJson(value);
255-
} catch (Exception e) {
256-
configurationMessages.add(new ConfigurationMessage("could not parse environment variable {} as json: {}", name, value));
257-
return defaultValue;
258-
}
259-
for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
260-
Object val = entry.getValue();
261-
if (!(val instanceof String)) {
262-
configurationMessages.add(new ConfigurationMessage("currently only string values are supported in json map from {}: {}", name, value));
263-
return defaultValue;
264-
}
265-
stringMap.put(entry.getKey(), (String) val);
266-
}
267-
return stringMap;
268-
}
269-
return defaultValue;
270-
}
271-
272241
// visible for testing
273242
static String trimAndEmptyToNull(String str) {
274243
if (str == null) {
@@ -278,6 +247,10 @@ static String trimAndEmptyToNull(String str) {
278247
return trimmed.isEmpty() ? null : trimmed;
279248
}
280249

250+
private static boolean isTrimEmpty(String value) {
251+
return value != null && !value.trim().isEmpty();
252+
}
253+
281254
public static class ConfigurationException extends RuntimeException {
282255

283256
public ConfigurationException(String message) {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.microsoft.applicationinsights.agent.bootstrap.configuration;
22

3-
import java.util.Collections;
4-
53
import org.junit.*;
64
import org.junit.contrib.java.lang.system.*;
75

@@ -22,55 +20,4 @@ public void testEmptyToNull() {
2220
assertEquals("a", trimAndEmptyToNull(" a "));
2321
assertEquals(null, trimAndEmptyToNull("\t"));
2422
}
25-
26-
@Test
27-
public void testOverlayWithEnvVarWithNone() {
28-
assertEquals("def", ConfigurationBuilder.overlayWithEnvVars("myenv", "myenv2", "def"));
29-
}
30-
31-
@Test
32-
public void testOverlayWithEnvVarWithFirst() {
33-
envVars.set("myenv", "abc");
34-
assertEquals("abc", ConfigurationBuilder.overlayWithEnvVars("myenv", "myenv2", "def"));
35-
}
36-
37-
@Test
38-
public void testOverlayWithEnvVarWithSecond() {
39-
envVars.set("myenv2", "xyz");
40-
assertEquals("xyz", ConfigurationBuilder.overlayWithEnvVars("myenv", "myenv2", "def"));
41-
}
42-
43-
@Test
44-
public void testOverlayWithEnvVarWithBoth() {
45-
envVars.set("myenv", "abc");
46-
envVars.set("myenv2", "xyz");
47-
assertEquals("abc", ConfigurationBuilder.overlayWithEnvVars("myenv", "myenv2", "def"));
48-
}
49-
50-
@Test
51-
public void testOverlayWithEnvVarWithoutMap() {
52-
assertEquals(Collections.singletonMap("one", "two"),
53-
ConfigurationBuilder.overlayWithEnvVars("myenv", Collections.singletonMap("one", "two")));
54-
}
55-
56-
@Test
57-
public void testOverlayWithEnvVarWithMap() {
58-
envVars.set("myenv", "{\"one\": \"2\"}");
59-
assertEquals(Collections.singletonMap("one", "2"), ConfigurationBuilder.overlayWithEnvVars("myenv",
60-
Collections.singletonMap("one", "two")));
61-
}
62-
63-
@Test
64-
public void testOverlayWithEnvVarWithMapAndBadType() {
65-
envVars.set("myenv", "{\"one\": 2}");
66-
assertEquals(Collections.singletonMap("one", "two"), ConfigurationBuilder.overlayWithEnvVars("myenv",
67-
Collections.singletonMap("one", "two")));
68-
}
69-
70-
@Test
71-
public void testOverlayWithEnvVarWithMapAndBadJson() {
72-
envVars.set("myenv", "--some invalid json--");
73-
assertEquals(Collections.singletonMap("one", "two"), ConfigurationBuilder.overlayWithEnvVars("myenv",
74-
Collections.singletonMap("one", "two")));
75-
}
7623
}

0 commit comments

Comments
 (0)