Do you want specifically to implement yourself the correlation part using a UniqueRequestId
?
If you only want the feature of being able to keep track of your processing on your services or across services then you should look at tracing
solutions which can be commonly found in application performance monitoring
libraries.
I think you could use spring-cloud-sleuth
with only log correlation (https://cloud.spring.io/spring-cloud-sleuth/reference/html/#only-sleuth-log-correlation)
Sleuth supports reactor (spring webflux), it'll automatically add some data to your logs
trace.id
a trace
represents an operation, or a data flow, that can happen on several services. To me this is what you are looking for, this trace is made to be sent to other services so that they reuse it in their logs. Allowing you to track a particular call over several services.
transaction.id
a transaction
represents an operation, or a data flow inside the same service.
For example (the code below does not work, its just to give you an idea):
- serviceA makes an HTTP call to serviceB, both running in different containers / JVM
public class serviceA() {
private final ServiceBClient serviceBClient;
?@Get
public Flux<Cat> getSomething() {
log.info('Calling serviceB');
return serviceBClient
.listAllCats()
.then(() -> { log.info('Called serviceB') });
}
}
public class serviceB() {
private final CatRepository catRepository;
?@Get
public Flux<Cat> listCats() {
log.info('Asking for cats');
return catRepository.listAll()
.doOnSuccess(() -> { log.info('Request success!') })
}
}
Below are example with custom log message (only care for the trace.id)
serviceA.log
[INFO] .... Calling serviceB, trace.id=1321313543121, transaction.id=123546
[INFO] .... Called serviceB, trace.id=1321313543121, transaction.id=123546
serviceB.log
[INFO] .... Asking for cats, trace.id=1321313543121, transaction.id=224465
[INFO] .... Called serviceB, trace.id=1321313543121, transaction.id=224465
transaction.id
, a transaction represent an operation on a specific service.
Sleuth should also the trace.id to error logs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…