4

I am trying to apply Openzipkin Brave to an application to help log tracing using Logback. The application is a spring webmvc application and it uses ForkJoinPool.

Tracing in the main thread (spring webmvc part) is working, but I have difficulties on passing the trace context to the ForkJoinThread. In my understanding, trace context is stored in threadlocal, which obviously is not accessible by ForkJoinWorkerThread (or ForkJoinTask). I wonder how I can make it work.

Tom Chi
  • 51
  • 2

2 Answers2

0

Knowing how the ForkJoinPool designed/operates using ForkJoinWorkerThreadFactory & ForkJoinWorkerThread and being a native java feature where worker threads executes outside the Spring Context. The only way to add tracing to your ForkJoinPool execution is to customize the Task itself.

Here is the blog on adding tracing to your ForkJoinPool Tasks execution, in Spring Boot 3x using Micrometer.

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
-1

EDIT: We discovered during answering of this question that ForkJoinPool does not work currently with Brave. Due to API constraints on the class we were not able to find a solution yet.

The following assumes using Brave version 5 (it might work on 4 also):

The CurrentTraceContext API gives us a method: executorService that we can use to wrap an ExecutorService. So you would need to do something like the following:


    ForkJoinPool fjp = ...;
    CurrentTraceContext ctc = Tracing.currentTraceContext();
    ExecutorService wrappedExecutorService = ctc.executorService(fjp);

Then you use the wrappedExecutorService as you were using your ForkJoinPool

  • If I want to have a ForkJoinPool instance at the end instead of ExecutorService, how to achieve it? – Tom Chi Sep 25 '18 at 06:47
  • I presume because you need to use some of the methods that are included in only ForkJoinPool? Currently it's not supported in any way I can see, sorry. I'm exploring it this morning but since Brave is Java 6 compatible it may not be easy to enhance so that you can get access to those methods – Brian Devins-Suresh Sep 26 '18 at 10:48
  • I've created this as a proposal to the other maintainers: https://github.com/openzipkin/brave/pull/797 . Please take a look, you could copy this code into your project and try it yourself if you want – Brian Devins-Suresh Sep 26 '18 at 11:48
  • Thanks Brian! I actually created my own class extending CurrentTraceContext and added a method to decorates the ForkJoinPool like how it decorates ExecutorService. It works fine so far. Your solution looks much cleaner than mine. I will have a try. – Tom Chi Sep 26 '18 at 15:04
  • I have tired your way by extending the ForkJoinWorkerThreadFactory but it has some issues. Since the traceContext is set to the ForkJoinWorkerThread at creation, it does not renew when the thread is being reused. – Tom Chi Sep 26 '18 at 15:37
  • @TomChi can you jump into the pull request and comment there? I was afraid that the threads would get reused but hadn't written the tests yet. I'm definitely interested in finishing this up though – Brian Devins-Suresh Sep 28 '18 at 10:37
  • Thanks Brian. I still cannot create a all-round solution for this. Let's continue the discussion in the pull request. – Tom Chi Oct 02 '18 at 11:37