diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 0ef5047ec..0cbe8956e 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -44,9 +44,8 @@ 2.2.7 - 9.5.0 3.5.2 - 0.33.0 + 1.39.0 8.0.2.RELEASE 1.1.11 @@ -382,44 +381,19 @@ - org.apache.skywalking - apm-toolkit-trace - ${skywalking.version} + io.opentelemetry + opentelemetry-api + ${opentelemetry.version} - org.apache.skywalking - apm-toolkit-logback-1.x - ${skywalking.version} + io.opentelemetry + opentelemetry-sdk + ${opentelemetry.version} - org.apache.skywalking - apm-toolkit-opentracing - ${skywalking.version} - - - - - - - - - - - - - io.opentracing - opentracing-api - ${opentracing.version} - - - io.opentracing - opentracing-util - ${opentracing.version} - - - io.opentracing - opentracing-noop - ${opentracing.version} + io.opentelemetry + opentelemetry-exporter-otlp + ${opentelemetry.version} diff --git a/yudao-framework/yudao-common/pom.xml b/yudao-framework/yudao-common/pom.xml index 8953bb16f..a577ada57 100644 --- a/yudao-framework/yudao-common/pom.xml +++ b/yudao-framework/yudao-common/pom.xml @@ -73,8 +73,8 @@ - org.apache.skywalking - apm-toolkit-trace + io.opentelemetry + opentelemetry-api diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/monitor/TracerUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/monitor/TracerUtils.java index 81092b656..4a804a069 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/monitor/TracerUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/monitor/TracerUtils.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.framework.common.util.monitor; -import org.apache.skywalking.apm.toolkit.trace.TraceContext; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; /** * 链路追踪工具类 @@ -24,7 +25,9 @@ public class TracerUtils { * @return 链路追踪编号 */ public static String getTraceId() { - return TraceContext.traceId(); + Span currentSpan = Span.current(); + SpanContext context = currentSpan.getSpanContext(); + return context.isValid() ? context.getTraceId() : ""; } } diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/pom.xml b/yudao-framework/yudao-spring-boot-starter-monitor/pom.xml index de0a0ed74..285cff2a9 100644 --- a/yudao-framework/yudao-spring-boot-starter-monitor/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-monitor/pom.xml @@ -42,26 +42,27 @@ - io.opentracing - opentracing-util + io.opentelemetry + opentelemetry-api true - org.apache.skywalking - apm-toolkit-trace + io.opentelemetry + opentelemetry-sdk true - org.apache.skywalking - apm-toolkit-logback-1.x + io.opentelemetry + opentelemetry-exporter-otlp true - org.apache.skywalking - apm-toolkit-opentracing - true + org.springframework.boot + spring-boot-starter-webflux + compile + io.micrometer diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoServletTracerAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoServletTracerAutoConfiguration.java new file mode 100644 index 000000000..de169d5b5 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoServletTracerAutoConfiguration.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.framework.tracer.config; + +import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; +import cn.iocoder.yudao.framework.tracer.core.aop.BizTraceAspect; +import cn.iocoder.yudao.framework.tracer.core.filter.TraceFilter; +import io.opentelemetry.api.trace.Tracer; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +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; + +@AutoConfiguration +@ConditionalOnClass(jakarta.servlet.Filter.class) +@EnableConfigurationProperties(TracerProperties.class) +@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) +public class YudaoServletTracerAutoConfiguration { + + @Value("${spring.application.name}") + private String applicationName; + + @Bean + public Tracer tracer() { + return io.opentelemetry.api.GlobalOpenTelemetry.getTracer(applicationName); + } + + @Bean + public BizTraceAspect bizTracingAop(Tracer tracer) { + return new BizTraceAspect(tracer); + } + + @Bean + public FilterRegistrationBean traceFilter() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new TraceFilter()); + registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER); + return registrationBean; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoTracerAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoTracerAutoConfiguration.java index 596b51675..3f91ac2a7 100644 --- a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoTracerAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoTracerAutoConfiguration.java @@ -1,53 +1,57 @@ -package cn.iocoder.yudao.framework.tracer.config; - -import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; -import cn.iocoder.yudao.framework.tracer.core.filter.TraceFilter; -import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -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; - -/** - * Tracer 配置类 - * - * @author mashu - */ -@AutoConfiguration -@ConditionalOnClass(name = { - "org.apache.skywalking.apm.toolkit.opentracing.SkywalkingTracer", // 来自 apm-toolkit-opentracing.jar -// "io.opentracing.Tracer", // 来自 opentracing-api.jar - "jakarta.servlet.Filter" -}) -@EnableConfigurationProperties(TracerProperties.class) -@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) -public class YudaoTracerAutoConfiguration { - - // TODO @芋艿:skywalking 不兼容最新的 opentracing 版本。同时,opentracing 也停止了维护,尬住了!后续换 opentelemetry 即可! +//package cn.iocoder.yudao.framework.tracer.config; +// +//import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; +//import cn.iocoder.yudao.framework.tracer.core.aop.BizTraceAspect; +//import cn.iocoder.yudao.framework.tracer.core.filter.TraceFilter; +//import io.opentelemetry.api.trace.Tracer; +//import org.springframework.beans.factory.annotation.Value; +//import org.springframework.boot.autoconfigure.AutoConfiguration; +//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +//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; +// +///** +// * Tracer 配置类 +// * +// * @author mashu +// */ +//@AutoConfiguration +//@ConditionalOnClass(name = { +// "io.opentelemetry.api.trace.Tracer", // 来自 opentelemetry-api.jar +//}) +//@EnableConfigurationProperties(TracerProperties.class) +//@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) +//public class YudaoTracerAutoConfiguration { +// +// @Value("${spring.application.name}") +// private String applicationName; +// +// // @Bean // public BizTraceAspect bizTracingAop() { // return new BizTraceAspect(tracer()); // } // +// /** +// * 创建 OpenTelemetry Tracer +// */ // @Bean // public Tracer tracer() { -// // 创建 SkywalkingTracer 对象 -// SkywalkingTracer tracer = new SkywalkingTracer(); -// // 设置为 GlobalTracer 的追踪器 -// GlobalTracer.registerIfAbsent(tracer); -// return tracer; +// return io.opentelemetry.api.GlobalOpenTelemetry.getTracer(applicationName); // } - - /** - * 创建 TraceFilter 过滤器,响应 header 设置 traceId - */ - @Bean - public FilterRegistrationBean traceFilter() { - FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); - registrationBean.setFilter(new TraceFilter()); - registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER); - return registrationBean; - } - -} +// +// +// /** +// * 创建 TraceFilter 过滤器,响应 header 设置 traceId +// */ +// @Bean +// public FilterRegistrationBean traceFilter() { +// FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); +// registrationBean.setFilter(new TraceFilter()); +// registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER); +// return registrationBean; +// } +// +//} diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoWebFluxTracerAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoWebFluxTracerAutoConfiguration.java new file mode 100644 index 000000000..8799f40f0 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/config/YudaoWebFluxTracerAutoConfiguration.java @@ -0,0 +1,44 @@ +package cn.iocoder.yudao.framework.tracer.config; + +import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; +import cn.iocoder.yudao.framework.tracer.core.aop.BizTraceAspect; +import cn.iocoder.yudao.framework.tracer.core.filter.TraceFilter; +import io.opentelemetry.api.trace.Tracer; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +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.web.server.WebFilter; + +@AutoConfiguration +@ConditionalOnClass(WebFilter.class) +@EnableConfigurationProperties(TracerProperties.class) +@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) +public class YudaoWebFluxTracerAutoConfiguration { + + @Value("${spring.application.name}") + private String applicationName; + + @Bean + public Tracer tracer() { + return io.opentelemetry.api.GlobalOpenTelemetry.getTracer(applicationName); + } + + @Bean + public BizTraceAspect bizTracingAop(Tracer tracer) { + return new BizTraceAspect(tracer); + } + + @Bean + public WebFilter traceWebFilter() { + return (exchange, chain) -> { + // 你在这里从 OpenTelemetry 拿 traceId + // exchange.getResponse().getHeaders().add("traceId", xxx); + return chain.filter(exchange); + }; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/aop/BizTraceAspect.java b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/aop/BizTraceAspect.java index 3c3b9f3bb..b37fcd1ee 100644 --- a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/aop/BizTraceAspect.java +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/aop/BizTraceAspect.java @@ -5,9 +5,8 @@ import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.tracer.core.annotation.BizTrace; import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; import cn.iocoder.yudao.framework.tracer.core.util.TracerFrameworkUtils; -import io.opentracing.Span; -import io.opentracing.Tracer; -import io.opentracing.tag.Tags; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.Tracer; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; @@ -36,9 +35,9 @@ public class BizTraceAspect { public Object around(ProceedingJoinPoint joinPoint, BizTrace trace) throws Throwable { // 创建 span String operationName = getOperationName(joinPoint, trace); - Span span = tracer.buildSpan(operationName) - .withTag(Tags.COMPONENT.getKey(), "biz") - .start(); + Span span = tracer.spanBuilder(operationName) + .setAttribute("component", "biz") + .startSpan(); try { // 执行原有方法 return joinPoint.proceed(); @@ -49,7 +48,7 @@ public class BizTraceAspect { // 设置 Span 的 biz 属性 setBizTag(span, joinPoint, trace); // 完成 Span - span.finish(); + span.end(); } } @@ -67,8 +66,8 @@ public class BizTraceAspect { private void setBizTag(Span span, ProceedingJoinPoint joinPoint, BizTrace trace) { try { Map result = SpringExpressionUtils.parseExpressions(joinPoint, asList(trace.type(), trace.id())); - span.setTag(BizTrace.TYPE_TAG, MapUtil.getStr(result, trace.type())); - span.setTag(BizTrace.ID_TAG, MapUtil.getStr(result, trace.id())); + span.setAttribute(BizTrace.TYPE_TAG, MapUtil.getStr(result, trace.type())); + span.setAttribute(BizTrace.ID_TAG, MapUtil.getStr(result, trace.id())); } catch (Exception ex) { log.error("[setBizTag][解析 bizType 与 bizId 发生异常]", ex); } diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/util/TracerFrameworkUtils.java b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/util/TracerFrameworkUtils.java index 51323e7c2..c1ea40757 100644 --- a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/util/TracerFrameworkUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/java/cn/iocoder/yudao/framework/tracer/core/util/TracerFrameworkUtils.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.tracer.core.util; -import io.opentracing.Span; -import io.opentracing.tag.Tags; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.StatusCode; import java.io.PrintWriter; import java.io.StringWriter; @@ -22,25 +22,20 @@ public class TracerFrameworkUtils { * @param span Span */ public static void onError(Throwable throwable, Span span) { - Tags.ERROR.set(span, Boolean.TRUE); + if (span == null || !span.getSpanContext().isValid()) { + return; + } if (throwable != null) { - span.log(errorLogs(throwable)); + // 记录异常信息 + span.recordException(throwable); + span.setStatus(StatusCode.ERROR, throwable.getMessage()); + + // 如果需要,可以把完整堆栈放到属性里 + StringWriter sw = new StringWriter(); + throwable.printStackTrace(new PrintWriter(sw)); + span.setAttribute("error.stacktrace", sw.toString()); + } else { + span.setStatus(StatusCode.ERROR); } } - - private static Map errorLogs(Throwable throwable) { - Map errorLogs = new HashMap(10); - errorLogs.put("event", Tags.ERROR.getKey()); - errorLogs.put("error.object", throwable); - errorLogs.put("error.kind", throwable.getClass().getName()); - String message = throwable.getCause() != null ? throwable.getCause().getMessage() : throwable.getMessage(); - if (message != null) { - errorLogs.put("message", message); - } - StringWriter sw = new StringWriter(); - throwable.printStackTrace(new PrintWriter(sw)); - errorLogs.put("stack", sw.toString()); - return errorLogs; - } - } diff --git a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 581832fbc..0281bf2e8 100644 --- a/yudao-framework/yudao-spring-boot-starter-monitor/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/yudao-framework/yudao-spring-boot-starter-monitor/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,4 @@ -cn.iocoder.yudao.framework.tracer.config.YudaoTracerAutoConfiguration +#cn.iocoder.yudao.framework.tracer.config.YudaoTracerAutoConfiguration +cn.iocoder.yudao.framework.tracer.config.YudaoServletTracerAutoConfiguration +cn.iocoder.yudao.framework.tracer.config.YudaoWebFluxTracerAutoConfiguration cn.iocoder.yudao.framework.tracer.config.YudaoMetricsAutoConfiguration