# APM Java agent: capture startup

**URL:** <https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134>\
**Category:** APM\
**Created:** [December 17, 2018, 11:51am UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134 "2018-12-17T11:51:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)\
**Post date:** [December 17, 2018, 11:51am UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134/1 "2018-12-17T11:51:42Z")

</div>

Hello,

We have several web applications based on Spring 4.x(without spring boot) that have a large amount on startup code(init-methods on spring beans and ApplicationListener). Is there a way to have a transaction over the complete startup?

My first idea was to add the CaptureTransaction to the ApplicationListener and init methods but this would create multiple transactions. Does anyone have a better idea?

---

<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:** [December 17, 2018, 4:13pm UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134/2 "2018-12-17T16:13:54Z")

</div>

Hi and thanks for another great question 🙂

The Java agent is currently not capable of tracing the startup.  
What could work for Spring, at least, is to instrument `org.springframework.context.ConfigurableApplicationContext#refresh`.

Currently, this is only possible if you add a custom Byte Buddy advice to instrument that method. However, we have plans to make it configurable to instrument arbitrary methods: [https://github.com/elastic/apm-agent-java/issues/347](https://github.com/elastic/apm-agent-java/issues/347)

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)\
**Post date:** [December 18, 2018, 7:29am UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134/3 "2018-12-18T07:29:46Z")

</div>

Hi Felix,

That sounds awesome but I guess it may take a while until it is available, right?

For anyone tumbling over this thread having the same problem, my workaround is:  
I subclassed XmlWebApplicationContext with a custom class and overwrote the relevant refresh() method.  
This method only calls the base method but can be annotated with @CaptureTransaction.  
In web.xml I had to add the context-param contextClass pointing to my custom ApplicationContext.

Regards,  
Wolfram

---

<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:** [December 21, 2018, 5:20pm UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134/4 "2018-12-21T17:20:20Z")

</div>

Hi Wolfram,

depends on you definition of "a while". Here is a PR for just that: [https://github.com/elastic/apm-agent-java/pull/398](https://github.com/elastic/apm-agent-java/pull/398) 🙂

Cheers,  
Felix

---

<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:** [January 11, 2019, 1:31pm UTC](https://discuss.elastic.co/t/apm-java-agent-capture-startup/161134/5 "2019-01-11T13:31:32Z")

</div>

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