How to correlate log events in distributed Vertx system

后端 未结 4 1060
无人共我
无人共我 2021-01-04 20:40

while doing logs in the multiple module of vertx, it is a basic requirement that we should be able to correlate all the logs for a single request.

as vertx being asy

4条回答
  •  太阳男子
    2021-01-04 21:14

    There's a surprising lack of good answers published about this, which is odd, given how easy it is.

    Assuming you set the correlationId in your MDC context on receipt of a request or message, the simplest way I've found to propagate it is to use interceptors to pass the value between contexts:

    vertx.eventBus()
            .addInboundInterceptor(deliveryContext -> {
                MultiMap headers = deliveryContext.message().headers();
                if (headers.contains("correlationId")) {
                    MDC.put("correlationId", headers.get("correlationId"));
                    deliveryContext.next();
                }
            })
            .addOutboundInterceptor(deliveryContext -> {
                deliveryContext.message().headers().add("correlationId", MDC.get("correlationId"));
                deliveryContext.next();
            });
    

提交回复
热议问题