# Instrument custom methood and create a transaction without code changes

**URL:** <https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163>\
**Category:** APM\
**Tags:** elastic-stack-monitoring, java\
**Created:** [November 14, 2023, 7:51pm UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163 "2023-11-14T19:51:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![senyam08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senyam08/32/106230_2.png) [@senyam08](https://discuss.elastic.co/u/senyam08)\
**Post date:** [November 14, 2023, 7:51pm UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163/1 "2023-11-14T19:51:36Z")

</div>

APM version 8.10.3

we are using [Vault](https://www.vaultproject.io/) secrets management solution for our java applications. We have issue with vault server overloaded and returning http error. Our java application usess Vault java driver which uses java.net.URLConnection to make call to vault server. Added apm java agent to our application and see only errors from vault. Not able to get any trace with vault method even though i have added custom trace for vault driver class and method. If vault http call is running in background thread, how do we get complete call stack with number of calls, dependency overview for vault and errors to get complete context. We are trying to get how many calls made from our application to vault using vault driver along with dependency stats for vault.  
Can we add a method via Trace\_methods and start transaction instead of span as its running in background thread and not being captured under any of the transaction. Thanks

---

<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:** [November 15, 2023, 8:22am UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163/2 "2023-11-15T08:22:19Z")

</div>

Hi,

With Elastic APM, the root of a distributed trace is a Transaction, which is usually the entry point of the application (incoming HTTP method call for an HTTP server for example).

In your case, the calls to Vault are likely executed outside or any transaction, for example when the application starts. In order to fix that you'll have to use `trace_methods` to instrument a method that wraps the HTTP calls to Vault.

For example, if your application has a single method that includes the whole application initialization where the calls to Vault are made, then it would be a good candidate for instrumentation with `trace_methods`.

`trace_methods` will create both transactions or spans depending on the execution context: if there is no active transaction a transaction is created, if there is one a span will be created.

As an alternative, with OpenTelemetry do not make any distinction between the spans and transactions, so in your case it would capture the outgoing spans without requiring to have a "parent span" that wraps the HTTP calls to Vault.  
However, this would require to use the OpenTelemetry java agent instead of the Elastic APM agent.

---

<div class="post-metadata">

**Author:** ![senyam08](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/senyam08/32/106230_2.png) [@senyam08](https://discuss.elastic.co/u/senyam08)\
**Post date:** [November 15, 2023, 8:33pm UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163/3 "2023-11-15T20:33:47Z")

</div>

Thanks for the detailed info. I did try adding method with trace\_methods config property. But i dont see new transaction created with custom trace method added for Vault

---

<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:** [November 16, 2023, 3:00pm UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163/4 "2023-11-16T15:00:04Z")

</div>

In order to know if the `trace_methods` is applied properly you should do the following:

- when the agent starts, the effective configuration is shown, you should see the value you have set for `trace_methods` configuration. If that's not the case, then it means it's not configured properly.
- set the agent with `log_level=debug`, with that the agent will log the instrumented methods if you grep for lines that include `Method match for instrumentation` you have all the instrumented methods. The method you have configured should be part of them, if that's not the case then it means the value that is configured does not match the application code.
- if the method is properly instrumented, then you should see `startTransation` and `endTransaction` in the log messages when the method stars and ends.

---

<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:** [December 14, 2023, 3:00pm UTC](https://discuss.elastic.co/t/instrument-custom-methood-and-create-a-transaction-without-code-changes/347163/5 "2023-12-14T15:00:32Z")

</div>

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