繁体   English   中英

如何在spring-webflux WebFilter中正确使用slf4j MDC

[英]How to correctly use slf4j MDC in spring-webflux WebFilter

我参考了博客文章Contextual Logging with Reactor Context and MDC但我不知道如何在 WebFilter 中访问反应器上下文。

@Component
public class RequestIdFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        List<String> myHeader =  exchange.getRequest().getHeaders().get("X-My-Header");

        if (myHeader != null && !myHeader.isEmpty()) {
            MDC.put("myHeader", myHeader.get(0));
        }

        return chain.filter(exchange);
    }
}

你可以做类似下面的事情,你可以用你喜欢的任何类设置context ,在这个例子中,我只使用了标题 - 但自定义类就可以了。 如果您在此处设置它,那么任何带有处理程序等的日志记录也将可以访问context
下面的logWithContext设置 MDC 并在之后清除它。 显然,这可以用你喜欢的任何东西代替。

public class RequestIdFilter  implements WebFilter {

    private Logger LOG = LoggerFactory.getLogger(RequestIdFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        return chain.filter(exchange)
                .doAfterSuccessOrError((r, t) -> logWithContext(headers, httpHeaders -> LOG.info("Some message with MDC set")))
                .subscriberContext(Context.of(HttpHeaders.class, headers));
    }

    static void logWithContext(HttpHeaders headers, Consumer<HttpHeaders> logAction) {
        try {
            headers.forEach((name, values) -> MDC.put(name, values.get(0)));
            logAction.accept(headers);
        } finally {
            headers.keySet().forEach(MDC::remove);
        }

    }

}

这是一种基于最新方法的解决方案,截至2021 年 5 月,取自官方文档

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Signal;
import reactor.util.context.Context;

@Slf4j
@Configuration
public class RequestIdFilter implements WebFilter {

  @Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    ServerHttpRequest request = exchange.getRequest();
    String requestId = getRequestId(request.getHeaders());
    return chain
        .filter(exchange)
        .doOnEach(logOnEach(r -> log.info("{} {}", request.getMethod(), request.getURI())))
        .contextWrite(Context.of("CONTEXT_KEY", requestId));
  }

  private String getRequestId(HttpHeaders headers) {
    List<String> requestIdHeaders = headers.get("X-Request-ID");
    return requestIdHeaders == null || requestIdHeaders.isEmpty()
        ? UUID.randomUUID().toString()
        : requestIdHeaders.get(0);
  }

  public static <T> Consumer<Signal<T>> logOnEach(Consumer<T> logStatement) {
    return signal -> {
      String contextValue = signal.getContextView().get("CONTEXT_KEY");
      try (MDC.MDCCloseable cMdc = MDC.putCloseable("MDC_KEY", contextValue)) {
        logStatement.accept(signal.get());
      }
    };
  }

  public static <T> Consumer<Signal<T>> logOnNext(Consumer<T> logStatement) {
    return signal -> {
      if (!signal.isOnNext()) return;
      String contextValue = signal.getContextView().get("CONTEXT_KEY");
      try (MDC.MDCCloseable cMdc = MDC.putCloseable("MDC_KEY", contextValue)) {
        logStatement.accept(signal.get());
      }
    };
  }
}

鉴于您的application.properties有以下行:

logging.pattern.level=[%X{MDC_KEY}] %5p

那么每次调用端点时,您的服务器日志都会包含这样的日志:

2021-05-06 17:07:41.852 [60b38305-7005-4a05-bac7-ab2636e74d94]  INFO 20158 --- [or-http-epoll-6] my.package.RequestIdFilter    : GET http://localhost:12345/my-endpoint/444444/

每次您想在反应式上下文中手动记录某些内容时,您都必须将以下内容添加到您的反应式链中:

.doOnEach(logOnNext(r -> log.info("Something")))

如果您希望将X-Request-ID传播到其他服务以进行分布式跟踪,您需要从响应式上下文(而不是从 MDC)读取它并使用以下内容包装您的WebClient代码:

Mono.deferContextual(
    ctx -> {
      RequestHeadersSpec<?> request = webClient.get().uri(uri);
      request = request.header("X-Request-ID", ctx.get("CONTEXT_KEY"));
      // The rest of your request logic...
    });

从 Spring Boot 2.2 开始,有 Schedulers.onScheduleHook 可以让您处理 MDC:

Schedulers.onScheduleHook("mdc", runnable -> {
    Map<String, String> map = MDC.getCopyOfContextMap();
    return () -> {
        if (map != null) {
            MDC.setContextMap(map);
        }
        try {
            runnable.run();
        } finally {
            MDC.clear();
        }
    };
});

或者, Hooks.onEachOperator 可用于通过订阅者上下文传递 MDC 值。

http://ttddyy.github.io/mdc-with-webclient-in-webmvc/

这不是完整的 MDC 解决方案,例如,在我的情况下,我无法清除 R2DBC 线程中的 MDC 值。

更新:这篇文章真的解决了我的 MDC 问题: https : //www.novatec-gmbh.de/en/blog/how-can-the-mdc-context-be-used-in-the-reactive-spring-applications/

它提供了基于订阅者上下文更新 MDC 的正确方法。

将它与由AuthenticationWebFilter填充的SecurityContext::class.java键结合起来,您将能够将用户登录放入您的日志中。

我的解决方案基于Reactor 3 Reference Guide 方法,但使用 doOnSuccess 而不是 doOnEach。

主要思想是通过以下方式使用Context进行 MDC 传播:

  1. 使用来自上游流的 MDC state 填充下游上下文(将由派生线程使用)(可以通过.contextWrite(context -> Context.of(MDC.getCopyOfContextMap()))
  2. 在派生线程中访问下游上下文并使用下游上下文中的值填充派生线程中的 MDC(主要挑战
  3. 清除下游上下文中的 MDC(可以通过.doFinally(signalType -> MDC.clear())完成)

主要问题是在派生线程中访问下游上下文。 您可以使用最方便的方法实施第 2 步)。 但这是我的解决方案:

webclient.post()
   .bodyValue(someRequestData)
   .retrieve()
   .bodyToMono(String.class)
// By this action we wrap our response with a new Mono and also 
// in parallel fill MDC with values from a downstream Context because
// we have an access to it
   .flatMap(wrapWithFilledMDC())
   .doOnSuccess(response -> someActionWhichRequiresFilledMdc(response)))
// Fill a downstream context with the current MDC state
   .contextWrite(context -> Context.of(MDC.getCopyOfContextMap())) 
// Allows us to clear MDC from derived threads
   .doFinally(signalType -> MDC.clear())
   .block();
// Function which implements second step from the above main idea
public static <T> Function<T, Mono<T>> wrapWithFilledMDC() {
// Using deferContextual we have an access to downstream Context, so
// we can just fill MDC in derived threads with 
// values from the downstream Context
   return item -> Mono.deferContextual(contextView -> {
// Function for filling MDC with Context values 
// (you can apply your action)
      fillMdcWithContextView(contextView);
      return Mono.just(item);
   });
}
public static void fillMdcWithContextValues(ContextView contextView) {
   contextView.forEach(
      (key, value) -> {
          if (key instanceof String keyStr && value instanceof String valueStr) {
             MDC.put(keyStr, valueStr);
          }
   });
}

这种方法也可以应用于 doOnError 和 onErrorResume 方法,因为主要思想是相同的。

使用的版本:

  • spring-boot:2.7.3
  • spring-webflux:5.3.22(来自 spring-boot)
  • 反应堆核心:3.4.22(来自 spring-webflux)
  • reactor.netty:1.0.22(来自 spring-webflux)

我通过以下方式实现了这一目标:-

package com.nks.app.filter;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author nks
 */
@Component
@Slf4j
public class SessionIDFilter implements WebFilter {
    
    private static final String APP_SESSION_ID = "app-session-id";
    
    /**
     * Process the Web request and (optionally) delegate to the next
     * {@code WebFilter} through the given {@link WebFilterChain}.
     *
     * @param serverWebExchange the current server exchange
     * @param webFilterChain    provides a way to delegate to the next filter
     * @return {@code Mono<Void>} to indicate when request processing is complete
     */
    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        serverWebExchange.getResponse()
                .getHeaders().add(APP_SESSION_ID, serverWebExchange.getRequest().getHeaders().getFirst(APP_SESSION_ID));
        MDC.put(APP_SESSION_ID, serverWebExchange.getRequest().getHeaders().getFirst(APP_SESSION_ID));
        log.info("[{}] : Inside filter of SessionIDFilter, ADDED app-session-id in MDC Logs", MDC.get(APP_SESSION_ID));
        return webFilterChain.filter(serverWebExchange);
    }
}

并且,可以记录与线程的app-session-id关联的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM