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

mrtCherry :

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?

mrtCherry :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=69369&siteId=1