Spring Cloud Sleuth和skywalking-trace

目录

1. 内容

2. 什么是请求链路追踪

3. 官方文档

4. 追踪粒度

5. sleuth

6. skywalking trace

7. sleuth vs skywalking-trace


1. 内容

spring cloud sleuth和skywalking trace均提供了 请求链路追踪 能力,本文介绍了一些个人认为重要的内容。

2. 什么是请求链路追踪

追踪处理链路

3. 官方文档

Spring Cloud Sleuth Reference Documentation

Setup java agent | Apache SkyWalking

Zipkin receiver | Apache SkyWalking

The OpenTracing project

4. 追踪粒度

trace表示一个具体请求
span

来自sleuth的介绍:

表示请求中的部分流程,携带耗时、tag、event信息。

默认追踪API接口,如restful接口、RPC接口。

若要追踪其他接口,如service层,需要使用@NewSpan注解。

来自opentracing的介绍:

构建了一个分布式追踪块,代表一个在分布式系统中完成的工作单元。包括名称、开始结束时间、tags、logs、上下文。

5. sleuth

5.1. maven依赖

    org.springframework.cloudspring-cloud-starter-sleuth

5.2. send to zipkin 

spring-cloud-sleuth-zipkin

zipkin,可视化链路追踪系统。

官方doc

  • OpenZipkin · A distributed tracing system
  • Open Zipkin · GitHub

需要单独部署

  • zipkin/zipkin-server at master · openzipkin/zipkin · GitHub

支持多种存储方式

5.3. 支持追踪多种请求

 web、http、message、rpc(如dubbo)等 ,sleuth实现了相关扩展点,所以基本不需要开发者添加额外的tracer、span编码

5.4. sleuth原理

spring扩展文件

  • spring-cloud-sleuth-autoconfigure-3.0.1.jar!/META-INF/spring.factories

跟踪web app

  • 基于autoconfig,添加javax.servlet.Filter
  • org.springframework.cloud.sleuth.autoconfig.instrument.web.TraceWebAutoConfiguration
  • org.springframework.cloud.sleuth.autoconfig.instrument.web.TraceWebServletConfiguration
  • org.springframework.cloud.sleuth.instrument.web.servlet.TracingFilter#doFilter

跟踪dubbo

  • brave.Tracing
    • 为追踪仪器提供工具集
  • brave.propagation.CurrentTraceContext
    • 存储span的上下文,例如使用threadLocal
  • org.springframework.cloud.sleuth.autoconfig.brave.BraveAutoConfiguration#tracing
    • 生成工具集bean
  • brave.dubbo.TracingFilter implements org.apache.dubbo.rpc.Filter
    • 通过dubbo filter进行请求跟踪
  • org.springframework.cloud.sleuth.autoconfig.brave.instrument.rpc.BraveRpcAutoConfiguration
    • 生成工具集bean rpcTracing
  • client-server如何处理请求追踪上下文
    • brave.rpc.RpcClientHandler
    • brave.rpc.RpcServerHandler

追踪自定义span

  • 采用spring aop
  • org.springframework.cloud.sleuth.instrument.annotation.SleuthAdvisorConfig

6. skywalking trace

6.1. 涉及技术

opentracing、java agent、bytebuddy

6.2. 操作步骤

6.3. 数据获取方式

数据获取方式没有什么特殊的。 

6.4. java agent实现

依赖ByteBuddy,它是运行时创建或修改class文件的工具,不需要熟悉class格式。

skywalking-agent\plugins,多种中间件对trace context传输的支持,如spring-mvc、dubbo、mysql、kafka
例如org.apache.skywalking.apm.plugin.dubbo.DubboInterceptor、org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.AbstractMethodInterceptor

通过源码了解基本原理,入口SkyWalkingAgent:

premain基本步骤:

加载配置默认config/agent.config
加载插件

读取插件jar中skywalking-plugin.def
用plugin.def中指定的全路径加载class

修改字节码读取多种拦截point并使用bytebuddy进行加强

org.apache.skywalking.apm.agent.core包内主要类与作用

ClassInstanceMethodsEnhancePluginDefine

插件超类,制定了强化的具体步骤。

需要子类指定待强化的类以及强化点和拦截器关系(构造器用什么拦截器、实例方法methodA用拦截器interceptorA)

InstanceMethodsAroundInterceptor实例方法拦截器超类,实现beforeMethod、afterMethod
template.*拦截器模板
SkyWalkingAgent#premain
BootstrapInstrumentBoost#inject
BootstrapInstrumentBoost#prepareJREInstrumentation

skywalking premain大致步骤:

1针对每个插件的所有拦截器,使用bytebuddy重新定义一个template并将拦截器全路径赋值给新定义template的field,以kv方式存储拦截器名和新定义template的字节码; 
2类加载器加载上述字节码; 
3使用bytebuddy为方法添加拦截器,如实例方法methodA用拦截器interceptorA。

后续拦截器行为即为template的行为:

target拦截器实例化,仅实例化一次
事前:target拦截器的beforeMethod
事中:原始逻辑
事后:target拦截器的afterMethod

7. sleuth vs skywalking-trace

sleuth使用扩展点,skywalking-trace使用agent并加强字节码。

两者同样支持多种中间件,但是sleuth需要添加某些中间件maven依赖。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部