NewRelic and SpringBoot with gradle
January 20, 2018
I thought of sharing on how we ended up having the NewRelic plugin for a SpringBoot applications. This was a dockerized SpringBoot-REST application. The newrelic agent is actually added as a javaagent plugin during the start process. I’ve seen some implementations where they had the newrelic-agent checked into the codebase or into a base docker image. Felt it was a code smell to take either way. So, this is what we did:
- Place the
newrelic.yml
in the root directory of the application. - Added the below configurations and dependencies in the build.gradle.
configurations {
//other configurations if any...
newRelic
}
dependencies {
newRelic group: 'com.newrelic.agent.java', name: 'newrelic-agent', version: '3.44.1'
}
task copyNewRelic(type: Copy) {
from configurations.newRelic
into "${project.buildDir}/libs"
rename { it.substring(0, it.indexOf("-")) + it.substring(it.lastIndexOf("."), it.size()) }
}
assemble.dependsOn copyNewRelic
- Edited the Dockerfile to include the newrelic-agent. Please note that we are using docker labels to minimize the end docker image.
WORKDIR /deploy
EXPOSE 8080
ENTRYPOINT java -javaagent:newrelic.jar -Dspring.profiles.active=${PROFILES} -jar application.jar
COPY --from=builder /source/build/libs/ ./
COPY --from=builder /source/newrelic.yml ./
This ended up working like a charm. Please share if there are better ways to achieve this.
Maintained by Sudhin Varghese who lives and works in Chennai with some great minds. You should follow me on Twitter