Quarkus日志跟踪,等效于Spring Cloud Sleuth

问题描述:

日志跟踪是非常重要的事情,因此我使用了这段代码,以使请求ID与响应ID匹配/连接.这适用于从ws.rs资源跟踪请求响应:

Log tracing is extremely important matter, so I played a little bit with this code, to match/connect request ids with the response ones. This works for tracing request responses from to/from my ws.rs resources:

  @Provider
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {

    private static final Logger LOG = Logger.getLogger(LoggingFilter.class);

    AtomicInteger _id = new AtomicInteger(0);
    AtomicInteger requestId = new AtomicInteger(0);

    @Context
    UriInfo info;

    @Context
    HttpServerRequest request;

    @Override
    public void filter(ContainerRequestContext context) {

        final String method = context.getMethod();
        final String path = info.getPath();
        final String address = request.remoteAddress().toString();
//        request.ge

        final long id = requestId.incrementAndGet();

        context.setProperty("prop", id);

        LOG.infof("Request %s %s from IP %s", method, path, address);
    }

    @Override
    public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {

        String LOGGING_ID_PROPERTY = "prop";
        String RESPONSE_PREFIX = "responsePrefix";

        final Object requestId = requestContext.getProperty(LOGGING_ID_PROPERTY);
        final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();

        final StringBuilder b = new StringBuilder();

        b.append("text");

        printResponseLine(b, "Client response received", id, responseContext.getStatus());
        printPrefixedHeaders(b, id, RESPONSE_PREFIX, responseContext.getHeaders());
    }

    private void printResponseLine(StringBuilder b, String string, long id, int status) {
        LOG.info(b.toString() + id);
    }

    private void printPrefixedHeaders(StringBuilder b, long id, String string, MultivaluedMap<String, Object> headerMap) {
        LOG.info(b.toString() + id);
    }
}

但是,如果我从应用程序中调用了一些其他休息点(使用其他客户端),则跟踪将会丢失.期望看到整个日志tr中的ID,从起点到下游,再到对呼叫客户端的响应.例如,通过 spring-cloud-sleuth 完成

But if I call some rest end-points (using rest clients) from my application, the trace is lost. The expectation is to see the IDs throughout the log trance from the origin to downstream to the response to the calling client. As it is done with spring-cloud-sleuth for example;

在Quarkus中已经可以做到吗?

Is there a way to do it in Quarkus already?

您可以使用ContainerRequestFilter,MDC和日志格式来实现它

You can implement this with ContainerRequestFilter,MDC and log format

@Provider
public class RestRequestFilter implements ContainerRequestFilter {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    Tracer tracer;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) {
        MDC.put("SPAN_ID", tracer.activeSpan().context().toString());
    }

}

application.properties

application.properties

quarkus.log.console.format=%d{yyyy-MM-dd HH:mm:ss.SSS} span_id:%X{SPAN_ID}  %-5p [%c{3.}] (%t) %s%e%n

执行此过滤器后,您的所有日志都将包含SPAN_ID

after this filter is executed all your logs will contain SPAN_ID