1
votes

I am building a Java webapp (Spring webapp using Maven build) on Azure and using Application Insights for monitoring. I used the reference link https://azure.microsoft.com/en-us/documentation/articles/app-insights-java-get-started/.

Since I use multiple environments I planned to pass the App Insights instrumentation Key as a system property from azure portal APP_SETTING (JAVA_OPTS value as -Dappinsight.instrumentation.key=xxxxxxx).

I have added required Maven dependencies and my src\main\resources\ApplicationInsights.xml has the App Insight instrumentation key reference as:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">


  <!-- The key from the portal: -->

  <InstrumentationKey>${appinsight.instrumentation.key}</InstrumentationKey>


  <!-- HTTP request component (not required for bare API) -->

  <TelemetryModules>
    <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebRequestTrackingTelemetryModule"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebSessionTrackingTelemetryModule"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebUserTrackingTelemetryModule"/>
  </TelemetryModules>

  <!-- Events correlation (not required for bare API) -->
  <!-- These initializers add context data to each event -->

  <TelemetryInitializers>
    <Add   type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationIdTelemetryInitializer"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationNameTelemetryInitializer"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebSessionTelemetryInitializer"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserTelemetryInitializer"/>
    <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserAgentTelemetryInitializer"/>

  </TelemetryInitializers>
</ApplicationInsights>

But it doesn't work. When I hardcode the key directly, it works.

Is there any specific way of referencing the system properties for Application insights in Spring?

1
I have same concern on replacing dynamic application insight instrument key in ApplicationInsights.xml based on the environment. Could you share possible approach ? - Uday Kiran

1 Answers

4
votes

The instrumentation key provided in the configuration file is taken as is, and therefore specifying a system property will not help.

Although it is not documented, AI Java SDK tries to resolve the instrumentation key in the following order:

  1. System property: -DAPPLICATION_INSIGHTS_IKEY=your_ikey
  2. Environment variable: APPLICATION_INSIGHTS_IKEY
  3. Configuration file: ApplicationInsights.xml.

So I guess one of the first two options will satisfy you.

The SDK is open-source, you can read the code here: TelemetryConfigurationFactory.setInstrumentationKey