SpringCloud 使用顯式父級創(chuàng)建Span

2023-12-01 16:04 更新

您可能要開始一個新的跨度并提供該跨度的顯式父項(xiàng)。假定范圍的父級在一個線程中,而您想在另一個線程中開始一個新的范圍。在Brave中,每當(dāng)您調(diào)用nextSpan()時,它都會參照當(dāng)前范圍的跨度創(chuàng)建一個跨度。您可以將范圍放入范圍中,然后調(diào)用nextSpan(),如以下示例所示:

// let's assume that we're in a thread Y and we've received
// the `initialSpan` from thread X. `initialSpan` will be the parent
// of the `newSpan`
Span newSpan = null;
try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(initialSpan)) {
	newSpan = this.tracer.nextSpan().name("calculateCommission");
	// ...
	// You can tag a span
	newSpan.tag("commissionValue", commissionValue);
	// ...
	// You can log an event on a span
	newSpan.annotate("commissionCalculated");
}
finally {
	// Once done remember to finish the span. This will allow collecting
	// the span to send it to Zipkin. The tags and events set on the
	// newSpan will not be present on the parent
	if (newSpan != null) {
		newSpan.finish();
	}
}
創(chuàng)建這樣的跨度后,必須完成它。否則,不會報(bào)告該錯誤(例如,報(bào)告到Zipkin)。


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號