SpringCloud 傳播

2023-12-01 15:40 更新

需要進行傳播以確保源自同一根的活動被收集到同一條跡線中。最常見的傳播方法是通過將RPC請求發(fā)送到接收它的服務(wù)器來從客戶端復(fù)制跟蹤上下文。

例如,進行下游HTTP調(diào)用時,其跟蹤上下文被編碼為請求標頭,并與之一起發(fā)送,如下圖所示:

   Client Span                                                Server Span
┌──────────────────┐                                       ┌──────────────────┐
│                  │                                       │                  │
│   TraceContext   │           Http Request Headers        │   TraceContext   │
│ ┌──────────────┐ │          ┌───────────────────┐        │ ┌──────────────┐ │
│ │ TraceId      │ │          │ X─B3─TraceId      │        │ │ TraceId      │ │
│ │              │ │          │                   │        │ │              │ │
│ │ ParentSpanId │ │ Extract  │ X─B3─ParentSpanId │ Inject │ │ ParentSpanId │ │
│ │              ├─┼─────────>│                   ├────────┼>│              │ │
│ │ SpanId       │ │          │ X─B3─SpanId       │        │ │ SpanId       │ │
│ │              │ │          │                   │        │ │              │ │
│ │ Sampled      │ │          │ X─B3─Sampled      │        │ │ Sampled      │ │
│ └──────────────┘ │          └───────────────────┘        │ └──────────────┘ │
│                  │                                       │                  │
└──────────────────┘                                       └──────────────────┘

上面的名稱來自B3 Propagation,它內(nèi)置于Brave,并具有許多語言和框架的實現(xiàn)。

大多數(shù)用戶使用框架攔截器來自動化傳播。接下來的兩個示例顯示了這對于客戶端和服務(wù)器的工作方式。

以下示例顯示了客戶端傳播如何工作:

@Autowired Tracing tracing;

// configure a function that injects a trace context into a request
injector = tracing.propagation().injector(Request.Builder::addHeader);

// before a request is sent, add the current span's context to it
injector.inject(span.context(), request);

以下示例顯示了服務(wù)器端傳播的工作方式:

@Autowired Tracing tracing;
@Autowired Tracer tracer;

// configure a function that extracts the trace context from a request
extractor = tracing.propagation().extractor(Request::getHeader);

// when a server receives a request, it joins or starts a new trace
span = tracer.nextSpan(extractor.extract(request));


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號