Commit 4cc45d73fb828fa29da54baabf309f3eff6b2947
1 parent
3018866c
将 bizType 和 bizId 进行封装,实现 SkyWalking Tag 检索
Showing
5 changed files
with
93 additions
and
4 deletions
src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.common.annotation; | ||
| 2 | + | ||
| 3 | +import java.lang.annotation.*; | ||
| 4 | + | ||
| 5 | +@Target({ElementType.METHOD}) | ||
| 6 | +@Retention(RetentionPolicy.RUNTIME) | ||
| 7 | +@Inherited | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * 打印业务流水号/业务类型注解 | ||
| 11 | + * | ||
| 12 | + * @author 麻薯 | ||
| 13 | + */ | ||
| 14 | +public @interface BizTracing { | ||
| 15 | + String BIZ_ID = "bizId"; | ||
| 16 | + String BIZ_TYPE = "bizType"; | ||
| 17 | + | ||
| 18 | + String bizId() default "NULL_ID"; | ||
| 19 | + | ||
| 20 | + String bizType() default "NULL_TYPE"; | ||
| 21 | + | ||
| 22 | +} |
src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.common.annotation; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.dashboard.util.sping.SpElUtil; | ||
| 4 | +import lombok.extern.slf4j.Slf4j; | ||
| 5 | +import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; | ||
| 6 | +import org.aspectj.lang.ProceedingJoinPoint; | ||
| 7 | +import org.aspectj.lang.annotation.Around; | ||
| 8 | +import org.aspectj.lang.annotation.Aspect; | ||
| 9 | +import org.aspectj.lang.annotation.Before; | ||
| 10 | +import org.aspectj.lang.annotation.Pointcut; | ||
| 11 | +import org.springframework.context.annotation.Configuration; | ||
| 12 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
| 13 | + | ||
| 14 | +@Aspect | ||
| 15 | +@Slf4j | ||
| 16 | +@Configuration | ||
| 17 | +public class BizTracingAop { | ||
| 18 | + | ||
| 19 | + @Around(value = "@annotation(bizTracing)") | ||
| 20 | + public void tagBizInfo( ProceedingJoinPoint joinPoint, BizTracing bizTracing) { | ||
| 21 | + String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); | ||
| 22 | + String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); | ||
| 23 | + log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); | ||
| 24 | + ActiveSpan.tag(BizTracing.BIZ_ID, bizId); | ||
| 25 | + ActiveSpan.tag(BizTracing.BIZ_TYPE, bizType); | ||
| 26 | + } | ||
| 27 | +} |
src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java
| @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; | @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.StrUtil; | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.dashboard.framework.tracer.core.ITrace; | 4 | import cn.iocoder.dashboard.framework.tracer.core.ITrace; |
| 5 | -import cn.iocoder.dashboard.util.bean.SpringUtil; | 5 | +import cn.iocoder.dashboard.util.sping.SpringBeanUtil; |
| 6 | import org.apache.skywalking.apm.toolkit.trace.TraceContext; | 6 | import org.apache.skywalking.apm.toolkit.trace.TraceContext; |
| 7 | 7 | ||
| 8 | import java.util.UUID; | 8 | import java.util.UUID; |
| @@ -61,7 +61,7 @@ public class TracerUtils { | @@ -61,7 +61,7 @@ public class TracerUtils { | ||
| 61 | * @return ITrace | 61 | * @return ITrace |
| 62 | */ | 62 | */ |
| 63 | private static ITrace getTracer() { | 63 | private static ITrace getTracer() { |
| 64 | - return SpringUtil.getBean(ITrace.class); | 64 | + return SpringBeanUtil.getBean(ITrace.class); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | /** | 67 | /** |
src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.util.sping; | ||
| 2 | + | ||
| 3 | +import org.aspectj.lang.ProceedingJoinPoint; | ||
| 4 | +import org.aspectj.lang.reflect.MethodSignature; | ||
| 5 | +import org.springframework.core.DefaultParameterNameDiscoverer; | ||
| 6 | +import org.springframework.expression.EvaluationContext; | ||
| 7 | +import org.springframework.expression.Expression; | ||
| 8 | +import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
| 9 | +import org.springframework.expression.spel.support.StandardEvaluationContext; | ||
| 10 | + | ||
| 11 | +import java.lang.reflect.Method; | ||
| 12 | + | ||
| 13 | +public class SpElUtil { | ||
| 14 | + | ||
| 15 | + private SpElUtil() { | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { | ||
| 19 | + SpelExpressionParser parser = new SpelExpressionParser(); | ||
| 20 | + DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); | ||
| 21 | + // 通过joinPoint获取被注解方法 | ||
| 22 | + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | ||
| 23 | + Method method = methodSignature.getMethod(); | ||
| 24 | + // 使用spring的DefaultParameterNameDiscoverer获取方法形参名数组 | ||
| 25 | + String[] paramNames = nameDiscoverer.getParameterNames(method); | ||
| 26 | + // 解析过后的Spring表达式对象 | ||
| 27 | + Expression expression = parser.parseExpression(spElString); | ||
| 28 | + // spring的表达式上下文对象 | ||
| 29 | + EvaluationContext context = new StandardEvaluationContext(); | ||
| 30 | + // 通过joinPoint获取被注解方法的形参 | ||
| 31 | + Object[] args = joinPoint.getArgs(); | ||
| 32 | + // 给上下文赋值 | ||
| 33 | + for (int i = 0; i < args.length; i++) { | ||
| 34 | + context.setVariable(paramNames[i], args[i]); | ||
| 35 | + } | ||
| 36 | + Object value = expression.getValue(context); | ||
| 37 | + return value == null ? "null" : value.toString(); | ||
| 38 | + | ||
| 39 | + } | ||
| 40 | +} |
src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java renamed to src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java
| 1 | -package cn.iocoder.dashboard.util.bean; | 1 | +package cn.iocoder.dashboard.util.sping; |
| 2 | 2 | ||
| 3 | import org.springframework.beans.BeansException; | 3 | import org.springframework.beans.BeansException; |
| 4 | 4 | ||
| @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; | @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; | ||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | @Component | 14 | @Component |
| 15 | -public class SpringUtil implements ApplicationContextAware { | 15 | +public class SpringBeanUtil implements ApplicationContextAware { |
| 16 | 16 | ||
| 17 | /** | 17 | /** |
| 18 | * Spring context | 18 | * Spring context |