如何在当前 Context 中设置 Span Context?
How to set Span Context in current Context?
在opentelemetery-api
版本0.8.0
中,我们曾经通过以下代码在当前Context
中设置一个新的SpanContext
:
TracingContextUtils.currentContextWith(DefaultSpan.create(newSpanCtx))
但是,在版本 0.13.1
中,- TracingContextUtils
和 DefaultSpan
都被删除了。那如何在当前的Context
中设置一个新的SpanContext
呢?
来自 opentelemetry-java version 0.10.0 release notes:
TracingContextUtils
and BaggageUtils
were removed from the public API. Instead, use the appropriate static methods on the Span
and Baggage
classes, or use methods on the Context
itself.
DefaultSpan
was removed from the public API. Instead, use Span.wrap(spanContext)
if you need a non-functional span that propagates the trace context.
您可以尝试类似的方法:
val newSpanCtx: SpanContext = null
val span: Span = Span.wrap(newSpanCtx)
Context.current().`with`(span).makeCurrent()
如何使用 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
}
这也是 quickstart 的说法。
在opentelemetery-api
版本0.8.0
中,我们曾经通过以下代码在当前Context
中设置一个新的SpanContext
:
TracingContextUtils.currentContextWith(DefaultSpan.create(newSpanCtx))
但是,在版本 0.13.1
中,- TracingContextUtils
和 DefaultSpan
都被删除了。那如何在当前的Context
中设置一个新的SpanContext
呢?
来自 opentelemetry-java version 0.10.0 release notes:
TracingContextUtils
andBaggageUtils
were removed from the public API. Instead, use the appropriate static methods on theSpan
andBaggage
classes, or use methods on theContext
itself.DefaultSpan
was removed from the public API. Instead, useSpan.wrap(spanContext)
if you need a non-functional span that propagates the trace context.
您可以尝试类似的方法:
val newSpanCtx: SpanContext = null
val span: Span = Span.wrap(newSpanCtx)
Context.current().`with`(span).makeCurrent()
如何使用 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
}
这也是 quickstart 的说法。