Spring Boot 3原生GraalVM应用集成New Relic方法咨询
Spring Boot 3 GraalVM原生镜像集成New Relic方案
你之前用--jvmargs添加New Relic代理的方式只适用于JVM运行模式,原生镜像编译后是独立二进制文件,不通过JVM启动,所以这种方式不会生效。下面是两种可行的集成方案:
方案一:使用New Relic官方原生镜像兼容代理
New Relic从7.x版本开始支持GraalVM原生镜像,需要在编译阶段集成代理并生成配置:
- 添加依赖
在Maven的pom.xml中加入New Relic代理依赖(版本选最新兼容版):
<dependency> <groupId>com.newrelic.agent.java</groupId> <artifactId>newrelic-agent</artifactId> <version>7.12.0</version> <scope>runtime</scope> </dependency>
- 编译时收集代理配置
通过GraalVM代理在编译阶段收集 instrumentation 配置,修改原生镜像插件配置:
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <buildArgs> <buildArg>-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image</buildArg> </buildArgs> </configuration> </plugin>
或者直接用命令行编译:
mvn native:compile -Dagentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image
- 运行原生镜像
编译完成后,运行二进制文件时指定New Relic配置文件路径:
./target/你的应用名 -Dnewrelic.config.file=/path/to/newrelic.yml
方案二:通过OpenTelemetry集成(推荐)
Spring Boot 3原生支持OpenTelemetry,而New Relic支持接收OTLP格式的追踪数据,这种方式适配性更好:
- 添加依赖
在pom.xml中加入OpenTelemetry starter和OTLP exporter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-otlp</artifactId> </dependency> <dependency> <groupId>io.opentelemetry.instrumentation</groupId> <artifactId>opentelemetry-spring-boot-starter</artifactId> <version>1.28.0</version> </dependency>
- 配置追踪参数
在application.properties中添加New Relic相关配置:
management.tracing.enabled=true management.tracing.sampling.probability=1.0 # New Relic OTLP endpoint otel.exporter.otlp.endpoint=https://otlp.nr-data.net:4317 # 替换为你的New Relic许可证密钥 otel.exporter.otlp.headers=api-key=你的NR_LICENSE_KEY # 设置你的应用名称 otel.resource.attributes=service.name=你的应用名称
- 编译并运行
直接编译原生镜像:
mvn native:compile
运行生成的二进制文件即可,追踪数据会自动发送到New Relic平台。
内容的提问来源于stack exchange,提问作者jayesh
相关产品推荐
相关产品推荐

