# Is ForkJoinPool tracable?

**URL:** <https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599>\
**Category:** APM\
**Tags:** java\
**Created:** [March 8, 2020, 12:47pm UTC](https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599 "2020-03-08T12:47:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![no\_jihun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/no_jihun/32/64028_2.png) [@no\_jihun](https://discuss.elastic.co/u/no_jihun)\
**Post date:** [March 8, 2020, 12:47pm UTC](https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599/1 "2020-03-08T12:47:37Z")

</div>

With this like code, I couldn't see details inside callable.

### code

```auto
public class ApmTest {

    static ForkJoinPool fjp = new ForkJoinPool();
    static ExecutorService pool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String result1 = fjp.submit(() -> runInForkJoinPool()).get();
        String result2 = pool.submit(() -> runInExecutorService()).get();
        System.out.println("result is " + result1);
        System.out.println("result is " + result2);
        fjp.shutdown();
        pool.shutdown();

    }

    public static String runInExecutorService() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "runInExecutorService";
    }

    public static String runInForkJoinPool() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "runInForkJoinPool";
    }
}

```

### result

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/a/4a8436daf82278cb00ff0107f820f0546ab437d0.png)

### configuration

```auto
    -javaagent:path of elastic-apm-agent-1.13.0.jar \
    -Delastic.apm.service_name=myservicename \
    -Delastic.apm.server_urls=myserverurl \
    -Delastic.apm.trace_methods=mypackage.ApmTest#* \
    -Delastic.apm.transaction_max_spans=50000 \
    -Delastic.apm.trace_methods_duration_threshold=50ms \
    -Delastic.apm.profiling_spans_enabled=true \
    -Delastic.apm.profiling_included_classes=mypackage.* \
    -Delastic.apm.application_packages=mypackage \

```

### Backgrounds

Because there are some tasks that are need to be executed in `FrokJoinPool` concepts; like `parelllStream().map(...)` at deep level operation, So I run whole task in `ForkJoinPool`.

### Question

I hope transaction is started from `main` and the followed method calls those executed inside ForkJoinPool are profiled as in same transaction.

ForkJoinPool is not possible to trace?

Or, Any workaround to manually propagate context to each parallel executions?

### Update

When I execute methods in `stream()` it profiled, but methods executed in `parallelStream()` are not profiled.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [March 16, 2020, 12:44pm UTC](https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599/2 "2020-03-16T12:44:43Z")

</div>

Hi and thanks for the question.

Inter-thread context propagation is not supported for `ForkJoinPool`.  
There are two ways you can make that happen:

1. wrap `Callable`/`Runnable`/`Task` submitted to the pool with your own type that also contains the `Span`, which you then `activate` on the worker thread through our [public API](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#public-api), similar to the example demonstrating context propagation through [blocking queues](https://github.com/elastic/apm-agent-java/blob/842185664456651f1d6a99602d21a7c052ddf111/apm-agent-plugins/apm-api-plugin/src/test/java/co/elastic/apm/api/BlockingQueueContextPropagationTest.java). If you do that with your example, feel free to make a pull request to add this so it can be a reference example to others as well 🙂
2. add a dedicated plugin, that will work similarly to our [Executors plugin](https://github.com/elastic/apm-agent-java/blob/842185664456651f1d6a99602d21a7c052ddf111/apm-agent-plugins/apm-java-concurrent-plugin/src/main/java/co/elastic/apm/agent/concurrent/ExecutorInstrumentation.java) (likely a lot more complicated).

`parallelStream` is not supported as well. You may be able to support that by wrapping your collection elements as well alongside a `Span`, and activate/deactivate/end this `Span` when processing the collection element on the worker thread.

I hope this helps.

---

<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:** [April 6, 2020, 8:44am UTC](https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599/3 "2020-04-06T08:44:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [June 3, 2020, 10:34am UTC](https://discuss.elastic.co/t/is-forkjoinpool-tracable/222599/4 "2020-06-03T10:34:00Z")

</div>

@no_jihun Would you like to test our new `ForkJoinPool` support? If so, please try [this](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-java%2Fapm-agent-java-mbp/detail/PR-1206/6/artifacts) snapshot build and let us know.
