@@ -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) {
0 commit comments