Spring and Angular applications on Azure (7 Part Series)
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)
- Creating and configuring Azure Web App and MySQL to host a Spring Boot application (2/7)
- Using Azure Pipelines to build, test and deploy a Spring Boot and Angular application (3/7)
- Using Azure Application Insights with Spring Boot (4/7)
- Using Azure Application Insights with Angular (5/7)
- Configuring Azure CDN to boost Angular performance (6/7)
- Configuring Azure Redis Cache to boost Spring Boot performance (7/7)
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
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.
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.
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-prometheusdependency in the project's
pom.xml, as Prometheus is now replaced by Azure Application Insights.
- Add the
applicationinsights-logging-logbackdependency, so all production logs will be sent to Azure Application Insights. As we configured the
LOGGING_LEVELkey when configuring Azure Web App, we should have all debug informations from our business code.
- Reconfigure the
logback-spring.xmlfile in order to get the Azure Application Insights instrumentation key from Spring Boot, and set the
INFOlevel (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!
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!