Skip to content

Commit 15a0321

Browse files
Make env vars and sys props always win for rpConfiguration. (#1918)
* Make env vars and sys props always win for rpConfiguration. * Env vars and sys props will always overlay rpConfigurations. * Only impact connectionString and samplingRate. * Add UT to cover this. Signed-off-by: Pan Li <[email protected]> * Fix test * Spotless * Remove String.format * Minor updates * Minor updates * Minor updates * Oops * Annotation * Fix UT. Signed-off-by: Pan Li <[email protected]> Co-authored-by: Trask Stalnaker <[email protected]>
1 parent dc0b83a commit 15a0321

File tree

5 files changed

+113
-60
lines changed

5 files changed

+113
-60
lines changed

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Locale;
4242
import java.util.Map;
43+
import org.checkerframework.checker.nullness.qual.Nullable;
4344
import org.slf4j.LoggerFactory;
4445

4546
public class ConfigurationBuilder {
@@ -154,11 +155,15 @@ public static Configuration create(Path agentJarPath, RpConfiguration rpConfigur
154155
+ " so no need to enable it under preview configuration");
155156
}
156157

157-
overlayEnvVars(config);
158-
applySamplingPercentageRounding(config);
158+
overlayFromEnv(config);
159+
config.sampling.percentage = roundToNearest(config.sampling.percentage, true);
160+
for (SamplingOverride override : config.preview.sampling.overrides) {
161+
override.percentage = roundToNearest(override.percentage, true);
162+
}
159163
// rp configuration should always be last (so it takes precedence)
160164
// currently applicationinsights-rp.json is only used by Azure Spring Cloud
161165
if (rpConfiguration != null) {
166+
overlayFromEnv(rpConfiguration);
162167
overlayRpConfiguration(config, rpConfiguration);
163168
}
164169
// only set role instance to host name as a last resort
@@ -346,22 +351,8 @@ public static void logConfigurationWarnMessages() {
346351
}
347352

348353
// visible for testing
349-
static void overlayEnvVars(Configuration config) throws IOException {
350-
config.connectionString =
351-
overlayWithSysPropEnvVar(
352-
APPLICATIONINSIGHTS_CONNECTION_STRING_SYS,
353-
APPLICATIONINSIGHTS_CONNECTION_STRING_ENV,
354-
config.connectionString);
355-
if (config.connectionString == null) {
356-
// this is for backwards compatibility only
357-
String instrumentationKey = getEnvVar(APPINSIGHTS_INSTRUMENTATIONKEY);
358-
if (instrumentationKey != null) {
359-
configurationLogger.warn(
360-
"APPINSIGHTS_INSTRUMENTATIONKEY is only supported for backwards compatibility,"
361-
+ " please consider using APPLICATIONINSIGHTS_CONNECTION_STRING instead");
362-
config.connectionString = "InstrumentationKey=" + instrumentationKey;
363-
}
364-
}
354+
static void overlayFromEnv(Configuration config) throws IOException {
355+
config.connectionString = overlayConnectionStringFromEnv(config.connectionString);
365356

366357
if (isTrimEmpty(config.role.name)) {
367358
// only use WEBSITE_SITE_NAME as a fallback
@@ -418,11 +409,34 @@ static void overlayEnvVars(Configuration config) throws IOException {
418409
overlayInstrumentationEnabledEnvVars(config);
419410
}
420411

421-
public static void applySamplingPercentageRounding(Configuration config) {
422-
config.sampling.percentage = roundToNearest(config.sampling.percentage, true);
423-
for (SamplingOverride override : config.preview.sampling.overrides) {
424-
override.percentage = roundToNearest(override.percentage, true);
412+
public static void overlayFromEnv(RpConfiguration config) {
413+
config.connectionString = overlayConnectionStringFromEnv(config.connectionString);
414+
config.sampling.percentage =
415+
overlayWithEnvVar(APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE, config.sampling.percentage);
416+
}
417+
418+
@Nullable
419+
private static String overlayConnectionStringFromEnv(String connectionString) {
420+
String value =
421+
overlayWithSysPropEnvVar(
422+
APPLICATIONINSIGHTS_CONNECTION_STRING_SYS,
423+
APPLICATIONINSIGHTS_CONNECTION_STRING_ENV,
424+
connectionString);
425+
426+
if (value != null) {
427+
return value;
428+
}
429+
430+
// this is for backwards compatibility only
431+
String instrumentationKey = getEnvVar(APPINSIGHTS_INSTRUMENTATIONKEY);
432+
if (instrumentationKey != null) {
433+
configurationLogger.warn(
434+
"APPINSIGHTS_INSTRUMENTATIONKEY is only supported for backwards compatibility,"
435+
+ " please consider using APPLICATIONINSIGHTS_CONNECTION_STRING instead");
436+
return "InstrumentationKey=" + instrumentationKey;
425437
}
438+
439+
return null;
426440
}
427441

428442
// visible for testing

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public void run() {
9595
RpConfiguration newRpConfiguration =
9696
RpConfigurationBuilder.loadJsonConfigFile(rpConfiguration.configPath);
9797

98+
ConfigurationBuilder.overlayFromEnv(newRpConfiguration);
99+
98100
if (!newRpConfiguration.connectionString.equals(rpConfiguration.connectionString)) {
99101
logger.debug(
100102
"Connection string from the JSON config file is overriding the previously configured connection string.");

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/configuration/ConfigurationBuilderTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
2525
import static org.assertj.core.api.Assertions.assertThatThrownBy;
26+
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable;
2627

2728
import com.microsoft.applicationinsights.agent.internal.common.FriendlyException;
2829
import java.io.File;
@@ -98,4 +99,40 @@ void testGetJsonEncodingExceptionMessage() {
9899
.isEqualTo(
99100
"Application Insights Java agent's configuration file path/to/file has a malformed JSON\n");
100101
}
102+
103+
@Test
104+
void testRpConfigurationOverlayWithEnvVarAndSysPropUnchanged() {
105+
String testConnectionString = "test-connection-string";
106+
float testSamplingPercentage = 10.0f;
107+
RpConfiguration config = new RpConfiguration();
108+
109+
config.connectionString = testConnectionString;
110+
config.sampling.percentage = testSamplingPercentage;
111+
112+
ConfigurationBuilder.overlayFromEnv(config);
113+
114+
assertThat(config.connectionString).isEqualTo(testConnectionString);
115+
assertThat(config.sampling.percentage).isEqualTo(testSamplingPercentage);
116+
}
117+
118+
@Test
119+
void testRpConfigurationOverlayWithEnvVarAndSysPropPopulated() throws Exception {
120+
String testConnectionString = "test-connection-string";
121+
float testSamplingPercentage = 10.0f;
122+
123+
withEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING", testConnectionString)
124+
.and("APPLICATIONINSIGHTS_SAMPLING_PERCENTAGE", String.valueOf(testSamplingPercentage))
125+
.execute(
126+
() -> {
127+
RpConfiguration config = new RpConfiguration();
128+
129+
config.connectionString = String.format("original-%s", testConnectionString);
130+
config.sampling.percentage = testSamplingPercentage + 1.0f;
131+
132+
ConfigurationBuilder.overlayFromEnv(config);
133+
134+
assertThat(config.connectionString).isEqualTo(testConnectionString);
135+
assertThat(config.sampling.percentage).isEqualTo(testSamplingPercentage);
136+
});
137+
}
101138
}

0 commit comments

Comments
 (0)