Manually starting a transaction

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 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:

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

        try (final Scope scope = transaction.activate()) {
            chain.doFilter(request, response);
        } catch (Exception e) {
            throw e;
        } finally {

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?


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:

        Transaction transaction = ElasticApm.currentTransaction();
        chain.doFilter(request, response);

Basically I'm just renaming the txn.

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.

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