Hot questions for Using Azure in azure application insights

Top Java Programmings / Azure / azure application insights

Question:

I followed below article to set up appinsights to java dynamic web project. However I get the below mentioned error

https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started Error Stack trace: Jan 05, 2017 5:40:07 PM org.apache.catalina.core.StandardContext filterStart SEVERE: Exception starting filter ApplicationInsightsWebFilter java.lang.ClassNotFoundException: com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:257) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:383) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:104) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)


Answer:

As a summary, seems that you had resolved the issue which might be caused by not automatically loading the added jar files on Tomcat via Eclipse. The solution is that as below.

  1. Move to the Properties > Deployment Assembly of the current project, and check whether the dependencies exists there as Build Path entries or Archives.
  2. Select the current project, and click Project > Clean at the menu bar, then restart the server.

Question:

I'm trying to use Azure Application Insight in Cuba Platform: https://docs.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


Answer:

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.

Question:

I've got an Application Insights resource in Azure. In our Java application, we have a log4j.xml file which writes to multiple log files depending on the service name.

I'm adding the Application Insights appender to the logging, and I'm able to see all the logs of the different log files in the Application Insights resource. However, I'm unable to filter by the log file name in Application Insights, so the trace logs are not useful.

Is there a way to get the log file name into the logging event properties? This way, in AppInsights we would be able to filter by log file name, and read them as if we had the log files there.

Edit: our log4j.xml looks something like this, with aiAppender in it for application insights

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="fileout" class="org.apache.log4j.DailyRollingFileAppender"> 
    <param name="datePattern" value="'.'yyyyMMdd"/>
    <param name="file" value="${app.install.home}/log/${app.server.name}-${app.service.process.host}.log"/>
    <param name="append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d %5p [%t] (%C) - %m%n"/> 
    </layout> 
</appender>
<appender name="aiAppender" 
    class="com.microsoft.applicationinsights.log4j.v1_2.ApplicationInsightsAppender">
</appender>    
<root> 
    <priority value ="INFO" /> 
    <appender-ref ref="fileout"/>
    <appender-ref ref="aiAppender" />
</root>


Answer:

You can implement a TelemetryInitializer to add the service name to the telemetry properties, which can be used for grouping.

You telemetry initializer should look like the following:

public class ServiceNameInitializer implements TelemetryInitializer {
    @Override
    public void initialize(Telemetry telemetry) {
        if (telemetry instanceof TraceTelemetry) {
            telemetry.getProperties().put("ServiceName", ServiceNameFromConfigurationOrProcessName);
        }
    }
}

Note that the condition can be removed - this will add the property to all telemetry types.

Once you've implemented the initializer, add it to the configuration file, inside the TelemetryInitializers element:

<TelemetryInitializers>
    .
    .
    <Add type="your.initializer.package.ServiceNameInitializer"/>
</TelemetryInitializers>

Question:

I am adding few custom properties to the RequestTelemetry object as below

Java Web app

RequestTelemetry.getProperties.put(key,value);

But the same doesn't reflect in the portal.

So i created a new telemetryClient and invoked telemettyClient.trackRequest() but it creates a duplicate request entry in portal.

Is there a way to achieve the same without duplicating the telemetry object.

TIA


Answer:

Telemetry Modules are the only place where we get access to http requests response objects in AI Java Sdk. Here's a blog I wrote that explains how to send custom http data via request telemetry

https://dtechonline.wordpress.com

Question:

I am trying to create a custom telemetry module in Java which can log custom http requests and responses headers. From my analysis telemetry modules are the one that has access to http request and http response objects.

I created my own telemetry module by extending the Webtelemetrymodule interface and registered the same using Application insights.xml

When my app boots I get error saying Failed to load class for my CustomTelemetry class

Am I missing something?? there isn't much documentation about telemetry modules, so any help will be greatly appreciated

Code

The below is my custom telemetry module and it's registered in ApplicationInsights.xml

public class CustomRequestTelemetryModule implements WebTelemetryModule {

public static final String REQUEST_ID_HEADER = "CUSTOM_HEADER";



@Override
public void onBeginRequest(ServletRequest request, ServletResponse response) {

}

@Override
public void onEndRequest(ServletRequest req, ServletResponse res) {

    //logger.info("Inside Custom Telemetry Module");

    HttpServletResponse response = (HttpServletResponse)res;

    String REQID = response.getHeader("REQUEST_ID_HEADER");

    RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext();
    RequestTelemetry telemetry = context.getHttpRequestTelemetry();

    //telemetry.setId(REQID);
    telemetry.getProperties().put("ID",REQID);



}

}

Application Insight Config

Blockquote

    <Add type="custompackage.CustomRequestTelemetryModule"/>

Logged Error by AI

AI: ERROR 16-05-2017 12:13, 15: Failed to create custompackage..CustomRequestTelemetryModule, class custompackage.CustomRequestTelemetryModule

TIA


Answer:

The class needs to implement WebTelemetryModule along with TelemetryModule interface. Doing so resolved this issue for me.

Here's a blog that I wrote which explains how this can be done in detail https://dtechonline.wordpress.com

Question:

In a sample Java web application I tried to insert custom metric but not seeing in the AppInsights metric explorer nor Customevents table. I see the following error in the console window.

Configuration file has been successfully found in: 'C:\Users...\WebAppCopy\WEB-INF\lib\ApplicationInsights.xml' AI: ERROR 08-01-2017 18:14, 14: Failed to create empty class name AI: ERROR 08-01-2017 18:14, 14: Failed to create DeadLockDetector, exception: null AI: ERROR 08-01-2017 18:14, 14: Failed to create JvmHeapMemoryUsedPerformanceCounter, exception: null Jan 08, 2017 6:14:48 PM org.apache.coyote.AbstractProtocol start


Answer:

The printings are not the problem, it is an internal bug which will be fixed in the upcoming new version.

You can enable the internal logger and check for possible errors.

You Can also send the me the ApplicationInsights.xml to gupele@microsoft.com