feat: 集成TLog,轻量级的分布式日志标记追踪神器

* 集成TLog,轻量级的分布式日志标记追踪神器
This commit is contained in:
jasmine 2024-01-30 13:40:49 +00:00 committed by Charles7c
parent d5716d661f
commit e4180fb976
9 changed files with 283 additions and 5 deletions

View File

@ -107,5 +107,11 @@
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-json-jackson</artifactId>
</dependency>
<!--轻量级的分布式日志标记追踪神器-->
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>tlog-web-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022-present Charles7c 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 top.charles7c.continew.admin.common.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* TLog 配置属性
*
* @author Jasmine
* @since 2024/01/30 11:39
*/
@Data
@Component
@ConfigurationProperties(prefix = "tlog")
public class TLogProperties {
/** 日志标签模板 */
private String pattern;
/** 自动打印调用参数和时间 */
private Boolean enableInvokeTimePrint;
/** 自定义TraceId生成器 */
private String idGenerator;
/** MDC模式 */
private Boolean mdcEnable;
}

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2022-present Charles7c 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 top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.id.TLogIdGeneratorLoader;
import com.yomahub.tlog.spring.TLogPropertyInit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import top.charles7c.continew.admin.common.config.properties.TLogProperties;
/**
* TLog 配置
*
* @see TLogConfiguration
* @author Jasmine
* @since 2024/01/30 11:39
*/
@Configuration
public class TLogConfiguration {
private final TLogProperties tLogProperties;
public TLogConfiguration(TLogProperties tLogProperties) {
this.tLogProperties = tLogProperties;
}
@Bean
@Primary
public TLogPropertyInit tLogPropertyInit() {
TLogPropertyInit tLogPropertyInit = new TLogPropertyInit();
tLogPropertyInit.setPattern(tLogProperties.getPattern());
tLogPropertyInit.setEnableInvokeTimePrint(tLogProperties.getEnableInvokeTimePrint());
tLogPropertyInit.setMdcEnable(tLogProperties.getMdcEnable());
// 设置自定义TraceId生成器
TLogIdGeneratorLoader.setIdGenerator(new TraceIdGenerator());
return tLogPropertyInit;
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2022-present Charles7c 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 top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.context.TLogContext;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* TLog 过滤器
*
* @see TLogConfiguration
* @author Jasmine
* @since 2024/01/30 11:39
*/
@Component
public class TLogServletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
try {
TLogWebCommon.loadInstance().preHandle((HttpServletRequest)request);
// 把traceId放入response的header为了方便有些人有这样的需求从前端拿整条链路的traceId
((HttpServletResponse)response).addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
chain.doFilter(request, response);
return;
} finally {
TLogWebCommon.loadInstance().afterCompletion();
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2022-present Charles7c 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 top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.core.rpc.TLogLabelBean;
import com.yomahub.tlog.core.rpc.TLogRPCHandler;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TLog
*
* @see TLogWebCommon
* @author Jasmine
* @since 2024/01/30 11:39
*/
public class TLogWebCommon extends TLogRPCHandler {
private final static Logger log = LoggerFactory.getLogger(TLogWebCommon.class);
private static volatile TLogWebCommon tLogWebCommon;
public static TLogWebCommon loadInstance() {
if (tLogWebCommon == null) {
synchronized (TLogWebCommon.class) {
if (tLogWebCommon == null) {
tLogWebCommon = new TLogWebCommon();
}
}
}
return tLogWebCommon;
}
public void preHandle(HttpServletRequest request) {
String traceId = request.getHeader(TLogConstants.TLOG_TRACE_KEY);
String spanId = request.getHeader(TLogConstants.TLOG_SPANID_KEY);
String preIvkApp = request.getHeader(TLogConstants.PRE_IVK_APP_KEY);
String preIvkHost = request.getHeader(TLogConstants.PRE_IVK_APP_HOST);
String preIp = request.getHeader(TLogConstants.PRE_IP_KEY);
TLogLabelBean labelBean = new TLogLabelBean(preIvkApp, preIvkHost, preIp, traceId, spanId);
processProviderSide(labelBean);
}
public void afterCompletion() {
cleanThreadLocal();
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022-present Charles7c 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 top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.id.TLogIdGenerator;
/**
* TLog 配置
*
* @see TraceIdGenerator
* @author Jasmine
* @since 2024/01/30 11:39
*/
public class TraceIdGenerator extends TLogIdGenerator {
@Override
public String generateTraceId() {
String traceId = String.valueOf(System.nanoTime());
return traceId;
}
}

View File

@ -248,3 +248,9 @@ generator:
Controller:
templatePath: generator/Controller.ftl
packageName: controller
--- ### TLog
tlog:
enable-invoke-time-print: true
pattern: '[$spanId][$traceId]'
mdc-enable: false

View File

@ -25,7 +25,7 @@
<!-- 输出日志到控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<encoder class="com.yomahub.tlog.core.enhance.logback.AspectLogbackEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>${LOG_CHARSET}</charset>
</encoder>
@ -33,7 +33,7 @@
<!-- 输出日志到控制台(不带颜色) -->
<appender name="CONSOLE_PROD" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<encoder class="com.yomahub.tlog.core.enhance.logback.AspectLogbackEncoder">
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>${LOG_CHARSET}</charset>
</encoder>
@ -52,14 +52,14 @@
<!-- 日志保留天数 -->
<maxHistory>${FILE_MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder>
<encoder class="com.yomahub.tlog.core.enhance.logback.AspectLogbackEncoder">
<pattern>${FILE_LOG_PATTERN}</pattern>
<charset>${LOG_CHARSET}</charset>
</encoder>
</appender>
<!-- 输出日志到文件(异步) -->
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<appender name="ASYNC_FILE" class="com.yomahub.tlog.core.enhance.logback.async.AspectLogbackAsyncAppender">
<!-- 不丢失日志,默认:如果队列的 80% 已满,则会丢弃 TRACT、DEBUG、INFO 级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度该值会影响性能默认256 -->

View File

@ -33,6 +33,7 @@
<properties>
<!-- 项目版本号 -->
<revision>2.4.0-SNAPSHOT</revision>
<tlog.version>1.5.1</tlog.version>
</properties>
<!-- 全局依赖版本管理 -->
@ -72,6 +73,13 @@
<artifactId>continew-admin-common</artifactId>
<version>${revision}</version>
</dependency>
<!--轻量级的分布式日志标记追踪神器-->
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>tlog-web-spring-boot-starter</artifactId>
<version>${tlog.version}</version>
</dependency>
</dependencies>
</dependencyManagement>