Java 无代码应用程序监视 Azure Monitor Application InsightsJava codeless application monitoring Azure Monitor Application Insights

Java 无代码应用程序监视只是为了简化操作 - 无需更改代码,只需更改几个配置即可启用 Java 代理。Java codeless application monitoring is all about simplicity - there are no code changes, the Java agent can be enabled through just a couple of configuration changes.

Java 代理可在任何环境中正常工作,并允许你监视所有 Java 应用程序。The Java agent works in any environment, and allows you to monitor all of your Java applications. 换句话说,无论你是在 VM 上、本地、AKS 中还是在 Windows、Linux 上运行 Java 应用,不管什么位置,Java 3.0 代理都可以监视你的应用。In other words, whether you are running your Java apps on VMs, on-premises, in AKS, on Windows, Linux - you name it, the Java 3.0 agent will monitor your app.

不再需要将 Application Insights Java SDK 添加到你的应用程序,因为 3.0 代理会自动收集请求、依赖项并自行记录所有内容。Adding the Application Insights Java SDK to your application is no longer required, as the 3.0 agent auto-collects requests, dependencies and logs all on its own.

你仍可以从应用程序发送自定义遥测。You can still send custom telemetry from your application. 3.0 代理会跟踪它并将它与所有自动收集的遥测数据相关联。The 3.0 agent will track and correlate it along with all of the auto-collected telemetry.

3.0 代理支持 Java 8 及更高版本。The 3.0 agent supports Java 8 and above.

快速入门Quickstart

1.下载代理1. Download the agent

警告

如果要从 3.0 预览版升级If you are upgrading from 3.0 Preview

请仔细检查所有的配置选项,因为除了文件名本身全部变为小写外,json 结构也已完全改变。Please review all the configuration options carefully, as the json structure has completely changed, in addition to the file name itself which went all lowercase.

下载 applicationinsights-agent-3.0.3.jarDownload applicationinsights-agent-3.0.3.jar

2.将 JVM 指向该代理2. Point the JVM to the agent

-javaagent:path/to/applicationinsights-agent-3.0.3.jar 添加到应用程序的 JVM 参数Add -javaagent:path/to/applicationinsights-agent-3.0.3.jar to your application's JVM args

典型的 JVM 参数包括 -Xmx512m-XX:+UseG1GCTypical JVM args include -Xmx512m and -XX:+UseG1GC. 如果你知道在何处添加这些参数,则你已知道要将此项添加到何处。So if you know where to add these, then you already know where to add this.

有关配置应用程序 JVM 参数的其他帮助,请参阅更新 JVM 参数的技巧For additional help with configuring your application's JVM args, please see Tips for updating your JVM args.

3.将代理指向 Application Insights 资源3. Point the agent to your Application Insights resource

如果还没有 Application Insights 资源,可以按照资源创建指南中的步骤创建一个新资源。If you do not already have an Application Insights resource, you can create a new one by following the steps in the resource creation guide.

通过设置环境变量,将代理指向 Application Insights 资源:Point the agent to your Application Insights resource, either by setting an environment variable:

APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=...

另一种方法是创建一个名为 applicationinsights.json 的配置文件,并将其置于 applicationinsights-agent-3.0.3.jar 所在的目录中,该文件包含以下内容:Or by creating a configuration file named applicationinsights.json, and placing it in the same directory as applicationinsights-agent-3.0.3.jar, with the following content:

{
  "connectionString": "InstrumentationKey=..."
}

可以在 Application Insights 资源中找到连接字符串:You can find your connection string in your Application Insights resource:

Application Insights 连接字符串

4.就这么简单!4. That's it!

现在启动应用程序,并访问 Azure 门户中的 Application Insights 资源以查看监视数据。Now start up your application and go to your Application Insights resource in the Azure portal to see your monitoring data.

备注

监视数据可能需要几分钟时间才能在门户中显示。It may take a couple of minutes for your monitoring data to show up in the portal.

配置选项Configuration options

applicationinsights.json 文件中,还可以配置以下内容:In the applicationinsights.json file, you can additionally configure:

  • 云角色名称Cloud role name
  • 云角色实例Cloud role instance
  • 采样Sampling
  • JMX 指标JMX metrics
  • 自定义维度Custom dimensions
  • 遥测处理器(预览版)Telemetry processors (preview)
  • 自动收集的日志记录Auto-collected logging
  • 自动收集的 Micrometer 指标(包括 Spring Boot Actuator 指标)Auto-collected Micrometer metrics (including Spring Boot Actuator metrics)
  • 检测信号Heartbeat
  • HTTP 代理HTTP Proxy
  • 自我诊断Self-diagnostics

如需完整的详细信息,请参阅配置选项See configuration options for full details.

自动收集的请求、依赖项、日志和指标Auto-collected requests, dependencies, logs, and metrics

请求Requests

  • JMS 使用者JMS Consumers
  • Kafka 使用者Kafka Consumers
  • Netty/WebFluxNetty/WebFlux
  • ServletServlets
  • Spring 计划Spring Scheduling

与分布式跟踪传播的依赖关系Dependencies with distributed trace propagation

  • Apache HttpClient 和 HttpAsyncClientApache HttpClient and HttpAsyncClient
  • gRPCgRPC
  • java.net.HttpURLConnectionjava.net.HttpURLConnection
  • JMSJMS
  • KafkaKafka
  • Netty 客户端Netty client
  • OkHttpOkHttp

其他依赖项Other dependencies

  • CassandraCassandra
  • JDBCJDBC
  • MongoDB(异步和同步)MongoDB (async and sync)
  • Redis(Lettuce 和 Jedis)Redis (Lettuce and Jedis)

日志Logs

  • java.util.loggingjava.util.logging
  • Log4j(包含 MDC 属性)Log4j (including MDC properties)
  • SLF4J/Logback(包含 MDC 属性)SLF4J/Logback (including MDC properties)

指标Metrics

  • Micrometer(包括 Spring Boot Actuator 指标)Micrometer (including Spring Boot Actuator metrics)
  • JMX 指标JMX Metrics

Azure SDKAzure SDKs

  • 此功能处于预览阶段,请参阅有关如何启用该功能的配置选项This feature is in preview, see the configuration options for how to enable it.

从应用程序发送自定义遥测Send custom telemetry from your application

我们在 3.0+ 版本中的目标是让你能够使用标准 API 发送自定义遥测。Our goal in 3.0+ is to allow you to send your custom telemetry using standard APIs.

到目前为止,我们支持 Micrometer、热门的日志记录框架和 Application Insights Java 2.x SDK。We support Micrometer, popular logging frameworks, and the Application Insights Java 2.x SDK so far. Application Insights Java 3.0 会自动捕获通过这些 API 发送的遥测,并将其与自动收集的遥测相关联。Application Insights Java 3.0 automatically captures the telemetry sent through these APIs, and correlates it with auto-collected telemetry.

支持的自定义遥测Supported custom telemetry

下表显示了当前支持的自定义遥测类型,你可以使用它们来对 Java 3.0 代理进行补充。The table below represents currently supported custom telemetry types that you can enable to supplement the Java 3.0 agent. 总而言之,通过 Micrometer 可以支持自定义指标,通过记录框架可以启用自定义异常和跟踪,通过 Application Insights Java 2.x SDK 可以支持任何类型的自定义遥测。To summarize, custom metrics are supported through micrometer, custom exceptions and traces can be enabled through logging frameworks, and any type of the custom telemetry is supported through the Application Insights Java 2.x SDK.

MicrometerMicrometer Log4j、logback、JULLog4j, logback, JUL 2.x SDK2.x SDK
自定义事件Custom Events Yes
自定义指标Custom Metrics Yes Yes
依赖项Dependencies Yes
异常Exceptions Yes Yes
页面视图Page Views Yes
请求Requests Yes
跟踪Traces Yes Yes

我们目前不打算发布带有 Application Insights 3.0 的 SDK。We're not planning to release an SDK with Application Insights 3.0 at this time.

Application Insights Java 3.0 已在侦听发送到 Application Insights Java 2.x SDK 的遥测。Application Insights Java 3.0 is already listening for telemetry that is sent to the Application Insights Java 2.x SDK. 对于现有 2.x 用户来说,此功能是升级过程中的一个重要部分。在 OpenTelemetry API 正式发布之前,它填补了我们在自定义遥测支持方面的一个重要空白。This functionality is an important part of the upgrade story for existing 2.x users, and it fills an important gap in our custom telemetry support until the OpenTelemetry API is GA.

使用 Micrometer 发送自定义指标Send custom metrics using Micrometer

将 Micrometer 添加到应用程序:Add Micrometer to your application:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-core</artifactId>
  <version>1.6.1</version>
</dependency>

使用 Micrometer 全局注册表来创建计量:Use the Micrometer global registry to create a meter:

static final Counter counter = Metrics.counter("test_counter");

并使用它来记录指标:and use that to record metrics:

counter.increment();

使用你喜爱的日志记录框架发送自定义跟踪和异常Send custom traces and exceptions using your favorite logging framework

系统自动检测 Log4j、Logback 和 java.util.logging,通过这些日志记录框架执行的日志记录会作为跟踪和异常遥测自动收集。Log4j, Logback, and java.util.logging are auto-instrumented, and logging performed via these logging frameworks is auto-collected as trace and exception telemetry.

默认情况下,仅当在 INFO 级别或更高级别执行日志记录时,才收集该日志记录。By default, logging is only collected when that logging is performed at the INFO level or above. 有关如何更改此级别的信息,请参阅配置选项See the configuration options for how to change this level.

如果要将自定义维度附加到日志,可以使用 Log4j 1.2 MDCLog4j 2 MDCLog4j 2 MDC,Application Insights Java 3.0 将自动捕获这些 MDC 属性作为跟踪和异常遥测的自定义维度。If you want to attach custom dimensions to your logs, you can use Log4j 1.2 MDC, Log4j 2 MDC, or Logback MDC, and Application Insights Java 3.0 will automatically capture those MDC properties as custom dimensions on your trace and exception telemetry.

使用 2.x SDK 发送自定义遥测Send custom telemetry using the 2.x SDK

applicationinsights-core-2.6.2.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-core-2.6.2.jar to your application (all 2.x versions are supported by Application Insights Java 3.0, but it's worth using the latest if you have a choice):

<dependency>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>applicationinsights-core</artifactId>
  <version>2.6.2</version>
</dependency>

创建 TelemetryClient:Create a TelemetryClient:

static final TelemetryClient telemetryClient = new TelemetryClient();

并使用它发送自定义遥测:and use that to send custom telemetry:

事件Events
telemetryClient.trackEvent("WinGame");
指标Metrics
telemetryClient.trackMetric("queueLength", 42.0);
依赖项Dependencies
boolean success = false;
long startTime = System.currentTimeMillis();
try {
    success = dependency.call();
} finally {
    long endTime = System.currentTimeMillis();
    RemoteDependencyTelemetry telemetry = new RemoteDependencyTelemetry();
    telemetry.setSuccess(success);
    telemetry.setTimestamp(new Date(startTime));
    telemetry.setDuration(new Duration(endTime - startTime));
    telemetryClient.trackDependency(telemetry);
}
日志Logs
telemetryClient.trackTrace(message, SeverityLevel.Warning, properties);
异常Exceptions
try {
    ...
} catch (Exception e) {
    telemetryClient.trackException(e);
}

使用 2.x SDK 添加请求自定义维度Add request custom dimensions using the 2.x SDK

备注

此功能仅在 3.0.2 及更高版本中提供This feature is only in 3.0.2 and later

applicationinsights-web-2.6.2.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-web-2.6.2.jar to your application (all 2.x versions are supported by Application Insights Java 3.0, but it's worth using the latest if you have a choice):

<dependency>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>applicationinsights-web</artifactId>
  <version>2.6.2</version>
</dependency>

并在代码中添加自定义维度:and add custom dimensions in your code:

import com.microsoft.applicationinsights.web.internal.ThreadContext;

RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
requestTelemetry.getProperties().put("mydimension", "myvalue");

使用 2.x SDK 设置请求遥测 user_IdSet the request telemetry user_Id using the 2.x SDK

备注

此功能仅在 3.0.2 及更高版本中提供This feature is only in 3.0.2 and later

applicationinsights-web-2.6.2.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-web-2.6.2.jar to your application (all 2.x versions are supported by Application Insights Java 3.0, but it's worth using the latest if you have a choice):

<dependency>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>applicationinsights-web</artifactId>
  <version>2.6.2</version>
</dependency>

并在代码中设置 user_Idand set the user_Id in your code:

import com.microsoft.applicationinsights.web.internal.ThreadContext;

RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
requestTelemetry.getContext().getUser().setId("myuser");

使用 2.x SDK 替代请求遥测名称Override the request telemetry name using the 2.x SDK

备注

此功能仅在 3.0.2 及更高版本中提供This feature is only in 3.0.2 and later

applicationinsights-web-2.6.2.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-web-2.6.2.jar to your application (all 2.x versions are supported by Application Insights Java 3.0, but it's worth using the latest if you have a choice):

<dependency>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>applicationinsights-web</artifactId>
  <version>2.6.2</version>
</dependency>

并在代码中设置名称:and set the name in your code:

import com.microsoft.applicationinsights.web.internal.ThreadContext;

RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
requestTelemetry.setName("myname");

使用 2.x SDK 获取请求遥测 ID 和操作 IDGet the request telemetry id and the operation id using the 2.x SDK

备注

此功能仅在 3.0.3 及更高版本中提供This feature is only in 3.0.3 and later

applicationinsights-web-2.6.2.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-web-2.6.2.jar to your application (all 2.x versions are supported by Application Insights Java 3.0, but it's worth using the latest if you have a choice):

<dependency>
  <groupId>com.microsoft.azure</groupId>
  <artifactId>applicationinsights-web</artifactId>
  <version>2.6.2</version>
</dependency>

同时在代码中获取请求遥测 ID 和操作 ID:and get the request telemetry id and the operation id in your code:

import com.microsoft.applicationinsights.web.internal.ThreadContext;

RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
String requestId = requestTelemetry.getId();
String operationId = requestTelemetry.getContext().getOperation().getId();