加载中

使用 -javaagent 参数手动设置

使用 -javaagent 选项是在 JVM 上设置 Java 代理最常见的方法,它具有以下特性:

  • 无需修改应用程序代码。
  • 需要更改 JVM 参数,这意味着需要重新启动整个 JVM。
  • 对于应用服务器,修改 JVM 参数需要更改应用服务器配置。
  • 代理构件是一个需要与 JVM 或应用服务器一起管理的额外二进制文件。
  • 确保应用程序在启动之前已被完全插桩。

开始使用 Elastic APM Java 代理的第一步是获取代理 jar 的副本。

Java 代理版本发布在 Maven 中央仓库中,要获取副本,你可以:

  • 手动下载 最新代理,或者从 Maven 中央仓库下载 以前的版本

  • 使用 curl 下载

    curl -o 'elastic-apm-agent.jar' -L 'https://repo1.maven.org/maven2/co/elastic/apm/elastic-apm-agent/1.56.0/elastic-apm-agent-1.56.0.jar'
    		

此外,我们还发布了 Java 代理的专用构建版本,其中包含最新的 log4j2 版本,因此至少需要 Java 8。该构建版本也可以在 Maven 中央仓库中找到。

Java 代理版本作为 Docker 镜像通过 docker.elastic.co 仓库发布。

latest 标签允许在构建镜像时使用最新的版本。

Dockerfile 中添加以下语句会将代理 jar 复制到 /elastic-apm-agent.jar

COPY --from=docker.elastic.co/observability/apm-agent-java:latest /usr/agent/elastic-apm-agent.jar /elastic-apm-agent.jar
		
注意

代理不像常规的应用程序依赖项。请勿在应用程序中声明对代理的依赖。

启动应用程序时,添加 JVM 标志 -javaagent:/path/to/elastic-apm-agent-<version>.jar

不同的应用服务器设置 -javaagent 标志和系统属性的方式有所不同。

请注意,系统属性只是配置代理的一种方法,但在每种情况下都必须设置 -javaagent 标志。请参阅 配置 了解如何使用配置文件或环境变量配置代理。

启动您的应用程序(例如 Spring Boot 应用程序或其他嵌入式服务器)并添加 -javaagent JVM 标志。使用 -D 前缀通过系统属性配置代理。

java -javaagent:/path/to/elastic-apm-agent-<version>.jar -Delastic.apm.service_name=my-cool-service -Delastic.apm.application_packages=org.example,org.another.example -Delastic.apm.server_url=http://127.0.0.1:8200 -jar my-application.jar
		

创建 bin/setenv.sh(如果文件已存在则修改)。确保使该文件具有可执行权限,例如 chmod +x bin/setenv.sh

添加以下行

export CATALINA_OPTS="$CATALINA_OPTS -javaagent:/path/to/elastic-apm-agent-<version>.jar"
export CATALINA_OPTS="$CATALINA_OPTS -Delastic.apm.service_name=my-cool-service"
export CATALINA_OPTS="$CATALINA_OPTS -Delastic.apm.application_packages=org.example,org.another.example"
export CATALINA_OPTS="$CATALINA_OPTS -Delastic.apm.server_url=http://127.0.0.1:8200"
		

创建 bin\setenv.bat(如果文件已存在则修改)。

set CATALINA_OPTS=%CATALINA_OPTS% -javaagent:C:/path/to/elastic-apm-agent-<version>.jar
set CATALINA_OPTS=%CATALINA_OPTS% -Delastic.apm.service_name=my-cool-service
set CATALINA_OPTS=%CATALINA_OPTS% -Delastic.apm.application_packages=org.example,org.another.example
set CATALINA_OPTS=%CATALINA_OPTS% -Delastic.apm.server_url=http://127.0.0.1:8200
		

选项 1:编辑 jetty.sh

export JAVA_OPTIONS="${JAVA_OPTIONS} -javaagent:/path/to/elastic-apm-agent-<version>.jar"
export JAVA_OPTIONS="${JAVA_OPTIONS} -Delastic.apm.service_name=my-cool-service"
export JAVA_OPTIONS="${JAVA_OPTIONS} -Delastic.apm.application_packages=org.example,org.another.example"
export JAVA_OPTIONS="${JAVA_OPTIONS} -Delastic.apm.server_url=http://127.0.0.1:8200"
		

选项 2:编辑 start.ini

--exec
-javaagent:/path/to/elastic-apm-agent-<version>.jar
-Delastic.apm.service_name=my-cool-service
-Delastic.apm.application_packages=org.example,org.another.example
-Delastic.apm.server_url=http://127.0.0.1:8200
		

选项 3:如果您使用的是嵌入式 Jetty,请参阅 通用设置

standalone.conf 文件的底部添加代理配置

Unix

export JAVA_OPTS="$JAVA_OPTS -javaagent:/path/to/elastic-apm-agent-<version>.jar"
export JAVA_OPTS="$JAVA_OPTS -Delastic.apm.service_name=my-cool-service"
export JAVA_OPTS="$JAVA_OPTS -Delastic.apm.application_packages=org.example,org.another.example"
export JAVA_OPTS="$JAVA_OPTS -Delastic.apm.server_url=http://127.0.0.1:8200"
		

Windows

set JAVA_OPTS=%JAVA_OPTS% -javaagent:C:/path/to/elastic-apm-agent-<version>.jar
set JAVA_OPTS=%JAVA_OPTS% -Delastic.apm.service_name=my-cool-service
set JAVA_OPTS=%JAVA_OPTS% -Delastic.apm.application_packages=org.example,org.another.example
set JAVA_OPTS=%JAVA_OPTS% -Delastic.apm.server_url=http://127.0.0.1:8200
		

编辑通常位于 domain/configuration 下的 domain.xml 文件,并为 -javaagent 标志添加 JVM 选项,以及用于配置的一些系统属性。

...
<server-group>
  <jvm>
     <jvm-options>
      ...
      <option value="-javaagent:/path/to/elastic-apm-agent-<version>.jar"/>
      ...
     </jvm-options>
  </jvm>
</server-group>
...
<system-properties>
  <property name="elastic.apm.service_name"         value="my-cool-service"/>
  <property name="elastic.apm.application_packages" value="org.example,org.another.example"/>
  <property name="elastic.apm.server_url"          value="http://127.0.0.1:8200"/>
</system-properties>
...
		

将以下行添加到 jvm.options 文件中。

-javaagent:/path/to/elastic-apm-agent-<version>.jar
-Delastic.apm.service_name=my-cool-service
-Delastic.apm.application_packages=org.example,org.another.example
-Delastic.apm.server_url=http://127.0.0.1:8200
		

更新 domain.xml 文件以添加 -javaagent 标志和系统属性。

<java-config>
  ...
  <jvm-options>-javaagent:/path/to/elastic-apm-agent-<version>.jar</jvm-options>
  <jvm-options>-Delastic.apm.service_name=my-cool-service</jvm-options>
  <jvm-options>-Delastic.apm.application_packages=org.example,org.another.example</jvm-options>
  <jvm-options>-Delastic.apm.server_url=http://127.0.0.1:8200</jvm-options>
</java-config>
		

编辑 startWebLogic.sh 文件,并在 setDomainEnv.sh 调用后添加以下行

export JAVA_OPTIONS="$JAVA_OPTIONS -javaagent:/path/to/elastic-apm-agent-<version>.jar"
export JAVA_OPTIONS="$JAVA_OPTIONS -Delastic.apm.service_name=my-cool-service"
export JAVA_OPTIONS="$JAVA_OPTIONS -Delastic.apm.application_packages=org.example,org.another.example"
export JAVA_OPTIONS="$JAVA_OPTIONS -Delastic.apm.server_url=http://127.0.0.1:8200"
		

编辑 startWebLogic.cmd 文件,并在 setDomainEnv.cmd 调用后添加以下行

set JAVA_OPTIONS=%JAVA_OPTIONS% -javaagent:C:/path/to/elastic-apm-agent-<version>.jar
set JAVA_OPTIONS=%JAVA_OPTIONS% -Delastic.apm.service_name=my-cool-service
set JAVA_OPTIONS=%JAVA_OPTIONS% -Delastic.apm.application_packages=org.example,org.another.example
set JAVA_OPTIONS=%JAVA_OPTIONS% -Delastic.apm.server_url=http://127.0.0.1:8200
		

Release v4.19 起,Elastic Java APM 代理框架已成为 Cloud Foundry Java Buildpack 的一部分。

用户提供的 Elastic APM 服务必须包含带有 elastic-apm 的名称或标签,以便 Elastic APM 代理框架自动将应用程序配置为与该服务协同工作。

创建一个用户提供的服务

cf cups my-elastic-apm-service -p '{"server_url":"my-apm-server-url","secret_token":"my-apm-server-secret-token"}'

my-apm-server-urlmy-apm-server-secret-token 分别是您的 Elasticsearch 服务器的服务密钥中的 server_urlsecret_token

将应用程序绑定到该服务

cf bind-service my-application my-elastic-apm-service

然后重新部署应用程序,或者使用应用程序清单文件中的 services 块。

有关 Cloud Foundry 的 Elastic Java APM 代理框架的更多详细信息,请参阅此处

applications:
- name: my-application
  memory: 1G
  path: ./target/my-application.jar
  services:
    - my-elastic-apm-service
		
© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.