# Manually starting a transaction

**URL:** <https://discuss.elastic.co/t/manually-starting-a-transaction/339354>\
**Category:** APM\
**Tags:** java\
**Created:** [July 26, 2023, 9:38pm UTC](https://discuss.elastic.co/t/manually-starting-a-transaction/339354 "2023-07-26T21:38:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 26, 2023, 9:38pm UTC](https://discuss.elastic.co/t/manually-starting-a-transaction/339354/1 "2023-07-26T21:38:09Z")

</div>

I have some legacy servlet code. The APM agent creates transactions with names like MyClass#myMethod. I would like a more descriptive name. I would prefer not to change the servlet code, but I'm ok with adding a filter.

I've tried the [onIncomingRequest](https://www.elastic.co/guide/en/apm/guide/current/apm-distributed-tracing.html) technique in a servlet filter but I end up with two transactions: one that I create manually and one that's created automatically by the APM agent. The transactions don't seem to be linked in any way, even though one occurs in the context of the other.

This is my code:

```auto
        Transaction transaction = ElasticApm.startTransactionWithRemoteParent(request::getHeader, null);

        try (final Scope scope = transaction.activate()) {
            transaction.setName(request.getHeader("myheader"));
            transaction.setType(Transaction.TYPE_REQUEST);
            chain.doFilter(request, response);
        } catch (Exception e) {
            transaction.captureException(e);
            throw e;
        } finally {
            transaction.end();
        }

```

Instead, I would like to skip the txn created by the APM agent, or at least make it a span of the one I create. I've tried excluding the package the servlet is in but it doesn't seem to make a difference.

How can I handle a situation like this?

thanks

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 26, 2023, 10:16pm UTC](https://discuss.elastic.co/t/manually-starting-a-transaction/339354/2 "2023-07-26T22:16:22Z")

</div>

It appears the transaction is already started when my filter runs, even though the servlet has not been called yet. This code seems to do what I need:

```auto
        Transaction transaction = ElasticApm.currentTransaction();
        transaction.setName(request.getHeader("myheader"));
        chain.doFilter(request, response);

```

Basically I'm just renaming the txn.

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [July 27, 2023, 10:20am UTC](https://discuss.elastic.co/t/manually-starting-a-transaction/339354/3 "2023-07-27T10:20:54Z")

</div>

You're approach is exactly what I would have suggested. By renaming instead of replacing the transaction you also ensure that distributed tracing still works correctly.

---

<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:** [August 24, 2023, 10:21am UTC](https://discuss.elastic.co/t/manually-starting-a-transaction/339354/4 "2023-08-24T10:21:50Z")

</div>

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