GuanceCloud / dd-trace-java

Datadog APM client for Java
https://docs.datadoghq.com/tracing/languages/java
Apache License 2.0
9 stars 3 forks source link

使用`java.ext.dirs` 方式运行应用,ddtrace出现断链现象 #61

Closed lrwh closed 9 months ago

lrwh commented 10 months ago

背景

Springboot 从 jar 包中分离出 lib 包,实现 lib 包与项目代码分离部署

运行指令

java -javaagent:/home/liurui/agent/dd-java-agent-1.12.1-guance.jar -Ddd.service.name=springboot-server -Ddd.env=1.0 -Ddd.agent.port=9529 -Djava.ext.dirs="./lib:$JAVA_HOME/jre/lib/ext" -jar springboot-server.jar

应用可以正常运行,产生的链路比集中式少了很多,且链路无法串联

demo 地址 https://github.com/lrwh/observable-demo/tree/main/springboot-server

需要调整pom

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- lib依赖包输出目录,打包的时候不打进jar包里 -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 压缩jar包,打出来的jar中没有了lib文件夹 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
lrwh commented 10 months ago

本地测试,使用链路运行正常且能正常串联。但也缺少了部分 span 信息。

java -javaagent:/home/liurui/agent/opentelemetry-javaagent-1.26.1-guance.jar -Dotel.traces.exporter=otlp -Dotel.exporter.otlp.endpoint=http://localhost:4317 -Dotel.resource.attributes=service.name=springboot-server -Djava.ext.dirs="./lib:$JAVA_HOME/jre/lib/ext" -jar springboot-server.jar
lrwh commented 10 months ago

使用 -Dloader.path 替换 -Djava.ext.dirs ddtrace 的链路能够串联,效果基本上和 OTEL 一样,但也缺少了部分 span 信息。

java -javaagent:/home/liurui/agent/dd-java-agent-1.12.1-guance.jar -Ddd.service.name=springboot-server -Ddd.env=1.0 -Ddd.agent.port=9529 -Dloader.path="lib/" -jar springboot-server.jar

推断

初步推断,是因为 agent 优先加载了,所以 agent 只对 jdk 原有的代码做了增强,而对于框架层,基本上看不见。

-Djava.ext.dirs 参数是 ExtClassLoader来加载的,而一般 java 程序是以 AppClassLoader 来加载代码。所以导致 agent 无法加载到 -Djava.ext.dirs 相关的依赖。

lrwh commented 9 months ago

按照以下方式进行处理,问题已解决

  1. 正常启动应用,不需要ddtrace

java -jar -Dloader.path="lib/" springboot-server.jar

-Dloader.path 替换 -Djava.ext.dirs

  1. 启动 ddtrace attach, 配置相关参数
java -Xbootclasspath/a:$JAVA_HOME/lib/tools.jar -jar agent-attach-java-jar-with-dependencies.jar  -options "dd.agent.port=9529,dd.trace.debug=true"  -displayName springboot-server.jar -agent-jar /usr/local/datakit/data/dd-java-agent.jar

agent-attach-java 仓库 : https://github.com/GuanceCloud/agent-attach-java

lrwh commented 9 months ago

部分客户仍然调整后无法导致应用无法启动

原始命令:

    nohup java -server ${SYS_ARGS} ${APP_ARGS} ${JAVA_AGENT_OPTS} $JAVA_MEM_OPTS $JAVA_JMX_OPTS $JAVA_RMI_SERVER_HOST  -Dconfig.entry=${Dconfig} -Xbootclasspath/a:$CONF_DIR -jar  -Djava.ext.dirs="${DEPLOY_DIR}/lib/:$JAVA_HOME/jre/lib/ext" ${DEPLOY_DIR}/lib/$JAR_NAME > $NOHUP_LOG_FILES 2>&1 &

调整后的命令

    nohup java -server ${SYS_ARGS} ${APP_ARGS} ${JAVA_AGENT_OPTS} $JAVA_MEM_OPTS $JAVA_JMX_OPTS $JAVA_RMI_SERVER_HOST  -Dconfig.entry=${Dconfig} -Xbootclasspath/a:$CONF_DIR -jar  -Dloader.path=“${DEPLOY_DIR}/lib/”  -Djava.ext.dirs="$JAVA_HOME/jre/lib/ext" ${DEPLOY_DIR}/lib/$JAR_NAME > $NOHUP_LOG_FILES 2>&1 &

image

lrwh commented 9 months ago

暂时无解,先close