【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

 




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

目录
打赏
0
8
8
0
203
分享
相关文章
|
2月前
|
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 身份和权限认证
本文介绍了 Apache Shiro 的身份认证与权限认证机制。在身份认证部分,分析了 Shiro 的认证流程,包括应用程序调用 `Subject.login(token)` 方法、SecurityManager 接管认证以及通过 Realm 进行具体的安全验证。权限认证部分阐述了权限(permission)、角色(role)和用户(user)三者的关系,其中用户可拥有多个角色,角色则对应不同的权限组合,例如普通用户仅能查看或添加信息,而管理员可执行所有操作。
91 0
微服务——SpringBoot使用归纳——Spring Boot中集成 Shiro——Shiro 三大核心组件
本课程介绍如何在Spring Boot中集成Shiro框架,主要讲解Shiro的认证与授权功能。Shiro是一个简单易用的Java安全框架,用于认证、授权、加密和会话管理等。其核心组件包括Subject(认证主体)、SecurityManager(安全管理员)和Realm(域)。Subject负责身份认证,包含Principals(身份)和Credentials(凭证);SecurityManager是架构核心,协调内部组件运作;Realm则是连接Shiro与应用数据的桥梁,用于访问用户账户及权限信息。通过学习,您将掌握Shiro的基本原理及其在项目中的应用。
91 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
92 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
49 0
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
217 0
|
10月前
|
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
386 6
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
608 4
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
611 1

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等