Setting up Application Insights and Log Analytics to monitor Spring Boot applications: Guide

  • User interacts with the Spring Boot Application.
  • The Spring Boot Application sends telemetry data (such as requests and exceptions) to Azure Application Insights.
  • Azure Application Insights forwards telemetry data (logs, metrics) to Azure Log Analytics.
  • Azure Log Analytics stores and analyzes the telemetry data, enabling querying and visualization.
  • Azure Monitor provides additional insights, visualizations, and alerting for the metrics.
  • User can visualize metrics and logs via Azure Monitor or Log Analytics.

  • 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

    1. Log in to the Azure Portal and search for Application Insights.
    2. 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).
    3. 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

    1. Go to the Application Insights Java SDK GitHub repository.
    2. Download the Java agent JAR (e.g., applicationinsights-agent-3.x.x.jar).
    3. 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

    1. 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:

        mvn spring-boot:run -Dspring-boot.run.jvmArguments="-javaagent:/path/to/applicationinsights-agent-3.x.x.jar"
        

        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:

        java -javaagent:/path/to/applicationinsights-agent-3.x.x.jar -jar target/your-app.jar
        
    2. Create an applicationinsights.json Configuration File: In the src/main/resources directory of your Spring Boot application, create a file named applicationinsights.json with the following content:

      {
        "connectionString": "InstrumentationKey=your_instrumentation_key"
      }

      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

    1. 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.
    2. 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

    1. 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.
    2. 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:

        traces | where timestamp > ago(1h) | summarize count() by severityLevel

      This query retrieves the count of logs grouped by severity level in the last hour.


    Step 6: Optional: Set Up Alerts

    1. 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

    1. Run Your Spring Boot Application and trigger some activity (e.g., make HTTP requests, generate errors).
    2. Go to Application Insights in the Azure Portal and verify that telemetry data (requests, exceptions, dependencies) is being collected.
    3. 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:

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-agent</artifactId>
            <version>3.6.2</version> <!-- Use the latest version -->
        </dependency>
    </dependencies>

    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.

    Popular posts from this blog

    Learn Java 8 streams with an example - print odd/even numbers from Array and List

    Java Stream API - How to convert List of objects to another List of objects using Java streams?

    Registration and Login with Spring Boot + Spring Security + Thymeleaf

    Java, Spring Boot Mini Project - Library Management System - Download

    ReactJS, Spring Boot JWT Authentication Example

    Top 5 Java ORM tools - 2024

    Java - Blowfish Encryption and decryption Example

    Spring boot video streaming example-HTML5

    Google Cloud Storage + Spring Boot - File Upload, Download, and Delete