Using Azure Application Insights with Spring Boot (4/7)
Tags: spring, angular, azure, java
Published: 2019-05-28 09:26:30

---
title:  Using Azure Application Insights with Spring Boot (4/7)
published: true
description: In this series of blog posts, we are going to deploy a full-stack application to Azure, and learn how to set up many components efficiently, and in a cost-conscious way.
tags: spring, angular, azure, java
series: "Spring and Angular applications on Azure"
---
This blog post is part of a series on "deploying a Spring Boot and Angular application on Azure", here is the full list of posts:

- [Creating a Spring Boot and Angular application for Azure (1/7)](https://dev.to/jdubois/creating-a-spring-boot-and-angular-application-for-azure-1-7-2mb8)
- [Creating and configuring Azure Web App and MySQL to host a Spring Boot application (2/7)](https://dev.to/jdubois/creating-and-configuring-azure-web-app-and-mysql-to-host-a-spring-boot-application-2-7-1n13)
- [Using Azure Pipelines to build, test and deploy a Spring Boot and Angular application (3/7)](https://dev.to/azure/using-azure-pipelines-to-build-test-and-deploy-a-spring-boot-and-angular-application-3-7-593j)
- Using Azure Application Insights with Spring Boot (4/7)
- [Using Azure Application Insights with Angular (5/7)](https://dev.to/azure/using-azure-application-insights-with-angular-5-7-4kej)
- [Configuring Azure CDN to boost Angular performance (6/7)](https://dev.to/azure/configuring-azure-cdn-to-boost-angular-performance-6-7-5ebe)
- [Configuring Azure Redis Cache to boost Spring Boot performance (7/7)](https://dev.to/azure/configuring-azure-redis-cache-to-boost-spring-boot-performance-7-7-52dl)

## What is Azure Application Insights and why you should use it

In the previous parts of this series, we have developed a Spring Boot and Angular application, created an Azure infrastructure, and automated the deployment of our application to Azure. But what happens when something goes wrong? And we talked about costs and budgets, but how can we optimize them, if we don't know how our application behaves when users access it?

[Azure Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview/?WT.mc_id=devto-blog-judubois) is an application performance management (APM) service provided by Azure. This is the first thing you should set up with any application: without monitoring and insights, you are blind to any performance, infrastructure, or misconfiguration that may occur to your application.

Azure Application Insights is a compeling solution:

- It is very complete, as we will see through this series of blog posts that we will monitor both a server-side (Java) application and a front-and (Angular) application
- It provides advanced and highly customizable dashboards
- It has a free tier (good for simple applications), and is otherwise very inexpensive
- If you're an Azure user, obviously this will be your default choice as it's included

## Set up Azure Application Insights

When we created our Azure infrastructure in the part 2 of this series, we asked Azure Web App to automatically set up monitoring. If you didn't check that box, no worries! Just search for "Application Insights" in the [Azure portal](https://portal.azure.com/?WT.mc_id=devto-blog-judubois) and create a new instance. What is important is to have the "Instrumentation Key", which is available from the "Overview" page on your Azure Application Insights instance, and which will be used to send monitoring data from your running application.

## Use Azure Application Insights with Spring Boot

We are now going to configure Azure Application Insights in a Spring Boot application.

Currently, Microsoft provides a Spring Boot Starter for automatically configuring Azure Application Insights: `applicationinsights-spring-boot-starter`, which is [available here on Maven Central](https://search.maven.org/search?q=g:com.microsoft.azure%20AND%20a:applicationinsights-spring-boot-starter&core=gav) and which is [fully documented here](https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-java-applicationinsights?view=azure-java-stable&WT.mc_id=devto-blog-judubois). This is done by the Azure Application Insights team, and this is the starter we will we use here.

Please note that Microsoft also provide a `azure-spring-boot-metrics-starter`, which is [available here on Maven Central](https://search.maven.org/search?q=g:com.microsoft.azure%20AND%20a:azure-spring-boot-metrics-starter), and which is [fully documented here](https://docs.microsoft.com/en-us/azure/azure-monitor/app/micrometer-java/?WT.mc_id=devto-blog-judubois), and adds Micrometer monitoring support on top of the previous starter. Its current version, at the time of this writing, uses a different configuration key as `applicationinsights-spring-boot-starter`, so we think it might be better to wait until both starters use the same configuration.

As `applicationinsights-spring-boot-starter` is a Spring Boot starter, all there is to do is to add its library to the project's `pom.xml`, and configure its `azure.application.insights.instrumentation.key` Spring Boot property. There are several ways to do it, we'll go for the easiest and add it to our `application-prod.yml` configuration file, so we have monitoring set up in production. 

Then, there are few more configuration steps to follow:

- Remove the `micrometer-registry-prometheus` dependency in the project's `pom.xml`, as Prometheus is now replaced by Azure Application Insights.
- Add the `applicationinsights-logging-logback` dependency, so all production logs will be sent to Azure Application Insights. As we configured the `LOGGING_LEVEL` key when configuring Azure Web App, we should have all debug informations from our business code.
- Reconfigure the `logback-spring.xml` file in order to get the Azure Application Insights instrumentation key from Spring Boot, and set the `com.microsoft.applicationinsights` at the `INFO` level (otherwise it would flood us with information).

You can check the changes we just made to set up Azure Application Insights on [this commit](https://github.com/jdubois/spring-on-azure/commit/adf9029911d3e336218056eda5c14901649280e8).

As we have also configured Logback to send all logs to Azure Application Insights, they will now be aggregate there, and will be easily searchable:

![](https://thepracticaldev.s3.amazonaws.com/i/p7re0j5pg9qipw755onv.png)

Now that everything is set up, and as we already configured a full Continuous Integration/Continuous Deployment pipeline, all we need to do is push our code to GitHub, and watch our application being automatically updated.

Once your application is deployed, time to test how Application Insights work! Go to your Application Insights instance, and select "Live Metrics Stream". If your application is connected, then you're all set up!

## Testing Azure Application Insights

JHipster has generated Protractor tests for our application: open up the `protractor.conf.js` file and change the `baseUrl` property to point to your Azure Web App instance. You can then run Protractor tests by typing `npm run e2e`. This will launch a browser, and perform a series of scenarios in the application, including failing scenarios.

If everything is set up correctly, in the "Live Metrics Stream", you should see those scenarios running:

![](https://thepracticaldev.s3.amazonaws.com/i/8m8vmvuzjcgwtjgnwf9a.png)

Then, you can use the regular dashboards to check your Spring Application performance:

![](https://thepracticaldev.s3.amazonaws.com/i/qbiqvow2uw2e2cuj8i1y.png)

Those dashboards are very complete, and they will allow you to better understand the potential issues in your application. In the next post of this series, we will do the same configuration, but with the Angular front-end application: you will then be able to have an aggregate view of both user interaction on the front-end, and server-side performance metrics!