1
votes

i have a question related to logging on production for spring web-flux with project reactor. I have the following code on a staging env which log a lot of info related to signals which of course is very useful for debugging purposes.

Mono.zip(localChargeIdMono, userIdMono, merchantMono, totalAmountMono, requestIdMono)
                .map(this::toNewTransaction)
                        .log("TransactionRepository.save")
                        .then(chargeMono)
                        .map(chargeResult -> Tuples.of(transaction, chargeResult))
                        .map(this::toNewTransactionUpdatedWithChargeResult))
                .flatMap(tuple -> Mono.zip(
                        transactionRepository.save(tuple.getT1()),
                        paymentUpdateRepository.save(tuple.getT2())))
                .log("t.p.save")
                .map(Tuple2::getT1)
                .map(this::toChargeTransactionDto);

But i was wondering whether this is the correct way of logging on production or should i use doOn* methods to log only relevant data such as side effects function in/out params.

Mono.zip(localChargeIdMono, userIdMono, merchantMono, totalAmountMono, requestIdMono)
                    .map(this::toNewTransaction)
                            .donOnNext(e -> log.info(e))
                            .then(chargeMono)
                            .map(chargeResult -> Tuples.of(transaction, chargeResult))
                            .map(this::toNewTransactionUpdatedWithChargeResult))
                    .flatMap(tuple -> Mono.zip(
                            transactionRepository.save(tuple.getT1()),
                            paymentUpdateRepository.save(tuple.getT2())))
                     .donOnNext(e -> log.info(e))
                    .map(Tuple2::getT1)
                    .map(this::toChargeTransactionDto);

I'm looking for an answer more related to once experience woking with project reactor on prod as this is my first time.

Thanks

1

1 Answers

1
votes

But i was wondering whether this is the correct way of logging on production or should i use doOn* methods to log only relevant data such as side effects function in/out params.

You can use both - there's no issue with that at all. They'll obviously do different things (log() will give you other subscriber level information like the requested demand, cancellation etc. which you won't get from doOnXXX) but both of those are fine to use in production. Neither perform particularly costly operations like generating stack traces AFAIK.

To the best of my knowledge, there's only two real things you need to be aware of with logging in production:

  • Avoid Hooks.onOperatorDebug(). Very useful for debugging of course, but generates huge numbers of stack traces which is very costly.
  • You must use an async appender, not a synchronous / blocking logging appender. This catches more people out as it's usually buried in a logging config file rather than in code, but it's crucial - otherwise every one of your log calls is a blocking operation.