DEV Community

Julien Dubois for Microsoft Azure

Posted on • Updated on

Using Azure Application Insights with Spring Boot (4/7)

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:

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 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 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 and which is fully documented here. 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, and which is fully documented here, 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.

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:

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:

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

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!

Top comments (5)

Collapse
 
kishore39600433 profile image
Biju

Hi Julien,

I am using Spring reactive app and have spring-boot-starter-webflux dependency . When I try to integrate with Application insights , I am getting below error .

Application insights doesn't support Spring 5 reactive framework?

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.microsoft.applicationinsights.autoconfigure.ApplicationInsightsWebMvcAutoConfiguration]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:599) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:302) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:589) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser.access$900(ConfigurationClassParser.java:108) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.lambda$processGroupImports$1(ConfigurationClassParser.java:808) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1540) ~[?:?]

Collapse
 
jdubois profile image
Julien Dubois • Edited

Hi Biju,
Unfortunately Spring Webflux is not supported at the date of this writing, see the documentation at docs.microsoft.com/bs-latn-ba/java... (last point of the prerequisites)

Collapse
 
kishore39600433 profile image
Biju

Okay. Thanks for the update.

Collapse
 
davidkarlsen profile image
davidkarlsen

I see azure-spring-boot-metrics-starter is deprecated now - will you still get the micrometer based metrics without it?

Collapse
 
sayedshakil01 profile image
sayedshakil01 • Edited

Hi Julien,
Is it possible to set package level or class level logging for Application Insights. Could you please share/show if yes.

Thanks,
Shakil Sayed