Application Monitoring for Azure App Service and Java
Monitoring of your Java web applications running on Azure App Services does not require any modifications to the code. This article will walk you through enabling Azure Monitor Application Insights monitoring as well as provide preliminary guidance for automating the process for large-scale deployments.
Enable Application Insights
The recommended way to enable application monitoring for Java applications running on Azure App Services is through Azure portal. Turning on application monitoring in Azure portal will automatically instrument your application with Application Insights, and doesn't require any code changes. You can apply additional configurations, and then based on your specific scenario you add your own custom telemetry if needed.
Auto-instrumentation through Azure portal
You can turn on monitoring for your Java apps running in Azure App Service just with one click, no code change required. The integration adds Application Insights Java 3.x and you will get the telemetry auto-collected.
Select Application Insights in the Azure control panel for your app service, then select Enable.
Choose to create a new resource, or select an existing Application Insights resource for this application.
Note
When you select OK to create the new resource you will be prompted to Apply monitoring settings. Selecting Continue will link your new Application Insights resource to your app service, doing so will also trigger a restart of your app service.
This last step is optional. After specifying which resource to use, you can configure the Java agent. If you do not configure the Java agent, default configurations will apply.
The full set of configurations is available, you just need to paste a valid json file. Exclude the connection string and any configurations that are in preview - you will be able to add the items that are currently in preview as they become generally available.
Once you modify the configurations through Azure portal, APPLICATIONINSIGHTS_CONFIGURATION_FILE environment variable will automatically be populated and will appear in App Service settings panel. This variable will contain the full json content that you have pasted in Azure portal configuration text box for your Java app.
Enable client-side monitoring
To enable client-side monitoring for your Java application, you need to manually add the client-side JavaScript SDK to your application.
Automate monitoring
In order to enable telemetry collection with Application Insights, only the following Application settings need to be set:
Application settings definitions
| App setting name | Definition | Value |
|---|---|---|
| ApplicationInsightsAgent_EXTENSION_VERSION | Main extension, which controls runtime monitoring. | ~2 in Windows or ~3 in Linux. |
| XDT_MicrosoftApplicationInsights_Java | Flag to control if Java agent is included. | 0 or 1 (only applicable in Windows). |
Note
Profiler and snapshot debugger are not available for Java applications
App Service Application settings with Azure Resource Manager
Application settings for App Services can be managed and configured with Azure Resource Manager templates. This method can be used when deploying new App Service resources with Azure Resource Manager automation, or for modifying the settings of existing resources.
The basic structure of the application settings JSON for an app service is below:
"resources": [
{
"name": "appsettings",
"type": "config",
"apiVersion": "2015-08-01",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
],
"tags": {
"displayName": "Application Insights Settings"
},
"properties": {
"key1": "value1",
"key2": "value2"
}
}
]
For an example of an Azure Resource Manager template with Application settings configured for Application Insights, this template can be helpful, specifically the section starting on line 238.
Automate the creation of an Application Insights resource and link to your newly created App Service.
To create an Azure Resource Manager template with all the default Application Insights settings configured, begin the process as if you were going to create a new Web App with Application Insights enabled.
Create a new App Service resource with your desired web app information. Enabled Application Insights on the Monitoring tab.
Select Review +create then Download a template for automation at the bottom.
This option generates the latest Azure Resource Manager template with all required settings configured.
Below is a sample, replace all instances of AppMonitoredSite with your site name:
{
"resources": [
{
"name": "[parameters('name')]",
"type": "Microsoft.Web/sites",
"properties": {
"siteConfig": {
"appSettings": [
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').InstrumentationKey]"
},
{
"name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
"value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').ConnectionString]"
},
{
"name": "ApplicationInsightsAgent_EXTENSION_VERSION",
"value": "~2"
}
]
},
"name": "[parameters('name')]",
"serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
"hostingEnvironment": "[parameters('hostingEnvironment')]"
},
"dependsOn": [
"[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
"microsoft.insights/components/AppMonitoredSite"
],
"apiVersion": "2016-03-01",
"location": "[parameters('location')]"
},
{
"apiVersion": "2016-09-01",
"name": "[parameters('hostingPlanName')]",
"type": "Microsoft.Web/serverfarms",
"location": "[parameters('location')]",
"properties": {
"name": "[parameters('hostingPlanName')]",
"workerSizeId": "[parameters('workerSize')]",
"numberOfWorkers": "1",
"hostingEnvironment": "[parameters('hostingEnvironment')]"
},
"sku": {
"Tier": "[parameters('sku')]",
"Name": "[parameters('skuCode')]"
}
},
{
"apiVersion": "2015-05-01",
"name": "AppMonitoredSite",
"type": "microsoft.insights/components",
"location": "West US 2",
"properties": {
"ApplicationId": "[parameters('name')]",
"Request_Source": "IbizaWebAppExtensionCreate"
}
}
],
"parameters": {
"name": {
"type": "string"
},
"hostingPlanName": {
"type": "string"
},
"hostingEnvironment": {
"type": "string"
},
"location": {
"type": "string"
},
"sku": {
"type": "string"
},
"skuCode": {
"type": "string"
},
"workerSize": {
"type": "string"
},
"serverFarmResourceGroup": {
"type": "string"
},
"subscriptionId": {
"type": "string"
}
},
"$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0"
}
Enable through PowerShell
In order to enable the application monitoring through PowerShell, only the underlying application settings need to be changed. Below is a sample, which enables application monitoring for a website called "AppMonitoredSite" in the resource group "AppMonitoredRG", and configures data to be sent to the "012345678-abcd-ef01-2345-6789abcd" instrumentation key.
Note
This article uses the Azure Az PowerShell module, which is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
$app = Get-AzWebApp -ResourceGroupName "AppMonitoredRG" -Name "AppMonitoredSite" -ErrorAction Stop
$newAppSettings = @{} # case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # preserve non Application Insights application settings.
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = "012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights instrumentation key
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop
Troubleshooting
Below is our step-by-step troubleshooting guide for Java-based applications running on Azure App Services.
Check that
ApplicationInsightsAgent_EXTENSION_VERSIONapp setting is set to a value of "~2" on Windows, "~3" on LinuxExamine the log file to see that the agent has started successfully: browse to `https://yoursitename.scm.azurewebsites.net/, under SSH change to the root directory, the log file is located under LogFiles/ApplicationInsights.
After enabling application monitoring for your Java app, you can validate that the agent is working by looking at the live metrics - even before you deploy and app to App Service you will see some requests from the environment. Remember that the full set of telemetry will only be available when you have your app deployed and running.
Set APPLICATIONINSIGHTS_SELF_DIAGNOSTICS_LEVEL environment variable to 'debug' if you do not see any errors and there is no telemetry
Sometimes the latest version of the Application Insights Java agent is not available in App Service - it takes a couple of months for the latest versions to roll out to all regions. In case you need the latest version of Java agent to monitor your app in App Service, you can upload the agent manually:
- Upload the Java agent jar file to App Service
- Get the latest version of Azure CLI
- Get the latest version of Application Insights Java agent
- Deploy Java agent to App Service - a sample command to deploy the Java agent jar:
az webapp deploy --src-path applicationinsights-agent-{VERSION_NUMBER}.jar --target-path java/applicationinsights-agent-{VERSION_NUMBER}.jar --type static --resource-group {YOUR_RESOURCE_GROUP} --name {YOUR_APP_SVC_NAME}or use this guide to deploy through Maven plugin
- Once the agent jar file is uploaded, go to App Service configurations and add a new environment variable, JAVA_OPTS, and set its value to
-javaagent:D:/home/{PATH_TO_THE_AGENT_JAR}/applicationinsights-agent-{VERSION_NUMBER}.jar - Disable Application Insights via Application Insights tab
- Restart the app
Note
If you set the JAVA_OPTS environment variable, you will have to disable Application Insights in the portal. Alternatively, if you prefer to enable Application Insights from the portal, make sure that you don't set the JAVA_OPTS variable in App Service configurations settings.
- Upload the Java agent jar file to App Service
Connection string and instrumentation key
When codeless monitoring is being used, only the connection string is required. However, we still recommend setting the instrumentation key to preserve backwards compatibility with older versions of the SDK when manual instrumentation is being performed.
Difference between Standard Metrics from Application Insights vs Azure App Service metrics?
Application Insights collects telemetry for those requests which made it to the application. If the failure occurred in WebApps/WebServer, and the request did not reach the user application, then Application Insights will not have any telemetry about it.
The duration for serverresponsetime calculated by Application Insights is not necessarily matching the server response time observed by Web Apps. This is because Application Insights only counts the duration when the request actual reaches user application. If the request is stuck/queued in WebServer, that waiting time will be included in the Web App metrics, but not in Application Insights metrics.
Release notes
For the latest updates and bug fixes, consult the release notes.
Next steps
- Monitor Azure Functions with Application Insights.
- Enable Azure diagnostics to be sent to Application Insights.
- Monitor service health metrics to make sure your service is available and responsive.
- Receive alert notifications whenever operational events happen or metrics cross a threshold.
- Use Application Insights for JavaScript apps and web pages to get client telemetry from the browsers that visit a web page.
- Set up Availability web tests to be alerted if your site is down.
Povratne informacije
Pošalјite i prikažite povratne informacije za