Setting up Application Insights and Log Analytics to monitor Spring Boot applications: Guide
To monitor a Spring Boot 3.x application using Azure Application Insights and Log Analytics, you need to set up Application Insights using the Java Agent (since the applicationinsights-spring-boot-starter
is not compatible with Spring Boot 3.x). Here’s an end-to-end guide for setting up Application Insights and Log Analytics for your Spring Boot 3.x application.
Prerequisites:
- Azure Subscription with access to Azure Application Insights and Azure Log Analytics.
- A Spring Boot 3.x application.
- Java 11 or higher.
- Maven or Gradle for dependency management.
Step 1: Create an Application Insights Resource in Azure
- Log in to the Azure Portal and search for Application Insights.
- Create a new Application Insights resource by clicking Create.
- Choose a resource group.
- Select the region closest to your application.
- Choose the Application Type (e.g., Java).
- After creation, note down the Instrumentation Key or Connection String for your Application Insights resource. You will use this to link your application to Application Insights.
Step 2: Download the Application Insights Java Agent
- Go to the Application Insights Java SDK GitHub repository.
- Download the Java agent JAR (e.g.,
applicationinsights-agent-3.x.x.jar
). - Place the agent JAR file in a known location within your project or on the server where the application will run.
Step 3: Configure the Java Agent in Spring Boot 3.x Application
Add the Java Agent to Your Spring Boot Application: You need to configure the Java agent when starting your Spring Boot application. You do this by passing the agent JAR as a JVM argument.
Modify your startup command: If you are using Maven to run your application, update the command as follows:
Replace
/path/to/applicationinsights-agent-3.x.x.jar
with the actual path to the agent JAR file.If running a packaged JAR, use this command:
Create an
applicationinsights.json
Configuration File: In thesrc/main/resources
directory of your Spring Boot application, create a file namedapplicationinsights.json
with the following content:Replace
your_instrumentation_key
with the actual Instrumentation Key or Connection String from your Application Insights resource.
Step 4: Integrate Log Analytics for Telemetry Analysis
Set up Log Analytics Workspace in Azure:
- In the Azure Portal, search for Log Analytics and create a Log Analytics Workspace.
- Once created, you’ll get the Workspace ID and Primary Key.
Configure Diagnostic Settings in Application Insights:
- In your Application Insights resource, go to the Diagnostic settings section.
- Add a new diagnostic setting:
- Select data: Choose to send logs and metrics (e.g., Performance, Requests, Exceptions).
- Destination details: Choose Log Analytics Workspace and select the Log Analytics Workspace you created earlier.
- This will send all the telemetry data from Application Insights to Log Analytics.
Step 5: Monitor Telemetry and Logs in Azure
Azure Monitor:
- Once your Spring Boot application is running and sending telemetry data, go to Azure Monitor.
- Under Application Insights, you’ll see the metrics for your application, such as request rates, failure rates, response times, etc.
Azure Log Analytics:
Go to Log Analytics in the Azure Portal.
Use Kusto Query Language (KQL) to query logs and metrics sent by Application Insights. For example:
This query retrieves the count of logs grouped by severity level in the last hour.
Step 6: Optional: Set Up Alerts
- Create Alerts:
- In Azure Monitor, you can create alerts based on telemetry data.
- For example, you can set an alert to trigger when the failure rate exceeds a threshold or when an exception occurs.
- Alerts can send notifications via email, SMS, or other channels.
Step 7: Verify the Setup
- Run Your Spring Boot Application and trigger some activity (e.g., make HTTP requests, generate errors).
- Go to Application Insights in the Azure Portal and verify that telemetry data (requests, exceptions, dependencies) is being collected.
- Check Log Analytics to see the logs and metrics from the Spring Boot application.
Example Maven Configuration for Application Insights Agent:
In your pom.xml, ensure you have the correct dependencies (for Spring Boot 3.x) and the agent setup is specified:
Summary:
- Application Insights tracks telemetry (requests, exceptions, dependencies) from your Spring Boot 3.x application using the Java Agent.
- Log Analytics aggregates and analyzes the telemetry data from Application Insights.
- Azure Monitor and Log Analytics allow you to visualize and query telemetry data, and set up alerts based on predefined thresholds.
This guide sets up end-to-end monitoring of your Spring Boot 3.x application with Azure Application Insights and Log Analytics.
Unlock Your Microservices Mastery for Only $9!
Get your copy now for just $9! and start building resilient and scalable microservices with the help of Microservices with Spring Boot 3 and Spring Cloud.