How to attach a java agent on to a running spring-boot application
Asked Answered
Y

1

10

I have a spring boot application packaged into a war running on a port, now i want to attach a java agent to this application, to monitor the micro services using Prometheus. But without using any of the spring plugins to scrape the metrics from.

For which i found a way to run the java agent with the application by starting it as (jus packaged my app into jar for trial it worked perfectly)

java -javaagent <Path-to javaagent jar>:PORT -jar app.jar

this works fine if my project is packaged into jar, It started java-agent on PORT and my application on a different port and im able to get the metrics from java-agent.

but the actual issue is my application is packaged into a war.

Can we run a java-agent with a war file?

also, another thing i want to try is, Attach this agent jar to the jvm after application is started with maven, like any other spring boot application with "mvn spring-boot:run"

How can this be achieved?

Yodel answered 22/4, 2019 at 10:55 Comment(6)
You need to start your app server with the java agent.Quintonquintuple
You need to activate actuator on spring boot cause it is supported with the appropriate end points ( I assume you are using Spring Boot 2.X)...Desilva
@Quintonquintuple Can you please elaborate on how to do that with the application being packaged into a war.Yodel
@Desilva Our need is to not use framework based plugins , as this should be working with any java application/frameworkYodel
@Quintonquintuple I have tried adding java agent to the catalina_opts in setenv.bat of my tomcat server.That didn't work. But how can i make this agent run on a port when it is set as a env variable??Yodel
I have added java agent to maven_opts to run on a port as, set MAVEN_OPTS =-javaagent <Path-to javaagent jar>=PORT=9300 But if someone has another/better approach. please reply me.Yodel
Y
8

As Suggested by @Strelok, I have added java agent to maven_opts to run on a port as,

set MAVEN_OPTS =-javaagent:Path-to javaagent jar=PORT=9300

Which worked in my local environment as i run my application with mvn spring-boot:run command.

But, If 2 different micro-services are to be running/monitored at the same time, Adding maven_opts as above might cause an issue for the 2nd micro-service , As the agent is already running on the same port. In such cases instead of adding maven_opts to env variables, We can add agent tag to pom.xml under spring-boot-maven-plugin to start the agent as:

<configuration>
    <agent>    
        PATH to JAR=port=XXXX
    </agent>
</configuration>

But our staging environments runs with Jenkins jobs which build and deploys the app war in to tomcat server and starts tomcat(by running ./startup.sh.) So i have added java agent's jar path to the catalina_opts in setenv.sh of my tomcat server.

and it worked, As expected.

Yodel answered 26/4, 2019 at 7:42 Comment(1)
It is <agents>, at least with spring-boot-starter-web 2.6.3,Edme

© 2022 - 2024 — McMap. All rights reserved.