# Programmatic self-attach configuration

**URL:** <https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381>\
**Category:** APM\
**Created:** [March 21, 2019, 7:02pm UTC](https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381 "2019-03-21T19:02:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredgalvao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fredgalvao/32/42048_2.png) [@fredgalvao](https://discuss.elastic.co/u/fredgalvao)\
**Post date:** [March 21, 2019, 7:02pm UTC](https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381/1 "2019-03-21T19:02:07Z")

</div>

**Kibana version** :  
6.6.1  
**Elasticsearch version** :  
6.6.1  
**APM Server version** :  
6.6.1  
**APM Agent language and version** :  
java-agent-1.4.0

### Issue

I have a few grails@2.2.4 applications, a few grails@3.3.1 applications, and a couple of pure springBoot@1.5.6 applications. I'm trying to setup elastic as a platform for both APM and metric collection/monitoring for all of them.  
I initially tested Elastic APM with the javaagent loaded via VMOptions, and was successful in it. I then tested the programmatic API to self-attach the agent in runtime, and was also successful with it[1].

Issue [1]: For grails@2.2.4, there's no equivalent of `src/main/resources/elasticapm.properties` that I could find (I didn't debug the agent to see where it was looking for, maybe I'm wrong). Thus, I reverted back to using VMOptions for configuration while still loading the agent via the programmatic API in runtime. Even though configuration wasn't ideal, it worked fine.

Now, I went through the thin API of `ElasticApmAttacher` and there doesn't seem to be a way to do a `local attach` (not a remote one) and provide configuration during the call to `ElasticApmAttacher.attach()`. It'd be **incredibly easier and more powerfull** in terms of customization and proper environment/service/version detection (none of the other configuration methods come close) if we could, for example:

```auto
ElasticApmAttacher.attach([
	service_name: "bacon-service",
	service_version: ReadFromSomewhereElse.getVersion(),
	environment: GrailsSomething.getCurrentEnvironment(),
	transaction_sample_rate: Redis.getIt(),
	log_level: "WARN",
	server_urls: ReadFromConsul.forExample()
])

```

I suppose I could hack into the `ElasticApmAttacher#attach(java.lang.String, java.lang.String)` method and try to attach to the current VM as if it were a remote one (so that I could pass the `agentArgs` along), but that doesn't seem idiomatic or proper.

Is there already a way to achieve this? Am I missing something? Would this be interesting in general?  
Also, **one last question** , why exactly is the programmatic API to self-attach marked as experimental? Are there any limitations? It seems to work perfectly on java7 zulu for me.

---

<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:** [March 22, 2019, 11:47am UTC](https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381/2 "2019-03-22T11:47:13Z")

</div>

I like the idea of being able to provide a map for self attachment. Will implement that shortly.

I have documented some caveats of the runtime attachment here: [https://www.elastic.co/guide/en/apm/agent/java/master/setup-attach-cli.html#setup-attach-cli-caveats](https://www.elastic.co/guide/en/apm/agent/java/master/setup-attach-cli.html#setup-attach-cli-caveats)

---

<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:** [March 22, 2019, 1:49pm UTC](https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381/3 "2019-03-22T13:49:18Z")

</div>

You can subscribe to this PR: [https://github.com/elastic/apm-agent-java/pull/544](https://github.com/elastic/apm-agent-java/pull/544)

---

<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:** [April 12, 2019, 9:49am UTC](https://discuss.elastic.co/t/programmatic-self-attach-configuration/173381/4 "2019-04-12T09:49:18Z")

</div>

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