1. spring-cloud-sleuth+zipkin源码探究
1.1. 前言
粗略看了下spring cloud sleuth core源码,发现内容真的有点多,它支持了很多类型的链路追踪,我就找其中一个比较有代表性的深入剖析下源码结构和内容
1.2. spring-cloud-sleuth-core源码解析
1.2.1. 结构
- 可以看到源码中支持的追踪类型有很多,支持async,hystrix,websocket,rxjava,Spring mvc,servlet,spring restTemplate,feign,zuul等等,这里我着重探讨spring web mvc的链路追踪
- 打开web包,找到TraceWebAutoConfiguration,这里配置了主要的初始化类
1.2.2. 过滤器注册
- 当启动初始化程序时,跟踪代码如下
@Bean
public FilterRegistrationBean traceWebFilter(TraceFilter traceFilter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
traceFilter);
filterRegistrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE,
REQUEST);
filterRegistrationBean.setOrder(TraceFilter.ORDER);
return filterRegistrationBean;
}
@Bean
@ConditionalOnMissingBean
public TraceFilter traceFilter(BeanFactory beanFactory,
SkipPatternProvider skipPatternProvider) {
return new TraceFilter(beanFactory, skipPatternProvider.skipPattern());
}
- 初始化traceFilter,进行过滤器注册
1.2.3. 拦截器注册
- 然后看
TraceWebMvcConfigurer
类,它会进行拦截器的注册
@Configuration
class TraceWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Autowired BeanFactory beanFactory;
@Bean
public TraceHandlerInterceptor traceHandlerInterceptor(BeanFactory beanFactory) {
return new TraceHandlerInterceptor(beanFactory);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(this.beanFactory.getBean(TraceHandlerInterceptor.class));
}
}
- 在
TraceHandlerInterceptor
类中,preHandle
,afterCompletion
方法可以看出,这是对请求进行拦截进行span的包装
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
String spanName = spanName(handler);
boolean continueSpan = getRootSpanFromAttribute(request) != null;
Span span = continueSpan ? getRootSpanFromAttribute(request) : getTracer().createSpan(spanName);
if (log.isDebugEnabled()) {
log.debug("Handling span " + span);
}
addClassMethodTag(handler, span);
addClassNameTag(handler, span);
setSpanInAttribute(request, span);
if (!continueSpan) {
setNewSpanCreatedAttribute(request, span);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
if (isErrorControllerRelated(request)) {
if (log.isDebugEnabled()) {
log.debug("Skipping closing of a span for error controller processing");
}
return;
}
Span span = getRootSpanFromAttribute(request);
if (ex != null) {
getErrorParser().parseErrorTags(span, ex);
}
if (getNewSpanFromAttribute(request) != null) {
if (log.isDebugEnabled()) {
log.debug("Closing span " + span);
}
Span newSpan = getNewSpanFromAttribute(request);
getTracer().continueSpan(newSpan);
getTracer().close(newSpan);
clearNewSpanCreatedAttribute(request);
}
}
1.2.4. zipkin端点提交
- 这里首先会初始化
HttpZipkinSpanReporter
类,,用来进行span
端点提交,然后初始化ZipkinSpanListener
span的监听器,用来监听并调用端点提交,以上配置再下图位置
1.2.5. 调用http接口时,进入过滤器
- 首先进入
TraceFilter
中的过滤方法doFilter
,这里会做span
的创建
private Span createSpan(HttpServletRequest request,
boolean skip, Span spanFromRequest, String name) {
if (spanFromRequest != null) {
if (log.isDebugEnabled()) {
log.debug("Span has already been created - continuing with the previous one");
}
return spanFromRequest;
}
//加入调用链路ZipkinHttpSpanExtractor,此链路在TraceHttpAutoConfiguration中配置实例化,调用链还没有时,返回为空,作为头节点
Span parent = spanExtractor().joinTrace(new HttpServletRequestTextMap(request));
if (parent != null) {
if (log.isDebugEnabled()) {
log.debug("Found a parent span " + parent + " in the request");
}
addRequestTagsForParentSpan(request, parent);
spanFromRequest = parent;
tracer().continueSpan(spanFromRequest);
if (parent.isRemote()) {
parent.logEvent(Span.SERVER_RECV);
}
request.setAttribute(TRACE_REQUEST_ATTR, spanFromRequest);
if (log.isDebugEnabled()) {
log.debug("Parent span is " + parent + "");
}
} else {
if (skip) {
spanFromRequest = tracer().createSpan(name, NeverSampler.INSTANCE);
}
else {
String header = request.getHeader(Span.SPAN_FLAGS);
if (Span.SPAN_SAMPLED.equals(header)) {
spanFromRequest = tracer().createSpan(name, new AlwaysSampler());
} else {
//创建span节点
spanFromRequest = tracer().createSpan(name);
}
}
spanFromRequest.logEvent(Span.SERVER_RECV);
request.setAttribute(TRACE_REQUEST_ATTR, spanFromRequest);
if (log.isDebugEnabled()) {
log.debug("No parent span present - creating a new span");
}
}
return spanFromRequest;
}
1.2.6. 进入拦截器
- 在
preHandle
方法中,对span
进行包装,然后把span放入请求头header中 - 最后再
DefaultTracer
中进行span的关闭和spanReporter
的提交
参考:https://blog.csdn.net/zhllansezhilian/article/details/83001870