Current transaction carried over ExecutionService

I have a Spring Boot application using APM Agent 1.36.0.

A request submits a job to an executor and returns immediately. The relevant code looks like:

private ExecutorService executor = Executors.newSingleThreadExecutor();

public void scheduleJob(Job details) {
    executor.submit(() -> doProcessing(details));
}

@CaptureTransaction(type = "scheduled")
protected void doProcessing(Job details) {
    // stuff
}

However, APM collects the spans generated during doProcessing under the original request transaction.

Is the transaction state supposed to be maintained across threads like this, and is there a way to (possibly selectively) stop it?

Config:

use_path_as_transaction_name=true
disable_instrumentations=experimental,micrometer
enable_log_correlation=true
span_stack_trace_min_duration=-1ms
cloud_provider=none
capture_headers=true
enabled=true
transaction_ignore_user_agents=Elastic-Heartbeat/*,Zabbix/*,*OpenVAS-VT*,*Nessus*,*MSIE 8.0*
central_config=false
environment=staging
application_packages=com.example.myapp
use_elastic_traceparent_header=false

You are correct, by default we propagate the trace context across asynchronous invocations, like Executor invocations.

The @CaptureTransaction does not start a transaction when a transaction is currently active.
However, this restriction does not apply to ElasticAPM.startTransaction(). Could you try this method instead?

If you want to stop trace propagation for Executors completely, you can add executor to the disable_instrumentations config setting instead.

I see, thank you.

Perhaps there should be a docs update to link each disable_instrumentations option to the feature(s) it controls.

Supported technologies | APM Java Agent Reference [1.x] | Elastic doesn't include the option names.

Also an alternative, you could use @Traced(type = "scheduled") annotation to automatically create a transaction if none is already active and a child span if there is none (which allows nested invocations).

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