# Detach java agent

**URL:** <https://discuss.elastic.co/t/detach-java-agent/186305>\
**Category:** APM\
**Tags:** java\
**Created:** [June 18, 2019, 3:46pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305 "2019-06-18T15:46:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandokandias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandokandias/32/48322_2.png) [@sandokandias](https://discuss.elastic.co/u/sandokandias)\
**Post date:** [June 18, 2019, 3:46pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305/1 "2019-06-18T15:46:20Z")

</div>

**Kibana version** : 7.1.1

**Elasticsearch version** : 7.1.1

**APM Server version** : 7.1.1

**APM Agent language and version** : Java - 1.7.0

**Browser version** : n/a

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:  
It`s a test environment on my laptop.  
APM-Server, EL and Kibana running via docker-compose  
Java-Agent installed by "apm-agent-attach-1.7.0.jar"

**Fresh install or upgraded from other version?** : Fresh

**Is there anything special in your setup?** For example, are you using the Logstash or Kafka outputs? Are you using a load balancer in front of the APM Servers? Have you changed index pattern, generated custom templates, changed agent configuration etc.  
No

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:  
The application is a spring-boot sample with one Controller and two post methods.  
I attach the agent using "apm-agent-attach-1.7.0.jar'" with the pid.  
Everything ok with the attach process.

My Question is: Is it possible to detach the agent from target application without restart it?

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [June 18, 2019, 8:38pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305/2 "2019-06-18T20:38:20Z")

</div>

Hi and welcome to the forum 👋

That is currently not possible but we have ideas how to add that in the future. However, you can dynamically set [`active`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-active) to `false` to disable the agent at runtime. Does that work for you?

---

<div class="post-metadata">

**Author:** ![sandokandias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandokandias/32/48322_2.png) [@sandokandias](https://discuss.elastic.co/u/sandokandias)\
**Post date:** [June 18, 2019, 9:07pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305/3 "2019-06-18T21:07:09Z")

</div>

Hi, thank you!

Yes works for my use case. How can I do that? - by another "attach" with the "active=false" arg?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [June 19, 2019, 11:18am UTC](https://discuss.elastic.co/t/detach-java-agent/186305/4 "2019-06-19T11:18:50Z")

</div>

Hi!

> [@sandokandias](#):
>
> How can I do that? - by another "attach" with the "active=false" arg?

No, that wouldn't be the way.  
Actually, I am not aware of a convenient way to do that...

The way to dynamically change configurations, like `active`, is through the [`elaticapm.properties` configuration file](https://www.elastic.co/guide/en/apm/agent/java/current/configuration.html#configuration), which should be located alongside the agent jar on the same directory. When using the remote attach option, like you do, the attach process puts a temporary agent jar file in a temp folder. If you can locate the temp folder it is placed at (eg `echo $TMPDIR` in macOS), you can put the configuration file there and do it through there, but of course this will be subject to the temp dir maintenance policy of the OS.

Another hackish option is to add endpoints to your application, like `/deactivate` and `/activate` in which you can set the related system property, for example:

```auto
System.setProperty("elastic.apm.active", Boolean.FALSE.toString());

```

Maybe @felixbarny has a "proper" way of doing that. Otherwise, we should certainly address that.

If you could just switch to using `-javaagent` instead of the remote attach, that would make things easier.

Eyal.

---

<div class="post-metadata">

**Author:** ![sandokandias](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandokandias/32/48322_2.png) [@sandokandias](https://discuss.elastic.co/u/sandokandias)\
**Post date:** [July 2, 2019, 12:29pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305/5 "2019-07-02T12:29:27Z")

</div>

Hi! Thank you for your response.

For the scenarios where we have full control of the application, we can use `-javaagent`.  
But we have some exceptions scenarios (_legacy apps of our customers_) that we can use only the remote attach option.

Your idea about add endpoints like `/deactivate` and `/activate` could be a viable solution, if we can customize the agent injecting a servlet to expose the resources.

What do you think?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 2, 2019, 1:46pm UTC](https://discuss.elastic.co/t/detach-java-agent/186305/6 "2019-07-02T13:46:19Z")

</div>

> [@sandokandias](#):
>
> if we can customize the agent injecting a servlet to expose the resources

No, we don't do that.

What about adding a config option that tells the agent where the properties file is located, then use a constant location for the file? Would that help? This means you still need to access the server and modify the file. How useful is that?  
And just out of curiosity- how high would you rank the importance of such switch-off option? Or, in other words- how problematic is restarting the application when you want to stop tracing?

BTW, we are working now on adding a mechanism that will allow configuring the agents through the APM UI. In the first version (of which release is not yet determined) this will only include the `transaction_sample_rate`, which not exactly what you want, but mostly. I definitely see the ability to turn `active` to false one that should follow.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 23, 2019, 9:46am UTC](https://discuss.elastic.co/t/detach-java-agent/186305/7 "2019-07-23T09:46:25Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
