# APM enabled on-demand without restart

**URL:** <https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227>\
**Category:** APM\
**Tags:** java\
**Created:** [September 27, 2024, 10:03am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227 "2024-09-27T10:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [September 27, 2024, 10:03am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/1 "2024-09-27T10:03:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [September 27, 2024, 11:07am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/2 "2024-09-27T11:07:01Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/java/current/setup-attach-cli.html)

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](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-recording), 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`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-config-file)
- configure the agent with `recording=false` by default in the properties file
- enable recording by modifying the properties file when needed.

---

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [September 30, 2024, 7:55am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/3 "2024-09-30T07:55:44Z")

</div>

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 the `config_file` option and use directly the `enviroment 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)?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [September 30, 2024, 8:25am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/4 "2024-09-30T08:25:17Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/java/current/config-reference-properties-file.html)

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).

---

<div class="post-metadata">

**Author:** ![rschirin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rschirin/32/45283_2.png) [@rschirin](https://discuss.elastic.co/u/rschirin)\
**Post date:** [September 30, 2024, 10:17am UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/5 "2024-09-30T10:17:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [October 7, 2024, 1:29pm UTC](https://discuss.elastic.co/t/apm-enabled-on-demand-without-restart/367227/6 "2024-10-07T13:29:48Z")

</div>

yes.
