【Azure Application Insights】为Spring Boot应用集成Application Insight SDK - LuBu0505/My-Code GitHub Wiki

问题描述

以Java Spring Boot 项目为例,演示如何集成 Application Insights SDK,收集日志并通过Azure Application Insights服务进行展示。

本文参考的是官方网站的“将 Azure Monitor Application Insights 与 Spring Boot 配合使用” 文档,只是该文档中缺少一些操作图片指引,导致不易学习。 image.png

问题解答

第一步:在Spring Boot pom.xml 文件中添加依赖项:applicationinsights-runtime-attach 和 applicationinsights-core

  • applicationinsights-runtime-attach :为主要的依赖项,但是它的连接字符串必须配置在第三步的 applicationinsights.json文件中
  • applicationinsights-core:如果想把连接字符串配置到代码中,就需要添加它作为依赖

需要在 pom.xml 中添加的内容:

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-runtime-attach</artifactId>
            <version>3.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-core</artifactId>
            <version>3.7.1</version>
        </dependency>

如果此处添加后,当第二步为main函数中添加代码后,依旧无法识别到 ApplicationInsights 对象,则需要检查是否使用 build.gradle, 如有,则需要把以上依赖项添加到build.gradle中 image.png

第二步:在 main 函数中添加 ApplicationInsights.attach()

根据文档说明 ApplicationInsights.attach() 代码必须添加在main函数中的最开始代码中。 

//启动application insights功能
ApplicationInsights.attach();

//可选的配置 //修改参数
System.setProperty("applicationinsights.runtime-attach.configuration.classpath.file", "applicationinsights-dev.json"); 
//配置connection string 
ConnectionString.configure("<Your Connection String>");

注意:如果需要在代码中配置连接字符串,则必须在  applicationinsights.json 中添加 "connectionStringConfiguredAtRuntime": true 配置。

第三步:添加 applicationinsights.json 文件

文件的路径为: src\main\resources\applicationinsights.json , 在文件中加入如下内容:

{
  "connectionString": "your application insights connection string", 
  "connectionStringConfiguredAtRuntime": true, //可选
  "role": {
    "name": "my local java spring boot app"
  },
  "sampling": {
    "percentage": 33.333
  }
}

效果图: image.png

以上三步配置完成后,就可以启动应用。

如果启动后,没有在Azure Application Insights中查看到日志,需要检查本地项目中日志文件: applicationinsights.log,查看其中是否有如下错误信息:

2025-04-29 11:12:57.902+08:00 WARN  c.m.a.a.i.c.BytecodeUtilImpl - Using com.microsoft.applicationinsights.connectionstring.ConnectionString.configure() requires setting the json configuration property "connectionStringConfiguredAtRuntime" to true
2025-04-29 11:24:37.162+08:00 INFO  c.m.a.a.i.c.ConfigurationBuilder - Some telemetry may be sampled out because a default sampling configuration was added in version 3.4.0 to reduce the default billing cost. You can set the sampling configuration explicitly: https://learn.microsoft.com/azure/azure-monitor/app/java-standalone-config#sampling
2025-04-29 11:24:37.702+08:00 ERROR c.m.applicationinsights.agent - 
*************************
**Application Insights Java Agent 3.7.1 startup failed (PID 18992)**
*************************

Description:
No connection string provided

Action:
Please provide connection string: https://go.microsoft.com/fwlink/?linkid=2153358

如有,则是配置信息不全导致。当配置正确后,日志信息如下: image.png

附件:如果需要自定义日志或其他信息,可以调用 telemetryClient 对象中的 trackXXXXXXXX 方法

package com.example.springboot;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.microsoft.applicationinsights.TelemetryClient;

@RestController
public class HelloController {

    @GetMapping("/")
    public String index() {
        return "Greetings from Spring Boot! Enable the eureka service register....by me at 1:33";
    }

    @GetMapping("/eureka")
    public String indexforEureka() {
        return "............................................................................................................................";
    }

    private TelemetryClient telemetryClient;

    @GetMapping("/appinsights")
    public String sendmessagetoai() {

        telemetryClient = new TelemetryClient();
        telemetryClient.trackEvent("Hello Application Insgihts events...!");
        telemetryClient.trackTrace("this message from spring cloud application ... local test.");

        return "............................................................................................................................";
    }

}

参考资料

将 Azure Monitor Application Insights 与 Spring Boot 配合使用 : https://docs.azure.cn/zh-cn/azure-monitor/app/java-spring-boot#enabling-programmatically

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

⚠️ **GitHub.com Fallback** ⚠️