SpringCloud 傳播額外的字段

2023-12-01 15:43 更新

有時(shí)您需要傳播額外的字段,例如請求ID或備用跟蹤上下文。例如,如果您處于Cloud Foundry環(huán)境中,則可能要傳遞請求ID,如以下示例所示:

// when you initialize the builder, define the extra field you want to propagate
Tracing.newBuilder().propagationFactory(
  ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-vcap-request-id")
);

// later, you can tag that request ID or use it in log correlation
requestId = ExtraFieldPropagation.get("x-vcap-request-id");

您可能還需要傳播未使用的跟蹤上下文。例如,您可能處于Amazon Web服務(wù)環(huán)境中,但沒有向X-Ray報(bào)告數(shù)據(jù)。為了確保X射線可以正確共存,請傳遞其跟蹤標(biāo)頭,如以下示例所示:

tracingBuilder.propagationFactory(
  ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "x-amzn-trace-id")
);

在Spring Cloud Sleuth中,跟蹤構(gòu)建器Tracing.newBuilder()的所有元素都定義為beans。因此,如果您要傳遞自定義PropagationFactory,則足以創(chuàng)建該類型的bean,我們將在Tracing bean中進(jìn)行設(shè)置。

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號