Skip to content

Commit a19c835

Browse files
committed
refactor conditions and add unit tests
1 parent 8c6c78b commit a19c835

2 files changed

Lines changed: 141 additions & 22 deletions

File tree

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizer.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ public Map<String, String> apply(ConfigProperties otelConfig) {
117117
}
118118

119119
String metricsExporter = otelConfig.getString("otel.metrics.exporter");
120-
if (isAksAttach(otelConfig)) {
121-
properties.put("otel.metrics.exporter", addAzureMonitorIfNotPresent(metricsExporter));
120+
String aksNamespaceId = System.getenv("AKS_ARM_NAMESPACE_ID");
121+
String metricsToLogAnalyticsEnabled = otelConfig.getString("applicationinsights.metrics.to.loganalytics.enabled");
122+
if (isAksAttach(aksNamespaceId)) {
123+
properties.put("otel.metrics.exporter", updateMetricsExporter(metricsExporter, metricsToLogAnalyticsEnabled));
122124
} else if (metricsExporter == null) {
123125
// this overrides the default "otlp" so the exporter can be configured later
124126
properties.put("otel.metrics.exporter", "none");
@@ -330,26 +332,6 @@ private static void enableInstrumentations(
330332
}
331333
}
332334

333-
private static boolean isAksAttach(ConfigProperties otelConfig) {
334-
String envVar = otelConfig.getString("applicationinsights.metrics.to.loganalytics.enabled");
335-
boolean metricsToLogAnalyticsEnabled = envVar == null || Boolean.parseBoolean(envVar);
336-
337-
String aksNamespaceId = System.getenv("AKS_ARM_NAMESPACE_ID");
338-
boolean isAks = !Strings.isNullOrEmpty(aksNamespaceId);
339-
340-
return metricsToLogAnalyticsEnabled && isAks;
341-
}
342-
343-
private static String addAzureMonitorIfNotPresent(String metricsExporter) {
344-
String azureMonitorName = AzureMonitorExporterProviderKeys.EXPORTER_NAME;
345-
if (metricsExporter == null || metricsExporter.isEmpty() || metricsExporter.equals("none")) {
346-
return azureMonitorName;
347-
} else if (!metricsExporter.contains(azureMonitorName)) {
348-
return metricsExporter + "," + azureMonitorName;
349-
}
350-
return metricsExporter;
351-
}
352-
353335
private static void setHttpHeaderConfiguration(
354336
Map<String, String> properties, String propertyName, List<String> headers) {
355337
if (!headers.isEmpty()) {
@@ -367,4 +349,50 @@ private static <T> String join(List<T> values, char separator) {
367349
}
368350
return sb.toString();
369351
}
352+
353+
// visible for tests
354+
static boolean isAksAttach(String aksNamespaceId) {
355+
return !Strings.isNullOrEmpty(aksNamespaceId);
356+
}
357+
358+
static String updateMetricsExporter(String metricsExporter, String metricsToLogAnalyticsEnabled) {
359+
String azureMonitorName = AzureMonitorExporterProviderKeys.EXPORTER_NAME;
360+
if (metricsExporter == null || metricsExporter.isEmpty()) {
361+
if (metricsToLogAnalyticsEnabled == null || Boolean.parseBoolean(metricsToLogAnalyticsEnabled)) {
362+
return azureMonitorName + ",otlp";
363+
} else {
364+
return azureMonitorName;
365+
}
366+
}
367+
368+
if (metricsToLogAnalyticsEnabled == null || metricsToLogAnalyticsEnabled.isEmpty()) {
369+
if (metricsExporter.contains(azureMonitorName) && !metricsExporter.contains("otlp")) {
370+
return metricsExporter + ",otlp";
371+
}
372+
return metricsExporter;
373+
}
374+
375+
// If AMLE is true, make sure both otlp and azure monitor exporters are present
376+
if (Boolean.parseBoolean(metricsToLogAnalyticsEnabled)) {
377+
if (metricsExporter == null || metricsExporter.isEmpty() || metricsExporter.equals("none")) {
378+
return azureMonitorName + ",otlp";
379+
}
380+
if (!metricsExporter.contains(azureMonitorName)) {
381+
metricsExporter += "," + azureMonitorName;
382+
}
383+
if (!metricsExporter.contains("otlp")) {
384+
metricsExporter += ",otlp";
385+
}
386+
return metricsExporter;
387+
} else {
388+
// If AMLE is false, make sure only azure monitor exporter is present
389+
if (metricsExporter == null || metricsExporter.isEmpty()) {
390+
return azureMonitorName;
391+
}
392+
if (metricsExporter.contains(azureMonitorName) && metricsExporter.contains("otlp")) {
393+
return metricsExporter.replace(",otlp", "").replace("otlp,", "");
394+
}
395+
return metricsExporter;
396+
}
397+
}
370398
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.applicationinsights.agent.internal.init;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
import java.util.stream.Stream;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.Arguments;
13+
import org.junit.jupiter.params.provider.MethodSource;
14+
15+
class AiConfigCustomizerTest {
16+
17+
@Test
18+
void isAksAttach() {
19+
assertThat(AiConfigCustomizer.isAksAttach("dummy-aks-namespace")).isTrue();
20+
21+
assertThat(AiConfigCustomizer.isAksAttach(null)).isFalse();
22+
assertThat(AiConfigCustomizer.isAksAttach("")).isFalse();
23+
}
24+
25+
@Test
26+
void updateMetricsExporter_ExporterUnset() {
27+
28+
assertThat(AiConfigCustomizer.updateMetricsExporter(null, null))
29+
.isEqualTo("azure_monitor,otlp");
30+
31+
assertThat(AiConfigCustomizer.updateMetricsExporter("", null))
32+
.isEqualTo("azure_monitor,otlp");
33+
34+
assertThat(AiConfigCustomizer.updateMetricsExporter(null, "true"))
35+
.isEqualTo("azure_monitor,otlp");
36+
37+
assertThat(AiConfigCustomizer.updateMetricsExporter(null, "True"))
38+
.isEqualTo("azure_monitor,otlp");
39+
}
40+
41+
static Stream<Arguments> stringPairs() {
42+
return Stream.of(
43+
Arguments.of("none", "none"),
44+
Arguments.of("azure_monitor", "azure_monitor,otlp"),
45+
Arguments.of("azure_monitor,otlp", "azure_monitor,otlp"),
46+
Arguments.of("otlp", "otlp"));
47+
}
48+
49+
@ParameterizedTest
50+
@MethodSource("stringPairs")
51+
void updateMetricsExporter_ExporterSet_AMLE_Unset(String metricsExporter, String expectAzureMonitor) {
52+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, null))
53+
.isEqualTo(expectAzureMonitor);
54+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, ""))
55+
.isEqualTo(expectAzureMonitor);
56+
}
57+
58+
static Stream<Arguments> stringPairs2() {
59+
return Stream.of(
60+
Arguments.of("none", "azure_monitor,otlp"),
61+
Arguments.of("azure_monitor", "azure_monitor,otlp"),
62+
Arguments.of("azure_monitor,otlp", "azure_monitor,otlp"),
63+
Arguments.of("otlp", "otlp,azure_monitor"));
64+
}
65+
66+
@ParameterizedTest
67+
@MethodSource("stringPairs2")
68+
void updateMetricsExporter_ExporterSet_AMLE_True(String metricsExporter, String expectAzureMonitor) {
69+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, "true"))
70+
.isEqualTo(expectAzureMonitor);
71+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, "True"))
72+
.isEqualTo(expectAzureMonitor);
73+
}
74+
75+
static Stream<Arguments> stringPairs3() {
76+
return Stream.of(
77+
Arguments.of("none", "none"),
78+
Arguments.of("azure_monitor", "azure_monitor"),
79+
Arguments.of("azure_monitor,otlp", "azure_monitor"),
80+
Arguments.of("otlp", "otlp"));
81+
}
82+
83+
@ParameterizedTest
84+
@MethodSource("stringPairs3")
85+
void updateMetricsExporter_ExporterSet_AMLE_False(String metricsExporter, String expectAzureMonitor) {
86+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, "false"))
87+
.isEqualTo(expectAzureMonitor);
88+
assertThat(AiConfigCustomizer.updateMetricsExporter(metricsExporter, "False"))
89+
.isEqualTo(expectAzureMonitor);
90+
}
91+
}

0 commit comments

Comments
 (0)