【Azure Application Insights】为Spring Boot应用集成Application Insight SDK

简介: 本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。

问题描述

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

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

 

问题解答

第一步:在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中

 

第二步:在 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
  }
}


效果图:

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

如果启动后,没有在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


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

 

 

附件:如果需要自定义日志或其他信息,可以调用 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

 




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

相关文章
|
7月前
|
前端开发
SpringBoot2.3.1集成Knife4j接口文档
SpringBoot2.3.1集成Knife4j接口文档
754 44
|
6月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
353 3
|
6月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
603 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
6月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
672 2
|
6月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
377 2
|
8月前
|
API 开发工具 网络架构
【Azure Service Bus】使用Python SDK创建Service Bus Namespace资源(中国区)
本文介绍了如何使用Python SDK创建Azure Service Bus Namespace资源。首先,通过Microsoft Entra ID注册应用获取Client ID、Client Secret和Tenant ID,完成中国区Azure认证。接着,初始化ServiceBusManagementClient对象,并调用`begin_create_or_update`方法创建资源。
192 29
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
129 0
|
6月前
|
存储 人工智能 Java
Springboot集成AI Springboot3 集成阿里云百炼大模型CosyVoice2 实现Ai克隆语音(未持久化存储)
本项目基于Spring Boot 3.5.3与Java 17,集成阿里云百炼大模型CosyVoice2实现音色克隆与语音合成。内容涵盖项目搭建、音色创建、音频合成、音色管理等功能,适用于希望快速掌握Spring Boot集成语音AI技术的开发者。需提前注册阿里云并获取API Key。
|
7月前
|
缓存 安全 Java
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
Shiro简介及SpringBoot集成Shiro(狂神说视频简易版)
622 7

热门文章

最新文章