Skip to content

Commit 6483a3c

Browse files
committed
Add second environment variable to configure ikey
None of the other SDKs use the `APPLICATION_INSIGHTS_IKEY` environment variable to configure the instrumentation key for Application Insights and instead use `APPINSIGHTS_INSTRUMENTATIONKEY`. For consistency with the other SDKs (e.g. Dot-Net https://aka.ms/n2ruy1 or Node https://aka.ms/v8r0pk) this Java SDK should also support the more common environment variable name to avoid confusion for developers who have experience with the non-Java SDKs.
1 parent 21d4b1f commit 6483a3c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/config/TelemetryConfigurationFactory.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public enum TelemetryConfigurationFactory {
6060
private String performanceCountersSection = DEFAULT_PERFORMANCE_MODULES_PACKAGE;
6161

6262
final static String EXTERNAL_PROPERTY_IKEY_NAME = "APPLICATION_INSIGHTS_IKEY";
63+
final static String EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY = "APPINSIGHTS_INSTRUMENTATIONKEY";
6364

6465
private AppInsightsConfigurationBuilder builder = new JaxbAppInsightsConfigurationBuilder();
6566

@@ -208,28 +209,39 @@ private void setTelemetryProcessors(ApplicationInsightsXmlConfiguration appConfi
208209

209210
/**
210211
* Setting an instrumentation key:
211-
* First we try the system property '-DAPPLICATION_INSIGHTS_IKEY=i_key'
212-
* Next we will try the environment variable 'APPLICATION_INSIGHTS_IKEY',
212+
* First we try the system property '-DAPPLICATION_INSIGHTS_IKEY=i_key' or '-DAPPINSIGHTS_INSTRUMENTATIONKEY=i_key'
213+
* Next we will try the environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
213214
* Next we will try to fetch the i-key from the ApplicationInsights.xml
214215
* @param userConfiguration The configuration that was represents the user's configuration in ApplicationInsights.xml.
215216
* @param configuration The configuration class.
216217
*/
217218
private void setInstrumentationKey(ApplicationInsightsXmlConfiguration userConfiguration, TelemetryConfiguration configuration) {
218219
try {
219-
// First, check whether an i-key was provided as a java system property i.e. '-DAPPLICATION_INSIGHTS_IKEY=i_key'
220-
String ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME);
220+
String ikey;
221221

222+
// First, check whether an i-key was provided as a java system property i.e. '-DAPPLICATION_INSIGHTS_IKEY=i_key', or '-DAPPINSIGHTS_INSTRUMENTATIONKEY=i_key'
223+
ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME);
224+
if (!Strings.isNullOrEmpty(ikey)) {
225+
configuration.setInstrumentationKey(ikey);
226+
return;
227+
}
228+
ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
222229
if (!Strings.isNullOrEmpty(ikey)) {
223230
configuration.setInstrumentationKey(ikey);
224231
return;
225232
}
226233

227-
// Second, try to find the i-key as an environment variable 'APPLICATION_INSIGHTS_IKEY'
234+
// Second, try to find the i-key as an environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
228235
ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME);
229236
if (!Strings.isNullOrEmpty(ikey)) {
230237
configuration.setInstrumentationKey(ikey);
231238
return;
232239
}
240+
ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
241+
if (!Strings.isNullOrEmpty(ikey)) {
242+
configuration.setInstrumentationKey(ikey);
243+
return;
244+
}
233245

234246
// Else, try to find the i-key in ApplicationInsights.xml
235247
if (userConfiguration != null) {

core/src/test/java/com/microsoft/applicationinsights/internal/config/TelemetryConfigurationFactoryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ public void systemPropertyIKeyBeforeConfigurationIKeyTest() {
117117
}
118118
}
119119

120+
@Test
121+
public void systemPropertyIKeySecondaryBeforeConfigurationIKeyTest() {
122+
try {
123+
System.setProperty(TelemetryConfigurationFactory.EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY, APP_INSIGHTS_IKEY_TEST_VALUE);
124+
ikeyTest(MOCK_IKEY, APP_INSIGHTS_IKEY_TEST_VALUE);
125+
} finally {
126+
// Avoid any influence on other unit tests
127+
System.getProperties().remove(TelemetryConfigurationFactory.EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
128+
}
129+
}
130+
120131
@Test
121132
public void testWithEmptySections() {
122133
AppInsightsConfigurationBuilder mockParser = Mockito.mock(AppInsightsConfigurationBuilder.class);

0 commit comments

Comments
 (0)