From 4cc45d73fb828fa29da54baabf309f3eff6b2947 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 22:29:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20bizType=20=E5=92=8C=20bizId=20?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=B0=81=E8=A3=85=EF=BC=8C=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=20SkyWalking=20Tag=20=E6=A3=80=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/annotation/BizTracing.java | 22 ++++++++++ .../common/annotation/BizTracingAop.java | 27 +++++++++++++ .../tracer/core/util/TracerUtils.java | 4 +- .../dashboard/util/sping/SpElUtil.java | 40 +++++++++++++++++++ .../SpringBeanUtil.java} | 4 +- 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java create mode 100644 src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java create mode 100644 src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java rename src/main/java/cn/iocoder/dashboard/util/{bean/SpringUtil.java => sping/SpringBeanUtil.java} (95%) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java new file mode 100644 index 000000000..0ca4b4d8e --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -0,0 +1,22 @@ +package cn.iocoder.dashboard.common.annotation; + +import java.lang.annotation.*; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited + +/** + * 打印业务流水号/业务类型注解 + * + * @author 麻薯 + */ +public @interface BizTracing { + String BIZ_ID = "bizId"; + String BIZ_TYPE = "bizType"; + + String bizId() default "NULL_ID"; + + String bizType() default "NULL_TYPE"; + +} diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java new file mode 100644 index 000000000..dd9093988 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -0,0 +1,27 @@ +package cn.iocoder.dashboard.common.annotation; + +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; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.ResponseBody; + +@Aspect +@Slf4j +@Configuration +public class BizTracingAop { + + @Around(value = "@annotation(bizTracing)") + public void tagBizInfo( ProceedingJoinPoint joinPoint, BizTracing bizTracing) { + String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); + String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); + ActiveSpan.tag(BizTracing.BIZ_ID, bizId); + ActiveSpan.tag(BizTracing.BIZ_TYPE, bizType); + } +} 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 a9f85be41..dfcb349a3 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,7 +2,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; -import cn.iocoder.dashboard.util.bean.SpringUtil; +import cn.iocoder.dashboard.util.sping.SpringBeanUtil; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -61,7 +61,7 @@ public class TracerUtils { * @return ITrace */ private static ITrace getTracer() { - return SpringUtil.getBean(ITrace.class); + return SpringBeanUtil.getBean(ITrace.class); } /** diff --git a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java new file mode 100644 index 000000000..1291d326d --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java @@ -0,0 +1,40 @@ +package cn.iocoder.dashboard.util.sping; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.core.DefaultParameterNameDiscoverer; +import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.StandardEvaluationContext; + +import java.lang.reflect.Method; + +public class SpElUtil { + + private SpElUtil() { + } + + public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { + SpelExpressionParser parser = new SpelExpressionParser(); + DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + // 通过joinPoint获取被注解方法 + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + Method method = methodSignature.getMethod(); + // 使用spring的DefaultParameterNameDiscoverer获取方法形参名数组 + String[] paramNames = nameDiscoverer.getParameterNames(method); + // 解析过后的Spring表达式对象 + Expression expression = parser.parseExpression(spElString); + // spring的表达式上下文对象 + EvaluationContext context = new StandardEvaluationContext(); + // 通过joinPoint获取被注解方法的形参 + Object[] args = joinPoint.getArgs(); + // 给上下文赋值 + for (int i = 0; i < args.length; i++) { + context.setVariable(paramNames[i], args[i]); + } + Object value = expression.getValue(context); + return value == null ? "null" : value.toString(); + + } +} diff --git a/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java rename to src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java index 4cbfb4e36..c2fd5c487 100644 --- a/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.util.bean; +package cn.iocoder.dashboard.util.sping; import org.springframework.beans.BeansException; @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; @Component -public class SpringUtil implements ApplicationContextAware { +public class SpringBeanUtil implements ApplicationContextAware { /** * Spring context