如何在当前上下文中设置跨上下文?

dldeef67  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(515)

opentelemetery-api 版本 0.8.0 ,我们曾经设置了一个新的 SpanContext 在当前 Context 通过以下代码:

TracingContextUtils.currentContextWith(DefaultSpan.create(newSpanCtx))

但是,在版本中 0.13.1 ,两者- TracingContextUtils 以及 DefaultSpan 已删除。那我怎么才能设置一个新的 SpanContext 在当前 Context ?

fnx2tebb

fnx2tebb1#

用这个怎么样 scope 打电话给 makeCurrent 方法?

Span span = tracer.spanBuilder("my span").startSpan();
// put the span into the current Context
try (Scope scope = span.makeCurrent()) {
    // your use case
    ...
} catch (Throwable t) {
    span.setStatus(StatusCode.ERROR, "Change it to your error message");
} finally {
    span.end(); // closing the scope does not end the span, this has to be done manually
}

这也是快速入门中所说的。

0dxa2lsx

0dxa2lsx2#

来自opentelemetry java版本0.10.0发行说明: TracingContextUtils 以及 BaggageUtils 已从公共api中删除。相反,在 Span 以及 Baggage 类,或在 Context 它自己。 DefaultSpan 已从公共api中删除。相反,使用 Span.wrap(spanContext) 如果需要传播跟踪上下文的非函数范围。
您可以尝试以下方法:

val newSpanCtx: SpanContext = null
val span: Span = Span.wrap(newSpanCtx)
Context.current().`with`(span).makeCurrent()

相关问题