masteer-jdk17 opentracing升级为opentelemetry
parent
5211b4e64f
commit
a6f9209c10
|
|
@ -44,9 +44,8 @@
|
|||
<!-- 服务保障相关 -->
|
||||
<lock4j.version>2.2.7</lock4j.version>
|
||||
<!-- 监控相关 -->
|
||||
<skywalking.version>9.5.0</skywalking.version>
|
||||
<spring-boot-admin.version>3.5.2</spring-boot-admin.version>
|
||||
<opentracing.version>0.33.0</opentracing.version>
|
||||
<opentelemetry.version>1.39.0</opentelemetry.version>
|
||||
<!-- Test 测试相关 -->
|
||||
<podam.version>8.0.2.RELEASE</podam.version>
|
||||
<jedis-mock.version>1.1.11</jedis-mock.version>
|
||||
|
|
@ -382,44 +381,19 @@
|
|||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-trace</artifactId>
|
||||
<version>${skywalking.version}</version>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-api</artifactId>
|
||||
<version>${opentelemetry.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-logback-1.x</artifactId>
|
||||
<version>${skywalking.version}</version>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-sdk</artifactId>
|
||||
<version>${opentelemetry.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-opentracing</artifactId>
|
||||
<version>${skywalking.version}</version>
|
||||
<!-- <exclusions>-->
|
||||
<!-- <exclusion>-->
|
||||
<!-- <artifactId>opentracing-api</artifactId>-->
|
||||
<!-- <groupId>io.opentracing</groupId>-->
|
||||
<!-- </exclusion>-->
|
||||
<!-- <exclusion>-->
|
||||
<!-- <artifactId>opentracing-util</artifactId>-->
|
||||
<!-- <groupId>io.opentracing</groupId>-->
|
||||
<!-- </exclusion>-->
|
||||
<!-- </exclusions>-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-api</artifactId>
|
||||
<version>${opentracing.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-util</artifactId>
|
||||
<version>${opentracing.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-noop</artifactId>
|
||||
<version>${opentracing.version}</version>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-exporter-otlp</artifactId>
|
||||
<version>${opentelemetry.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -73,8 +73,8 @@
|
|||
|
||||
<!-- 监控相关 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-trace</artifactId>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 工具类相关 -->
|
||||
|
|
|
|||
|
|
@ -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() : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,26 +42,27 @@
|
|||
|
||||
<!-- 监控相关 -->
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-util</artifactId>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-trace</artifactId>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-sdk</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-logback-1.x</artifactId>
|
||||
<groupId>io.opentelemetry</groupId>
|
||||
<artifactId>opentelemetry-exporter-otlp</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-opentracing</artifactId>
|
||||
<optional>true</optional>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Micrometer 对 Prometheus 的支持 -->
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
|
|
|
|||
|
|
@ -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> traceFilter() {
|
||||
FilterRegistrationBean<TraceFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
registrationBean.setFilter(new TraceFilter());
|
||||
registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER);
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
|
|
@ -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> traceFilter() {
|
||||
FilterRegistrationBean<TraceFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
registrationBean.setFilter(new TraceFilter());
|
||||
registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 创建 TraceFilter 过滤器,响应 header 设置 traceId
|
||||
// */
|
||||
// @Bean
|
||||
// public FilterRegistrationBean<TraceFilter> traceFilter() {
|
||||
// FilterRegistrationBean<TraceFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
// registrationBean.setFilter(new TraceFilter());
|
||||
// registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER);
|
||||
// return registrationBean;
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, Object> 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, Object> errorLogs(Throwable throwable) {
|
||||
Map<String, Object> errorLogs = new HashMap<String, Object>(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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue