From 060aaa9e2075e8308ec051e9286dcba476e2317b Mon Sep 17 00:00:00 2001 From: CangJieStore Date: Mon, 16 Nov 2026 22:50:22 +0800 Subject: [PATCH] 2.7.0 --- simple-starter-bom/pom.xml | 2 +- simple-starter-dependencies/pom.xml | 2 +- .../autoconfigure/TraceAutoConfiguration.java | 21 ++++++ .../filter/EnsureTraceMdcTurboFilter.java | 64 +++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 simple-starter-trace/src/main/java/reliable/simple/starter/trace/filter/EnsureTraceMdcTurboFilter.java diff --git a/simple-starter-bom/pom.xml b/simple-starter-bom/pom.xml index 9d32a1a..08a1d20 100644 --- a/simple-starter-bom/pom.xml +++ b/simple-starter-bom/pom.xml @@ -62,7 +62,7 @@ - 2.6.4 + 2.7.0 4.1.2 diff --git a/simple-starter-dependencies/pom.xml b/simple-starter-dependencies/pom.xml index c0742ed..ba0351c 100644 --- a/simple-starter-dependencies/pom.xml +++ b/simple-starter-dependencies/pom.xml @@ -63,7 +63,7 @@ - 2.6.4 + 2.7.0 4.1.2 2025.1.4 diff --git a/simple-starter-trace/src/main/java/reliable/simple/starter/trace/autoconfigure/TraceAutoConfiguration.java b/simple-starter-trace/src/main/java/reliable/simple/starter/trace/autoconfigure/TraceAutoConfiguration.java index cf8deb2..91cac24 100644 --- a/simple-starter-trace/src/main/java/reliable/simple/starter/trace/autoconfigure/TraceAutoConfiguration.java +++ b/simple-starter-trace/src/main/java/reliable/simple/starter/trace/autoconfigure/TraceAutoConfiguration.java @@ -16,10 +16,15 @@ package reliable.simple.starter.trace.autoconfigure; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.turbo.TurboFilter; +import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.PropertySource; import reliable.simple.starter.core.util.GeneralPropertySourceFactory; +import reliable.simple.starter.trace.filter.EnsureTraceMdcTurboFilter; /** * 链路追踪自动配置基类(基于 Micrometer Tracing + OpenTelemetry) @@ -37,4 +42,20 @@ import reliable.simple.starter.core.util.GeneralPropertySourceFactory; @PropertySource(value = "classpath:default-trace.yml", factory = GeneralPropertySourceFactory.class) public class TraceAutoConfiguration { + /** + * 启动后挂上,让后台线程的日志也写出 mdc.traceId。 + */ + @Bean + public EnsureTraceMdcTurboFilter ensureTraceMdcTurboFilter() { + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); + for (TurboFilter existing : context.getTurboFilterList()) { + if (existing instanceof EnsureTraceMdcTurboFilter filter) { + return filter; + } + } + EnsureTraceMdcTurboFilter filter = new EnsureTraceMdcTurboFilter(); + filter.start(); + context.addTurboFilter(filter); + return filter; + } } \ No newline at end of file diff --git a/simple-starter-trace/src/main/java/reliable/simple/starter/trace/filter/EnsureTraceMdcTurboFilter.java b/simple-starter-trace/src/main/java/reliable/simple/starter/trace/filter/EnsureTraceMdcTurboFilter.java new file mode 100644 index 0000000..dd1e0fb --- /dev/null +++ b/simple-starter-trace/src/main/java/reliable/simple/starter/trace/filter/EnsureTraceMdcTurboFilter.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022-present Xcynet Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package reliable.simple.starter.trace.filter; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.turbo.TurboFilter; +import ch.qos.logback.core.spi.FilterReply; +import io.opentelemetry.api.trace.Span; +import java.util.concurrent.ThreadLocalRandom; +import org.slf4j.MDC; +import org.slf4j.Marker; +import reliable.simple.starter.trace.constant.TraceMdcKeys; + +/** + * 每条日志都带 mdc.traceId。请求线程沿用入口过滤器写入的值,其余线程补一个并留在该线程上。 + * + * @author Copyright with xcynet + */ +public class EnsureTraceMdcTurboFilter extends TurboFilter { + + @Override + public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) { + String traceId = MDC.get(TraceMdcKeys.TRACE_ID); + if (traceId != null && !traceId.isBlank()) { + return FilterReply.NEUTRAL; + } + Span span = Span.current(); + if (span.getSpanContext().isValid()) { + MDC.put(TraceMdcKeys.TRACE_ID, span.getSpanContext().getTraceId()); + if (MDC.get(TraceMdcKeys.SPAN_ID) == null) { + MDC.put(TraceMdcKeys.SPAN_ID, span.getSpanContext().getSpanId()); + } + return FilterReply.NEUTRAL; + } + MDC.put(TraceMdcKeys.TRACE_ID, randomTraceId()); + return FilterReply.NEUTRAL; + } + + private static String randomTraceId() { + byte[] bytes = new byte[16]; + ThreadLocalRandom.current().nextBytes(bytes); + StringBuilder builder = new StringBuilder(32); + for (byte value : bytes) { + builder.append(Character.forDigit((value >> 4) & 0xF, 16)); + builder.append(Character.forDigit(value & 0xF, 16)); + } + return builder.toString(); + } +}