Skip to content

Commit 443c99c

Browse files
committed
simplify
1 parent 68dcbc9 commit 443c99c

File tree

4 files changed

+105
-135
lines changed

4 files changed

+105
-135
lines changed

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

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import static java.util.concurrent.TimeUnit.SECONDS;
77

8-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.AzureMonitorExporterProviderKeys;
9-
import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.Strings;
108
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
119
import com.microsoft.applicationinsights.agent.internal.legacyheaders.DelegatingPropagatorProvider;
1210
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
@@ -117,14 +115,7 @@ public Map<String, String> apply(ConfigProperties otelConfig) {
117115
}
118116

119117
String metricsExporter = otelConfig.getString("otel.metrics.exporter");
120-
String aksNamespaceId = System.getenv("AKS_ARM_NAMESPACE_ID");
121-
String metricsToLogAnalyticsEnabled =
122-
otelConfig.getString("applicationinsights.metrics.to.loganalytics.enabled");
123-
if (isAksAttach(aksNamespaceId)) {
124-
properties.put(
125-
"otel.metrics.exporter",
126-
updateMetricsExporter(metricsExporter, metricsToLogAnalyticsEnabled));
127-
} else if (metricsExporter == null) {
118+
if (metricsExporter == null) {
128119
// this overrides the default "otlp" so the exporter can be configured later
129120
properties.put("otel.metrics.exporter", "none");
130121
}
@@ -352,37 +343,4 @@ private static <T> String join(List<T> values, char separator) {
352343
}
353344
return sb.toString();
354345
}
355-
356-
// visible for tests
357-
static boolean isAksAttach(String aksNamespaceId) {
358-
return !Strings.isNullOrEmpty(aksNamespaceId);
359-
}
360-
361-
static String updateMetricsExporter(String metricsExporter, String metricsToLogAnalyticsEnabled) {
362-
String azureMonitorName = AzureMonitorExporterProviderKeys.EXPORTER_NAME;
363-
// If AMLE is true, configure both otlp and azure monitor exporters
364-
if (Boolean.parseBoolean(metricsToLogAnalyticsEnabled)) {
365-
return azureMonitorName + ",otlp";
366-
}
367-
368-
// If AMLE is unset:
369-
if (metricsToLogAnalyticsEnabled == null || metricsToLogAnalyticsEnabled.isEmpty()) {
370-
if (metricsExporter == null || metricsExporter.isEmpty()) {
371-
// default is "azure_monitor,otlp"
372-
return azureMonitorName + ",otlp";
373-
} else if (metricsExporter.contains(azureMonitorName) && !metricsExporter.contains("otlp")) {
374-
// if azure monitor is already present and otlp is not, add otlp
375-
metricsExporter += ",otlp";
376-
}
377-
return metricsExporter;
378-
}
379-
380-
// If AMLE is false, configure only azure monitor exporter.
381-
// 1. Default is azure monitor
382-
// 2. If otlp is set, cancel it and replace with azure monitor (AMLE false has higher priority than otlp setting)
383-
if (metricsExporter == null || metricsExporter.isEmpty() || metricsExporter.contains(azureMonitorName)) {
384-
return azureMonitorName;
385-
}
386-
return metricsExporter;
387-
}
388346
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
264264
return props;
265265
})
266266
.addPropertiesCustomizer(new AiConfigCustomizer())
267+
.addPropertiesCustomizer(
268+
otelConfig -> {
269+
Map<String, String> props = new HashMap<>();
270+
if (isAksAttach()) {
271+
String metricsExporter = otelConfig.getString("otel.metrics.exporter");
272+
String amle =
273+
otelConfig.getString("applicationinsights.metrics.to.loganalytics.enabled");
274+
props.put(
275+
"otel.metrics.exporter",
276+
conditionallyAddAzureMonitorExporter(metricsExporter, amle));
277+
}
278+
return props;
279+
})
267280
.addSpanExporterCustomizer(
268281
(spanExporter, configProperties) -> {
269282
if (spanExporter instanceof AzureMonitorSpanExporterProvider.MarkerSpanExporter) {
@@ -798,4 +811,47 @@ private static CompletableResultCode flushAll(
798811
});
799812
return overallResult;
800813
}
814+
815+
private static boolean isAksAttach() {
816+
return !Strings.isNullOrEmpty(System.getenv("AKS_ARM_NAMESPACE_ID"));
817+
}
818+
819+
// visible for tests
820+
// Per spec: when amle=true, ensure azure_monitor is included; otherwise respect user's setting
821+
// see
822+
// https://github.com/aep-health-and-standards/Telemetry-Collection-Spec/blob/main/ApplicationInsights/AutoAttach_Env_Vars.md#metrics-exporter
823+
static String conditionallyAddAzureMonitorExporter(String metricsExporter, String amle) {
824+
825+
// Default to azure_monitor when not set
826+
if (Strings.isNullOrEmpty(metricsExporter)) {
827+
// Note: this won't really happen since we default otel.metrics.exporter
828+
// already in the PropertiesSupplier above which runs before this
829+
return AzureMonitorExporterProviderKeys.EXPORTER_NAME;
830+
}
831+
832+
// When amle=true, ensure azure_monitor is included
833+
if ("true".equals(amle)) {
834+
if ("none".equals(metricsExporter)) {
835+
return AzureMonitorExporterProviderKeys.EXPORTER_NAME;
836+
}
837+
if (!containsAzureMonitor(metricsExporter)) {
838+
return metricsExporter + "," + AzureMonitorExporterProviderKeys.EXPORTER_NAME;
839+
}
840+
}
841+
842+
return metricsExporter;
843+
}
844+
845+
// visible for tests
846+
static boolean containsAzureMonitor(String metricsExporter) {
847+
if (metricsExporter == null) {
848+
return false;
849+
}
850+
for (String exporter : metricsExporter.split(",")) {
851+
if (AzureMonitorExporterProviderKeys.EXPORTER_NAME.equals(exporter.trim())) {
852+
return true;
853+
}
854+
}
855+
return false;
856+
}
801857
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/init/AiConfigCustomizerTest.java

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
class SecondEntryPointTest {
14+
15+
// Test cases matching the spec table in
16+
// https://github.com/aep-health-and-standards/Telemetry-Collection-Spec/blob/main/ApplicationInsights/AutoAttach_Env_Vars.md#metrics-exporter
17+
//
18+
// OTEL_METRICS_EXPORTER | AMLE | azure_monitor included
19+
// ----------------------|--------|------------------------
20+
static Stream<Arguments> metricsExporterSpecTable() {
21+
return Stream.of(
22+
// AMLE unset
23+
Arguments.of(null, null, true),
24+
Arguments.of("none", null, false),
25+
Arguments.of("azure_monitor", null, true),
26+
Arguments.of("otlp,azure_monitor", null, true),
27+
Arguments.of("otlp", null, false),
28+
// AMLE=true (always include azure_monitor)
29+
Arguments.of(null, "true", true),
30+
Arguments.of("none", "true", true),
31+
Arguments.of("azure_monitor", "true", true),
32+
Arguments.of("otlp,azure_monitor", "true", true),
33+
Arguments.of("otlp", "true", true),
34+
// AMLE=false (same as unset)
35+
Arguments.of(null, "false", true),
36+
Arguments.of("none", "false", false),
37+
Arguments.of("azure_monitor", "false", true),
38+
Arguments.of("otlp,azure_monitor", "false", true),
39+
Arguments.of("otlp", "false", false));
40+
}
41+
42+
@ParameterizedTest(name = "exporter={0}, amle={1} -> included={2}")
43+
@MethodSource("metricsExporterSpecTable")
44+
void testUpdateMetricsExporter(String exporter, String amle, boolean expectAzureMonitor) {
45+
String result = SecondEntryPoint.conditionallyAddAzureMonitorExporter(exporter, amle);
46+
assertThat(SecondEntryPoint.containsAzureMonitor(result)).isEqualTo(expectAzureMonitor);
47+
}
48+
}

0 commit comments

Comments
 (0)