From e351cdf7850b2af4de5927e0ba2514451457e701 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 01:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20skywalking=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../config/TracerAutoConfiguration.java | 36 ++++++++-- ...rProperties.java => TracerProperties.java} | 2 +- .../{BizTracing.java => BizTrace.java} | 7 +- .../tracer/core/annotation/BizTracingAop.java | 32 --------- .../tracer/core/aop/BizTraceAspect.java | 65 +++++++++++++++++++ .../tracer/core/filter/TraceFilter.java | 33 ++++++++++ .../tracer/core/util/TracerUtils.java | 8 +-- .../LocalLogbackPatternConverter.java | 42 ------------ .../TraceIdPatternLogbackLayout.java | 37 ----------- .../web/core/enums/FilterOrderEnum.java | 1 + .../controller/auth/SysAuthController.java | 2 + src/main/resources/logback-spring.xml | 19 ++++-- 13 files changed, 159 insertions(+), 130 deletions(-) rename src/main/java/cn/iocoder/dashboard/framework/tracer/config/{BizTracerProperties.java => TracerProperties.java} (87%) rename src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/{BizTracing.java => BizTrace.java} (82%) delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java diff --git a/pom.xml b/pom.xml index fc89b9800..546c01014 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,11 @@ apm-toolkit-logback-1.x ${skywalking.version} + + org.apache.skywalking + apm-toolkit-opentracing + ${skywalking.version} + de.codecentric diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java index 917bc673c..aeb90af56 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java @@ -1,10 +1,15 @@ package cn.iocoder.dashboard.framework.tracer.config; -import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTracingAop; +import cn.iocoder.dashboard.framework.tracer.core.aop.BizTraceAspect; +import cn.iocoder.dashboard.framework.tracer.core.filter.TraceFilter; +import cn.iocoder.dashboard.framework.web.core.enums.FilterOrderEnum; +import io.opentracing.Tracer; +import org.apache.skywalking.apm.toolkit.opentracing.SkywalkingTracer; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -14,21 +19,38 @@ import org.springframework.context.annotation.Configuration; * @author mashu */ @Configuration -@ConditionalOnClass({BizTracingAop.class}) -@EnableConfigurationProperties(BizTracerProperties.class) +@ConditionalOnClass({BizTraceAspect.class}) +@EnableConfigurationProperties(TracerProperties.class) @ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) public class TracerAutoConfiguration { @Bean @ConditionalOnMissingBean - public BizTracerProperties bizTracerProperties() { - return new BizTracerProperties(); + public TracerProperties bizTracerProperties() { + return new TracerProperties(); } @Bean @ConditionalOnMissingBean - public BizTracingAop bizTracingAop() { - return new BizTracingAop(); + public BizTraceAspect bizTracingAop() { + return new BizTraceAspect(); + } + + @Bean + @ConditionalOnMissingBean + public Tracer tracer() { + return new SkywalkingTracer(); + } + + /** + * 创建 TraceFilter 过滤器,响应 header 设置 traceId + */ + @Bean + public FilterRegistrationBean traceFilter() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new TraceFilter()); + registrationBean.setOrder(FilterOrderEnum.TRACE_FILTER); + return registrationBean; } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java similarity index 87% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java index 798d2eea3..093179cad 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java @@ -10,5 +10,5 @@ import org.springframework.boot.context.properties.ConfigurationProperties; */ @ConfigurationProperties("yudao.tracer") @Data -public class BizTracerProperties { +public class TracerProperties { } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java similarity index 82% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java index c663ce62d..c371cabc1 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java @@ -10,7 +10,7 @@ import java.lang.annotation.*; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited -public @interface BizTracing { +public @interface BizTrace { /** * 业务编号 tag 名 @@ -21,6 +21,11 @@ public @interface BizTracing { */ String TYPE_TAG = "biz.type"; + /** + * @return 操作名 + */ + String operationName() default ""; + /** * @return 业务编号 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java deleted file mode 100644 index 8ce87c027..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.core.annotation; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.util.sping.SpElUtil; -import lombok.extern.slf4j.Slf4j; -import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; - -/** - * 业务链路AOP切面 - * - * @author mashu - */ -@Aspect -@Slf4j -public class BizTracingAop { - - @Around(value = "@annotation(bizTracing)") - public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { - String bizId = (String) SpElUtil.analysisSpEl(bizTracing.id(), joinPoint); - String bizType = (String) SpElUtil.analysisSpEl(bizTracing.type(), joinPoint); - if (StrUtil.isBlankIfStr(bizId)) { - log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); - return; - } - log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); - ActiveSpan.tag(BizTracing.ID_TAG, bizId); - ActiveSpan.tag(BizTracing.TYPE_TAG, bizType); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java new file mode 100644 index 000000000..5f1d945b0 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java @@ -0,0 +1,65 @@ +package cn.iocoder.dashboard.framework.tracer.core.aop; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTrace; +import cn.iocoder.dashboard.util.sping.SpElUtil; +import io.opentracing.Span; +import io.opentracing.Tracer; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; + +import javax.annotation.Resource; + +/** + * {@link BizTrace} 切面,记录业务链路 + * + * @author mashu + */ +@Aspect +@Slf4j +public class BizTraceAspect { + + private static final String BIZ_OPERATION_NAME_PREFIX = "Biz/"; + + @Resource + private Tracer tracer; + + @Around(value = "@annotation(trace)") + public Object around(ProceedingJoinPoint joinPoint, BizTrace trace) throws Throwable { + // 创建 span + String operationName = getOperationName(joinPoint, trace); + Span span = tracer.buildSpan(operationName).startManual(); + try { + // 执行原有方法 + return joinPoint.proceed(); + } finally { + // 设置 Span 的 biz 属性 + setBizTag(span, joinPoint, trace); + // 完成 Span + span.finish(); + } + } + + private String getOperationName(ProceedingJoinPoint joinPoint, BizTrace trace) { + // 自定义操作名 + if (StrUtil.isNotEmpty(trace.operationName())) { + return BIZ_OPERATION_NAME_PREFIX + trace.operationName(); + } + // 默认操作名,使用方法名 + return BIZ_OPERATION_NAME_PREFIX + + joinPoint.getSignature().getDeclaringType().getSimpleName() + + "/" + joinPoint.getSignature().getName(); + } + + private void setBizTag(Span span, ProceedingJoinPoint joinPoint, BizTrace trace) { + try { + span.setTag(BizTrace.TYPE_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.type(), joinPoint))); + span.setTag(BizTrace.ID_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.id(), joinPoint))); + } catch (Exception ex) { + log.error("[around][解析 bizType 与 bizId 发生异常]", ex); + } + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java new file mode 100644 index 000000000..bedd52889 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java @@ -0,0 +1,33 @@ +package cn.iocoder.dashboard.framework.tracer.core.filter; + +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Trace 过滤器,打印 traceId 到 header 中返回 + * + * @author 芋道源码 + */ +public class TraceFilter extends OncePerRequestFilter { + + /** + * Header 名 - 链路追踪编号 + */ + private static final String HEADER_NAME_TRACE_ID = "trace-id"; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws IOException, ServletException { + // 设置响应 traceId + response.addHeader(HEADER_NAME_TRACE_ID, TracerUtils.getTraceId()); + // 继续过滤 + chain.doFilter(request, response); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 25562a8eb..e8af904ba 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -2,8 +2,6 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import org.apache.skywalking.apm.toolkit.trace.TraceContext; -import java.util.UUID; - /** * 链路追踪工具类 * @@ -18,10 +16,8 @@ public class TracerUtils { } /** - * 获得链路追踪编号 - *

- * 直接返回skywalking 的TraceId 如果不存在的话为空字符串"" - *

+ * 获得链路追踪编号,直接返回 SkyWalking 的 TraceId。 + * 如果不存在的话为空字符串!!! * * @return 链路追踪编号 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java deleted file mode 100644 index 3b69098d2..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 cn.iocoder.dashboard.framework.tracer.skywalking; - -import ch.qos.logback.classic.pattern.ClassicConverter; -import ch.qos.logback.classic.spi.ILoggingEvent; -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; - -/** - * Created by mashu on 2021/3/6. - */ -public class LocalLogbackPatternConverter extends ClassicConverter { - /** - * 作为默认的方式, 从{@link TracerUtils}中获取traceId, - * 需要用这个去替代logback的Layout, - * 同时避免了sky-walking agent生效的情况下仍然输出定值的问题. - * - * @param iLoggingEvent the event - * @return the traceId: UUID, or the real traceId. - */ - @Override - public String convert(ILoggingEvent iLoggingEvent) { - return TracerUtils.getTraceId(); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java deleted file mode 100644 index 2f13ce517..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 cn.iocoder.dashboard.framework.tracer.skywalking; - -import ch.qos.logback.classic.PatternLayout; - -/** - * Based on the logback-component convert register mechanism, - * register {@link LocalLogbackPatternConverter} as a new convert, match to "tid". - * You can use "%tid" in logback config file, "Pattern" section. - * If sky-walking agent is not active mode, it will use UUID as tid. - *

- * logback 的转换组件,为tid 添加占位符的转换器 - * Created by mashu on 2021/3/6. - */ -public class TraceIdPatternLogbackLayout extends PatternLayout { - static { - defaultConverterMap.put("tid", LocalLogbackPatternConverter.class.getName()); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java b/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java index 7c6ac8c6a..93bdb032e 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java +++ b/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java @@ -9,6 +9,7 @@ public interface FilterOrderEnum { int CORS_FILTER = Integer.MIN_VALUE; + int TRACE_FILTER = CORS_FILTER + 1; int REQUEST_BODY_CACHE_FILTER = Integer.MIN_VALUE + 500; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java index 39bef0a17..16612f2f6 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.controller.auth; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; +import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTrace; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthLoginRespVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthMenuRespVO; @@ -58,6 +59,7 @@ public class SysAuthController { @GetMapping("/get-permission-info") @ApiOperation("获取登陆用户的权限信息") + @BizTrace(id = "1", type = "'user'") public CommonResult getPermissionInfo() { // 获得用户信息 SysUserDO user = userService.getUser(getLoginUserId()); diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index fcf202143..16f7d5261 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -1,11 +1,13 @@ -    +   +         - + %d{ISO8601} | %tid | %thread | %-5level | %msg%n + ./logs/ruoyi-vue-pro-%d{yyyy-MM-dd_HH}.log @@ -20,13 +22,20 @@ 10MB + 0 10 + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n + + s @@ -35,12 +44,14 @@ + + @@ -48,7 +59,7 @@ -        +              @@ -56,4 +67,4 @@ - \ No newline at end of file +