2.7.0
This commit is contained in:
parent
bbb4cb9fb1
commit
060aaa9e20
4 changed files with 87 additions and 2 deletions
|
|
@ -62,7 +62,7 @@
|
|||
<!-- 本 POM 无 parent,版本号只能在此单独维护;
|
||||
发布时统一用 -Drevision=x.y.z 覆盖(用户属性优先级高于此处),
|
||||
若手工改版本,必须同步修改 simple-starter-dependencies/pom.xml 中的 revision -->
|
||||
<revision>2.6.4</revision>
|
||||
<revision>2.7.0</revision>
|
||||
<!-- 必须与 simple-starter-dependencies 的 spring-boot.version 保持一致 -->
|
||||
<spring-boot.version>4.1.2</spring-boot.version>
|
||||
</properties>
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@
|
|||
|
||||
<properties>
|
||||
<!-- Project Version -->
|
||||
<revision>2.6.4</revision>
|
||||
<revision>2.7.0</revision>
|
||||
<!-- Core Framework Versions -->
|
||||
<spring-boot.version>4.1.2</spring-boot.version>
|
||||
<spring-cloud.version>2025.1.4</spring-cloud.version>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue