SpringCloud 使用显式父级创建Span
2023-12-01 16:04 更新
您可能要开始一个新的跨度并提供该跨度的显式父项。假定范围的父级在一个线程中,而您想在另一个线程中开始一个新的范围。在Brave中,每当您调用nextSpan()
时,它都会参照当前范围的跨度创建一个跨度。您可以将范围放入范围中,然后调用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(); } }
创建这样的跨度后,必须完成它。否则,不会报告该错误(例如,报告到Zipkin)。
以上内容是否对您有帮助:
更多建议: