RxJava and Log using MDC alternative / good practice

2,132 views
Skip to first unread message

david

unread,
Feb 17, 2016, 4:48:21 AM2/17/16
to RxJava
Hello, 

In a web application, we currently use SLF4J with some MDC key. For example, we store a correlation ID from each request.

That's allow us add this correlation ID to each log, via SLF4J, without having to pass this correlation ID to each methods and each log. (Which is super convenient for the developper)

BUT this MDC mechanism is based on a ThreadLocal. The correlation ID is store in the Thread Local, then, then something is logged, the logger get the value from the ThreadLocal.

It works, if you're still in the same thread. 

With RxJava, you can't use this ThreadLocal thing as you may not in the same thread when you store the correlation ID and when you log it.

My question is : how to you deal with this situation ? How do you logs data like correlation ID in your application, in all your logs ?

1) We create a custom logger that keep this correlationID. But We have to pass this logger along all our code base (t's like passing the correlation ID)
2) We try this : http://blog.mabn.pl/2014/11/rxjava-logback-and-mdc-threadlocal.html (on each thread switching, store the right MDC).But it not working when a Scheduler is backed by an ExecutorService (the onSchedule method is not called)

So, If you already encounter this situation, how do you deals with it ? 

Regards

Dávid Karnok

unread,
Feb 17, 2016, 4:54:00 AM2/17/16
to david, RxJava
You need custom schedulers for this as RxJava can't know what thread local values you want to move along data. I have an example scheduler in my blog: http://akarnokd.blogspot.hu/2015/05/schedulers-part-1.html 
--
Best regards,
David Karnok

david

unread,
Feb 18, 2016, 10:33:40 AM2/18/16
to RxJava, david.wu...@gmail.com

Thanks for your quick reply !

According to you blog and what I understood from it (“Advanced RxJava” : you mean it !), I don’t created my own scheduler, because I’m not confident in building my own scheduler. And it force developpers to always use the same scheduler too. I think I can avoid this by returning this custom scheduler in my RxJavaSchedulerHook. (but as I’m not confident in building my own scheduler, I’m not confident to do this neither)

So, now, I changed my mind, and I stop using a scheduler builded from an Executor, but, instead, I schedule my task using a standard/common RxJava scheduler (like Schedulers.computation(), Schedulers.io(), …). This is this executor, build from an Executor, that doesn’t call the onSchedule method from my RxJavaSchedulerHook.

So now, I copy my MDC key before scheduling my action. This action will restore MDC keys. (my code is very close to this code )

Do you think that it’s a good idea ? I’m a little bit worried about the copy of the keys, in term of performane. But I think that we won’t switch from schedulers to schedulers too offen.

Regards

Reply all
Reply to author
Forward
0 new messages