【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

 




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

相关文章
|
2月前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
188 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
4月前
|
XML 人工智能 Java
Spring Boot集成Aviator实现参数校验
Aviator是一个高性能、轻量级的Java表达式求值引擎,适用于动态表达式计算。其特点包括支持多种运算符、函数调用、正则匹配、自动类型转换及嵌套变量访问,性能优异且依赖小。适用于规则引擎、公式计算和动态脚本控制等场景。本文介绍了如何结合Aviator与AOP实现参数校验,并附有代码示例和仓库链接。
298 0
|
4月前
|
安全 Java 数据库
第16课:Spring Boot中集成 Shiro
第16课:Spring Boot中集成 Shiro
737 0
|
4月前
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
465 0
|
2月前
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
249 0
|
4月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
270 3
|
4月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
400 3
|
4月前
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
515 0
第07课:Spring Boot集成Thymeleaf模板引擎
下一篇
oss云网关配置