How to correlate log events in distributed Vertx system

后端 未结 4 1081
无人共我
无人共我 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:12

    In a thread based system, you current context is held by the current thread, thus MDC or any ThreadLocal would do.

    In an actor based system such as Vertx, your context is the message, thus you have to add a correlation ID to every message you send.

    For any handler/callback you have to pass it as method argument or reference a final method variable.

    For sending messages over the event bus, you could either wrap your payload in a JsonObject and add the correlation id to the wrapper object

    vertx.eventBus().send("someAddr", 
      new JsonObject().put("correlationId", "someId")
                      .put("payload", yourPayload));
    

    or you could add the correlation id as a header using the DeliveryOption

    //send
    vertx.eventBus().send("someAddr", "someMsg", 
                new DeliveryOptions().addHeader("correlationId", "someId"));
    
    //receive    
    vertx.eventBus().consumer("someAddr", msg -> {
            String correlationId = msg.headers().get("correlationId");
            ...
        });
    

    There are also more sophisticated options possible, such as using an Interceptor on the eventbus, which Emanuel Idi used to implement Zipkin support for Vert.x, https://github.com/emmanuelidi/vertx-zipkin, but I'm not sure about the current status of this integration.

提交回复
热议问题