Brave组件

本文深入探讨了Brave组件的核心部分,包括Tracing、Tracer、Span和TraceContext的概念及关系。Tracer用于创建和控制Span,而Span表示阶段,可以通过TraceContext进行存储和远程传输。文章还介绍了如何在不同中间件中传输Context,以及Sleuth的包装策略。读者将了解到如何在服务调用中继续前一个Span以及Propagation.Factory在HTTP头注入中的应用。
摘要由CSDN通过智能技术生成

核心组件

在这里插入图片描述
可以看出代码中围绕的是

  • Tracing
  • Tracer
  • PendingSpans
  • TraceContext
    什么是Tracing,Tracing可以看做是构建Tracer的一个构建器,他们是1:1的关系
    什么是Tracer,Trace是控制产生Span的接口,用于创建一个Span
    什么是Span,Span可以看做是一个阶段, 一个链路由多个阶段组成, 因此一个Span可以有父Span,以及生成子Span
    什么是TraceContext, TraceContext可以作为一个Span阶段的参数存储以及远程传输的参数存储,例如SpanId,ParentSpanId 等,
    和Span 是 1:1的关系
  1. 如何开启一个新的追踪阶段, 这是Tracer类的第一个方法,可以看出, 一个Span对应一个父类的Context和当前的Context,父可以为空

Q1. Span和TraceContext放在哪里
Q2. Span如何使用

 public Span newTrace() {
   
    return _toSpan(null, newRootContext(0));
  }
  
  Span _toSpan(@Nullable TraceContext parent, TraceContext context) {
   
  
    if (isNoop(context)) return new NoopSpan(context);
    // 这一步创建一个Span
    PendingSpan pendingSpan = pendingSpans.getOrCreate(parent, context, false);
    TraceContext pendingContext = pendingSpan.context();
    // A lost race of Tracer.toSpan(context) is the only known situation where "context" won't be
    // the same as pendingSpan.context()
    if (pendingContext != null) context = pendingContext;
    // 【最终返回的是RealSpan】
    return new RealSpan(context, pendingSpans, pendingSpan.state(), pendingSpan.clock());
  }

A1. Span放在pendingSpans的Map里, Key为Context,Value为Span, 因此 Span和Context是1:1的关系


  // 创建Span的逻辑
  // 可以看出一个Span (PendingSpan)包含了 一个 context, span, clock
  public PendingSpan getOrCreate(
    @Nullable TraceContext parent, TraceContext context, boolean start) {
   
    // 【这个是典型的先从缓存拿, 没有就创建, 可以知道】
    PendingSpan result = get(context);
    if (result != null) return result;
    // 【创建 Span】 
    MutableSpan span 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值