Using Azure Application Insights to Provide Business Indicators in Java Applications

Thiago Mendes
Analytics Vidhya
Published in
6 min readMay 3, 2020

Today I would like to share with you an interesting way to provide some business indicators in your Java application, in a simple way, without the need to expose data through an analytical environment or perform queries in your application’s database and take the risk to degrade the performance with poorly performed queries.

But what is Azure Application Insights?

In a nutshell, Application Insights is a managed service from Azure that allows you to monitor your applications in a simple way, helping you understand your customers’ behavior, evaluate performance and diagnose possible problems.

More information about Application Insights at: https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview

Before I start, I would like to propose a hypothetical scenario, with some requirements, so that we can subsequently meet them.

Imagine that we are implementing an application that aims to receive some donations over the internet.

In addition to the functional requirements, the business area aims to obtain some indicators such as:

- Number of logins in a given period;

- Number of donations in a given period;

- Filter donations worth more than 1000 dollars in a given period;

Based on these requirements, we will proceed with the implementation.

The first step is to create the application, for that, in order to save time, we will use the site Spring Initialzr to generate the skeleton of the project.

After creating the project skeleton, import it into your preferred IDE. In my case, I will use IntelliJ.

With the project imported into the IDE, open the POM.xml file and add the Application Insights dependency.

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

Now, it’s time to enter the Azure portal and create the Application Insights instance responsible for storing the data that will be generated by your application.

Access the portal with your account and search for the Application Insights service through the search field.

Create a new instance of Application Insights, using the + ADD button.

With the Application Insights instance created, access it and copy the Instrumentation Key, present in the upper right corner.

Now return to your IDE, open the application’s application.properties file (in src/main/resources) and create the following properties:

# Instrumentation Key #azure.application-insights.instrumentation-key = [Paste here the Instrumentation Key that you copied on the Azure portal]# Name of your application #spring.application.name = donations

Now we are going to create the controller class, with two endpoints, one responsible for login and one for donations.

You can also add exception handling and asynchronous processing strategies to these calls made to SDK methods, in order to avoid possible errors or slowdowns during the execution of the process, but as this is not the purpose of the post, I will not go into details about it.

package com.tmdev.donations;

import com.microsoft.applicationinsights.TelemetryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DonationsController {

@Autowired
private TelemetryClient telemetryClient;

@PostMapping("/login")
public void login() {

telemetryClient.trackEvent("DonationsLogin");

// Performs the sequence of business and security logics for login
}

@PostMapping("/donate")
public void donate(@RequestBody DonateRequest donateRequest) {

telemetryClient.trackMetric("Donation", donateRequest.getAmount());

// Performs the sequence of business logic for donation
}

}

We will also create the class that will be used as the request body in the donation endpoint.

package com.tmdev.donations;

public class DonateRequest {

private String donorName;

private double amount;

public String getDonorName() {
return donorName;
}

public void setDonorName(String donorName) {
this.donorName = donorName;
}

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}
}

Now it’s time to run our application, using maven’s spring-boot: run task.

With the application running, I will use POSTMAN to execute some requests for the login endpoint.

I will also execute some requests for the donation endpoint, between requests, I will change the value of donations, so that later we can perform the filter based on these values.

After executing the endpoints via POSTMAN, it is time to go back to the Azure portal, access the Application Insights instance and view the generated data.

Within the Application Insights instance, we have several ways to visualize this data, below I will list some of them…

Through the Events menu item on the left, it is possible to view the data generated through the trackEvent method, instrumented within the application, within the login endpoint.

Based on the data generated through the login endpoint, it is possible to perform some specific filters and use some types of charts.

Below I list the logins of the last hour, with intervals of 3 minutes and present it in a line chart.

To obtain the data generated through calls made to the donations endpoint, instrumented using the trackMetric method, we use the Metrics menu item on the left side.

Unlike the trackEvent method, with the trackMetric method, we can inform some variables that can be used to compose the search filters.

For more information on each type of method present in the SDK, access the documentation through the link https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics

It is also possible to perform searches on the generated data, through the menu item Logs, on the left side, however for that, you will need to write queries using Microsoft’s KUSTO language.

The KUSTO language is very intuitive and has great documentation. For more information, access the link https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/

Below I run a query to get the donations made in the last 24 hours.

customMetrics| where name == "Donation"

Now I run the same query above, but changing the presentation of the result to a bar chart.

Now in order to meet the requirement for queries with an amount greater than 1000 dollars, I add a WHERE condition to the filter.

customMetrics| where name == "Donation" and value > 1000

At any time you can use the Pin to dashboard function, to set these queries on a specific dashboard, which you can customize as you wish.

It is important to say that the fact that you have packaged the Application Insights SDK, also allows you to access the standard metrics of your application, which are collected automatically, without the need for instrumentation.

Conclusion

Using the Application Insights SDK in your applications, in addition to having access to the most diverse information related to APM (Application Performance Management), we can also use customized metrics to provide indicators, helping to understand your client’s behavior and direct some business decisions, based on data generated through specific instrumentation.

To get the code used in this post, access the link https://github.com/thiagomendes/donations

--

--