7

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
  • 131
  • 1
  • 1
  • 8
  • You need to start your app server with the java agent. – Strelok Apr 22 '19 at 14:22
  • 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)... – khmarbaise Apr 22 '19 at 17:32
  • @Strelok Can you please elaborate on how to do that with the application being packaged into a war. – mrtCherry Apr 23 '19 at 06:31
  • @khmarbaise Our need is to not use framework based plugins , as this should be working with any java application/framework – mrtCherry Apr 23 '19 at 06:34
  • @Strelok 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?? – mrtCherry Apr 23 '19 at 09:25
  • I have added java agent to maven_opts to run on a port as, set MAVEN_OPTS =-javaagent =PORT=9300 But if someone has another/better approach. please reply me. – mrtCherry Apr 24 '19 at 09:27

1 Answers1

6

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.

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
mrtCherry
  • 131
  • 1
  • 1
  • 8