javaazuretomcatazure-application-insightscuba-platform

Application Insights in Cuba Platform


I'm trying to use Azure Application Insight in Cuba Platform: https://learn.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started

I managed to add the library to Cuba Platform, in particular to the Web module, but I couldn't set the "Instrumentation key" in any way.

There are three ways to do so:

1- Putting ApplicationInsights.xml in the "resource" folder

I couldn't find a place to put the file to be read by TelemetryConfigurationFactory class. Internally, I see it's using getResource() to scan in various "sensible" places. I tried WEB-INF, META-INF, conf directory in Tomcat, conf/app, root of java package, work/app in Tomcat and probably something more with no results.

2- System property: -DAPPLICATION_INSIGHTS_IKEY=your_ikey

3- Environment variable: APPLICATION_INSIGHTS_IKEY

Tried both in a docker container, tried the last one locally: no results. In particular, System.getEnv is returning null even after exporting manually the variable locally, so this could be some error on my side

Any insight :D is welcome


Solution

  • I tried to search for the configuration of Instrumentation key from Application Insights source code.

    I can see the snippet of code as below in the TelemetryConfigurationFactory Class.

    /**
         * Setting an instrumentation key:
         * First we try the system property '-DAPPLICATION_INSIGHTS_IKEY=i_key' or '-DAPPINSIGHTS_INSTRUMENTATIONKEY=i_key'
         * Next we will try the environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
         * Next we will try to fetch the i-key from the ApplicationInsights.xml
         * @param userConfiguration The configuration that was represents the user's configuration in ApplicationInsights.xml.
         * @param configuration The configuration class.
         */
        private void setInstrumentationKey(ApplicationInsightsXmlConfiguration userConfiguration, TelemetryConfiguration configuration) {
            try {
                String ikey;
    
                // 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'
                ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME);
                if (!Strings.isNullOrEmpty(ikey)) {
                    configuration.setInstrumentationKey(ikey);
                    return;
                }
                ikey = System.getProperty(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
                if (!Strings.isNullOrEmpty(ikey)) {
                    configuration.setInstrumentationKey(ikey);
                    return;
                }
    
                // Second, try to find the i-key as an environment variable 'APPLICATION_INSIGHTS_IKEY' or 'APPINSIGHTS_INSTRUMENTATIONKEY'
                ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME);
                if (!Strings.isNullOrEmpty(ikey)) {
                    configuration.setInstrumentationKey(ikey);
                    return;
                }
                ikey = System.getenv(EXTERNAL_PROPERTY_IKEY_NAME_SECONDARY);
                if (!Strings.isNullOrEmpty(ikey)) {
                    configuration.setInstrumentationKey(ikey);
                    return;
                }
    
                // Else, try to find the i-key in ApplicationInsights.xml
                if (userConfiguration != null) {
                    ikey = userConfiguration.getInstrumentationKey();
                    if (ikey == null) {
                        return;
                    }
    
                    ikey = ikey.trim();
                    if (ikey.length() == 0) {
                        return;
                    }
    
                    configuration.setInstrumentationKey(ikey);
                }
            } catch (Exception e) {
                InternalLogger.INSTANCE.error("Failed to set instrumentation key: '%s'", e.getMessage());
            }
        }
    

    It seems that Instrumentation key can be configured as a java system property or as an environment variable or in ApplicationInsights.xml.

    According to your description , System.getEnv("XXX") in your code return null. You could check the possible reasons below :

    1. Use My Computer > Advanced > Environment Variables to make a variable visible to all new processes.

      2.The process which tries to read the variable is already running. Restart it.

      3.The start script of Tomcat unsets the variable before it invokes java.exe

      4.Tomcat unsets the variable in it's Java code.

    In addition , you could refer to this thread : System.getenv() returns null when the environment variable exists.

    Hope it helps you.