Hey there, I am using APM Java agent with .jar agent. I would like to know if is there a way to enable or disable agent without restart on my custom application?
Currently I have to restart my java app to do that.
Hi!
This is possible using what we call "runtime attach" as described here in our documentation: Automatic setup with apm-agent-attach-cli.jar | APM Java Agent Reference [1.x] | Elastic
It is however not possible to detach at runtime, but you should be able to disable the agent through configuration with the recording
configuration option, using either an external properties files (that is reloaded every 30s by default) or using central configuration.
A simple approach to implement this could be to:
- configure the agent with an external properties file with
config_file
- configure the agent with
recording=false
by default in the properties file - enable recording by modifying the properties file when needed.
Hi @Sylvain_Juge, thank you.
I have several doubts.
- I didn't find a
config_file
template. Is it anywhere? - I saw that every configuration key is available also in
Environment
format (i.e.ELASTIC_APM_RECORDING
). So, if my Java application has a mechanism to recheck/reload env_variables it should be enough? I mean, I could avoid to use theconfig_file
option and use directly theenviroment variables format
? And then, should be my Java Application able to monitor env_vars or directly the agent? - What is the polling variable (default 30s)?
The config file is just a regular java properties file, there is an example here: Property file reference | APM Java Agent Reference [1.x] | Elastic
You can't change environment variables of a started process. however the JVM system properties can be changed at runtime (which you can do by using System.setProperty
from within the application). However though, this is quite uncommon to change those at runtime as they are usually provided through JVM command line with -Dxxx
arguments which can't be changed after process is started, I'm pretty sure that the agent only reads them only once, but it could be worth to try changing something from the application by using System.setProperty
and experiment to see if that works for you.
Apart from that, the configuration polling is hard-coded at 30s and can't be configured. When using central configuration the polling frequency is controlled by the cache header sent from the server (but I don't know if there is a way to configure it though).
Ok. thank you again for these details.
If I try to manage everything using central configuration provided by APM Server, could I get my goal?
yes.