Application Insights for Java web apps that are already live
If you have a web application that is already running on your J2EE server, you can start monitoring it with Application Insights without the need to make code changes or recompile your project. With this option, you get information about HTTP requests sent to your server, unhandled exceptions, and performance counters.
You'll need a subscription to Microsoft Azure.
The procedure on this page adds the SDK to your web app at runtime. This runtime instrumentation is useful if you don't want to update or rebuild your source code. But if you can, we recommend you add the SDK to the source code instead. That gives you more options such as writing code to track user activity.
1. Get an Application Insights instrumentation key
- Sign in to the Microsoft Azure portal
Create a new Application Insights resource and set the application type to Java web application.
The resource is created in a few seconds.
Open the new resource and get its instrumentation key. You'll need to paste this key into your code project shortly.
2. Download the SDK
- Download the Application Insights SDK for Java.
- On your server, extract the SDK contents to the directory from which your project binaries are loaded. If you’re using Tomcat, this directory would typically be under
Note that you need to repeat this on each server instance, and for each app.
3. Add an Application Insights xml file
Create ApplicationInsights.xml in the folder in which you added the SDK. Put into it the following XML.
Substitute the instrumentation key that you got from the Azure portal.
<?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>** Your 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>
- The instrumentation key is sent along with every item of telemetry and tells Application Insights to display it in your resource.
- The HTTP Request component is optional. It automatically sends telemetry about requests and response times to the portal.
- Events correlation is an addition to the HTTP request component. It assigns an identifier to each request received by the server, and adds this identifier as a property to every item of telemetry as the property 'Operation.Id'. It allows you to correlate the telemetry associated with each request by setting a filter in diagnostic search.
4. Add an HTTP filter
Locate and open the web.xml file in your project, and merge the following snippet of code under the web-app node, where your application filters are configured.
To get the most accurate results, the filter should be mapped before all other filters.
<filter> <filter-name>ApplicationInsightsWebFilter</filter-name> <filter-class> com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter </filter-class> </filter> <filter-mapping> <filter-name>ApplicationInsightsWebFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5. Check firewall exceptions
You might need to set exceptions to send outgoing data.
6. Restart your web app
7. View your telemetry in Application Insights
Return to your Application Insights resource in Microsoft Azure portal.
Telemetry about HTTP requests appears on the overview blade. (If it isn't there, wait a few seconds and then click Refresh.)
Click through any chart to see more detailed metrics.
And when viewing the properties of a request, you can see the telemetry events associated with it such as requests and exceptions.