使用
spring-boot-starter-parent
3.0.6 与
spring-boot-starter-webflux
spring-boot-starter-security
micrometer-tracing
micrometer-tracing-bridge-otel
我也在我的主要
Hooks.enableAutomaticContextPropagation();
课上使用@SpringBootApplication
;
有一个简单的过滤器,应该只将当前跟踪 ID 注入响应:
package some.controller;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
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;
import java.util.UUID;
@Slf4j
@Component
public class AddResponseHeaderWebFilter implements WebFilter, Ordered {
private final Tracer tracer;
public AddResponseHeaderWebFilter(
final Tracer tracer
) {
this.tracer = tracer;
}
@SuppressWarnings("NullableProblems")
@Override
public Mono<Void> filter(final ServerWebExchange exchange, final WebFilterChain chain) {
return chain.filter(withCallback(exchange));
}
private ServerWebExchange withCallback(final ServerWebExchange exchange) {
exchange
.getResponse()
.beforeCommit(() -> {
HttpHeaders httpHeaders = exchange.getResponse().getHeaders();
httpHeaders.add("trace-id", callId());
return Mono.empty();
});
return exchange;
}
private static String fallbackValue() {
String fallbackValue = UUID.randomUUID().toString();
log.warn("Could not use trace ID; generated dummy UUID for tracking purposes: {}", fallbackValue);
return fallbackValue;
}
private String callId() {
Span currentSpan = this.tracer.currentSpan();
return currentSpan == null
? fallbackValue()
: currentSpan.context().traceId();
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
如果我的控制器返回
Mono<List<Object>>
或 Mono<Object>
,它会很好地工作,但当控制器返回时总是陷入“回退”Flux<Object>
;
这是一个已知的限制吗(它确实在 SB 2.7.x 中与 Sleuth 一起工作)还是需要一些额外的配置/代码更改?
注意:对于
Flux<Object>
响应,我看到添加了“传输编码:分块”标头