Java 无代码应用程序监视 Azure Monitor Application Insights - 公共预览版Java codeless application monitoring Azure Monitor Application Insights - public preview

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 autocollects 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 autocollected telemetry.

快速入门Quickstart

1.下载代理1. Download the agent

下载 applicationinsights-agent-3.0.0-PREVIEW.5.jarDownload applicationinsights-agent-3.0.0-PREVIEW.5.jar

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

-javaagent:path/to/applicationinsights-agent-3.0.0-PREVIEW.5.jar 添加到应用程序的 JVM 参数Add -javaagent:path/to/applicationinsights-agent-3.0.0-PREVIEW.5.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 参数的其他帮助,请参阅 3.0 预览版:更新 JVM 参数的技巧For additional help with configuring your application's JVM args, please see 3.0 Preview: 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=00000000-0000-0000-0000-000000000000

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

{
  "instrumentationSettings": {
    "connectionString": "InstrumentationKey=00000000-0000-0000-0000-000000000000"
  }
}

可以在 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
  • 应用程序日志捕获Application log capture
  • JMX 指标JMX metrics
  • MicrometerMicrometer
  • 检测信号Heartbeat
  • 采样Sampling
  • HTTP 代理HTTP Proxy
  • 自我诊断Self diagnostics

有关详细信息,请参阅 3.0 公共预览版:配置选项See details at 3.0 Public Preview: Configuration Options.

自动收集的请求、依赖项、日志和指标Autocollected 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
  • Log4jLog4j
  • SLF4J/LogbackSLF4J/Logback

指标Metrics

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

从应用程序发送自定义遥测Sending 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、OpenTelemetry API 和常用日志记录框架。We support Micrometer, OpenTelemetry API, and the popular logging frameworks. Application Insights Java 3.0 会自动捕获遥测,并将其与所有自动收集的遥测相关联。Application Insights Java 3.0 will automatically capture the telemetry, and correlate it along with all of the autocollected telemetry.

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

Application Insights Java 3.0 已在侦听发送到 Application Insights Java SDK 2.x 的遥测。Application Insights Java 3.0 is already listening for telemetry that is sent to the Application Insights Java SDK 2.x. 对于现有 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.

使用 Application Insights Java SDK 2.x 发送自定义遥测Sending custom telemetry using Application Insights Java SDK 2.x

applicationinsights-core-2.6.0.jar 添加到应用程序(Application Insights Java 3.0 支持所有 2.x 版本,但如果你可以选择,最好使用最新版本):Add applicationinsights-core-2.6.0.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.0</version>
  </dependency>

创建 TelemetryClient:Create a TelemetryClient:

private static final TelemetryClient telemetryClient = new TelemetryClient();

并使用它发送自定义遥测。and use that for sending custom telemetry.

事件Events

telemetryClient.trackEvent("WinGame");

指标Metrics

可以通过 Micrometer 发送指标遥测:You can send metric telemetry via Micrometer:

  Counter counter = Metrics.counter("test_counter");
  counter.increment();

也可以使用 Application Insights Java SDK 2.x:Or you can also use Application Insights Java SDK 2.x:

  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.setTimestamp(new Date(startTime));
      telemetry.setDuration(new Duration(endTime - startTime));
      telemetryClient.trackDependency(telemetry);
  }

日志Logs

可以通过常用的日志记录框架发送自定义日志遥测。You can send custom log telemetry via your favorite logging framework.

也可以使用 Application Insights Java SDK 2.x:Or you can also use Application Insights Java SDK 2.x:

  telemetryClient.trackTrace(message, SeverityLevel.Warning, properties);

异常Exceptions

可以通过常用的日志记录框架发送自定义异常遥测。You can send custom exception telemetry via your favorite logging framework.

也可以使用 Application Insights Java SDK 2.x:Or you can also use Application Insights Java SDK 2.x:

  try {
      ...
  } catch (Exception e) {
      telemetryClient.trackException(e);
  }

从 Application Insights Java SDK 2.x 升级Upgrading from Application Insights Java SDK 2.x

如果已在应用程序中使用 Application Insights Java SDK 2.x,则无需将其删除。If you're already using Application Insights Java SDK 2.x in your application, there is no need to remove it. Java 3.0 代理会检测到它,并捕获和关联你通过 Java SDK 2.x 发送的任何自定义遥测,同时禁止 Java SDK 2.x 执行的任何自动收集以防止重复捕获。The Java 3.0 agent will detect it, and capture and correlate any custom telemetry you're sending via the Java SDK 2.x, while suppressing any autocollection performed by the Java SDK 2.x to prevent duplicate capture.

备注

注意:使用 3.0 代理时,Java SDK 2.x TelemetryInitializer 和 TelemetryProcessor 不会运行。Note: Java SDK 2.x TelemetryInitializers and TelemetryProcessors will not be run when using the 3.0 agent.