本文整理了Java中com.nike.wingtips.Tracer.registerWithThread()
方法的一些代码示例,展示了Tracer.registerWithThread()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tracer.registerWithThread()
方法的具体详情如下:
包路径:com.nike.wingtips.Tracer
类名称:Tracer
方法名:registerWithThread
[英]"Registers" a COPY of the given span stack with this thread (sets up the ThreadLocal span stack with a copy of the given argument) and sets up the MDC appropriately based on what you pass in. This is used in asynchronous projects/frameworks where multiple in-progress requests might be handled by the same thread before any of the requests can finish (e.g. Netty, where the worker I/O threads might process a portion of request A, flip over to request B to do some work, then go back to request A, etc). When the worker thread was about to stop working on the original request and move to a new one you would call #unregisterFromThread()and store the result with some kind of context state that follows the original request, and when processing switches back to the original request you would call this method to set the thread's span stack and MDC info back to the way it was so you could continue where you left off.
In a properly functioning project/framework this thread would not have any span information in its stack when this method was called, so if there are any spans already on this thread then this method will mark them as invalid, complete them, and log an appropriate error message before registering the stack passed into this method.
NOTE: A copy of the given stack is registered so that changes to the stack you pass in don't affect the stack stored here. This prevents a host of subtle annoying bugs.
WARNING: This method should NOT be called if you're in an environment where a single thread is guaranteed to process a request from start to finish without jumping to a different request in the middle. In that case just use the normal start and complete span methods and ignore this method.
[中]使用此线程“注册”给定span堆栈的副本(使用给定参数的副本设置ThreadLocal span堆栈),并根据传入的内容适当设置MDC。这在异步项目/框架中使用,其中多个进行中的请求可能在任何请求完成之前由同一个线程处理(例如,Netty,工作I/O线程可能处理请求a的一部分,翻转到请求B来做一些工作,然后返回到请求a,等等)。当工作线程即将停止处理原始请求并移动到新请求时,您将调用#unregisterFromThread(),并使用原始请求之后的某种上下文状态存储结果,当处理切换回原始请求时,你会调用这个方法,将线程的跨度堆栈和MDC信息设置回原来的状态,这样你就可以继续之前的工作了。
在正常运行的项目/框架中,调用此方法时,此线程的堆栈中不会有任何跨度信息,因此,如果此线程上已有任何跨度,则此方法会将它们标记为无效,完成它们,并在注册传递到此方法中的堆栈之前记录适当的错误消息。
注意:将注册给定堆栈的副本,以便对传入的堆栈所做的更改不会影响存储在此处的堆栈。这可以防止出现许多微妙的恼人的错误。
警告:如果您在一个线程保证从一开始到结束处理一个请求而不跳转到中间的不同请求的环境中,则不应该调用此方法。在这种情况下,只需使用正常的开始和完成跨度方法,而忽略此方法。
代码示例来源:origin: Nike-Inc/wingtips
/**
* Calls {@link Tracer#unregisterFromThread()} and {@link MDC#clear()} to reset this thread's tracing and
* MDC state to be completely clean, then (optionally) resets the span stack and MDC info to the arguments
* provided. If the span stack argument is null then the span stack will *not* be reset, and similarly if the MDC
* info is null then the MDC info will *not* be reset. So if both are null then when this method finishes the trace
* stack and MDC will be left in a blank state.
*
* @deprecated Please move to the Java 8 version of this class and method ({@code AsyncWingtipsHelper} or the static
* {@code AsyncWingtipsHelperStatic}) whenever possible.
*/
@Deprecated
public static void unlinkTracingFromCurrentThread(Deque<Span> spanStackToResetFor,
Map<String, String> mdcContextMapToResetFor) {
Tracer.getInstance().unregisterFromThread();
MDC.clear();
if (mdcContextMapToResetFor != null)
MDC.setContextMap(mdcContextMapToResetFor);
if (spanStackToResetFor != null)
Tracer.getInstance().registerWithThread(spanStackToResetFor);
}
代码示例来源:origin: com.nike.riposte/riposte-core
/**
* Calls {@link Tracer#unregisterFromThread()} and {@link org.slf4j.MDC#clear()} to reset this thread's tracing and
* MDC state to be completely clean, then (optionally) resets the trace stack and MDC info to the arguments
* provided. If the trace stack argument is null then the trace stack will *not* be reset, and similarly if the MDC
* info is null then the MDC info will *not* be reset. So if both are null then when this method finishes the trace
* stack and MDC will be left in a blank state.
*/
public static void unlinkTracingAndMdcFromCurrentThread(Deque<Span> distributedTraceStackToResetFor,
Map<String, String> mdcContextMapToResetFor) {
Tracer.getInstance().unregisterFromThread();
MDC.clear();
if (mdcContextMapToResetFor != null)
MDC.setContextMap(mdcContextMapToResetFor);
if (distributedTraceStackToResetFor != null)
Tracer.getInstance().registerWithThread(distributedTraceStackToResetFor);
}
代码示例来源:origin: Nike-Inc/riposte
/**
* Calls {@link Tracer#unregisterFromThread()} and {@link org.slf4j.MDC#clear()} to reset this thread's tracing and
* MDC state to be completely clean, then (optionally) resets the trace stack and MDC info to the arguments
* provided. If the trace stack argument is null then the trace stack will *not* be reset, and similarly if the MDC
* info is null then the MDC info will *not* be reset. So if both are null then when this method finishes the trace
* stack and MDC will be left in a blank state.
*/
public static void unlinkTracingAndMdcFromCurrentThread(Deque<Span> distributedTraceStackToResetFor,
Map<String, String> mdcContextMapToResetFor) {
Tracer.getInstance().unregisterFromThread();
MDC.clear();
if (mdcContextMapToResetFor != null)
MDC.setContextMap(mdcContextMapToResetFor);
if (distributedTraceStackToResetFor != null)
Tracer.getInstance().registerWithThread(distributedTraceStackToResetFor);
}
代码示例来源:origin: Nike-Inc/riposte
@Test
public void doChannelRead_does_not_propagate_errors_if_unexpected_exceptions_occur_during_endpoint_timing_annotations() {
// given
Span spanMock = mock(Span.class);
Tracer.getInstance().registerWithThread(new ArrayDeque<>(Collections.singleton(spanMock)));
assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanMock);
TracingState tracingStateForTest = TracingState.getCurrentThreadTracingState();
doReturn(tracingStateForTest.getLeft()).when(stateMock).getDistributedTraceStack();
doReturn(tracingStateForTest.getRight()).when(stateMock).getLoggerMdcContextMap();
doReturn(true).when(taggingStrategySpy).shouldAddEndpointStartAnnotation();
doThrow(new RuntimeException("intentional exception")).when(taggingStrategySpy).endpointStartAnnotationName();
doReturn(true).when(taggingStrategySpy).shouldAddEndpointFinishAnnotation();
doThrow(new RuntimeException("intentional exception")).when(taggingStrategySpy).endpointFinishAnnotationName();
ResponseInfo<?> responseInfoMock = mock(ResponseInfo.class);
// when
handlerSpy.doChannelRead(ctxMock, msg);
futureThatWillBeAttachedToSpy.complete(responseInfoMock);
Object result = futureThatWillBeAttachedToSpy.join();
// then
verify(taggingStrategySpy).endpointStartAnnotationName();
verify(taggingStrategySpy).endpointFinishAnnotationName();
assertThat(result).isSameAs(responseInfoMock);
// We verified that the methods were called that would have thrown exceptions, and nothing propagated. So
// we're good.
}
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_do_nothing_if_copy_of_same_stack_is_passed_in() {
// given
Tracer tracer = Tracer.getInstance();
tracer.startRequestWithRootSpan("foo");
Span subspan = tracer.startSubSpan("bar", SpanPurpose.LOCAL_ONLY);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
// when
Deque<Span> spanStack = getSpanStackThreadLocal().get();
tracer.registerWithThread(new LinkedList<>(spanStack));
// then
assertThat(getSpanStackThreadLocal().get()).isEqualTo(spanStack);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
}
代码示例来源:origin: Nike-Inc/riposte
@Test
public void doChannelInactive_does_not_try_to_recomplete_span_if_already_completed() throws Exception {
// given
Span span = setupTracingForChannelInactive(false);
Deque<Span> deque = new LinkedList<>();
deque.add(span);
Tracer.getInstance().registerWithThread(deque);
Tracer.getInstance().completeRequestSpan();
Assertions.assertThat(span.isCompleted()).isTrue();
long durationNanosBefore = span.getDurationNanos();
// when
PipelineContinuationBehavior result = handler.doChannelInactive(ctxMock);
// then
Assertions.assertThat(span.isCompleted()).isTrue(); // no change
Assertions.assertThat(span.getDurationNanos()).isEqualTo(durationNanosBefore);
verify(requestInfoMock).releaseAllResources();
verify(proxyRouterStateMock).cancelRequestStreaming(any(), any());
verify(proxyRouterStateMock).cancelDownstreamRequest(any());
Assertions.assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_do_nothing_if_same_stack_is_passed_in() {
// given
Tracer tracer = Tracer.getInstance();
tracer.startRequestWithRootSpan("foo");
Span subspan = tracer.startSubSpan("bar", SpanPurpose.LOCAL_ONLY);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
// when
Deque<Span> spanStack = getSpanStackThreadLocal().get();
tracer.registerWithThread(spanStack);
// then
assertThat(getSpanStackThreadLocal().get()).isEqualTo(spanStack);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
}
代码示例来源:origin: Nike-Inc/wingtips
MDC.setContextMap(mdcContextMapToLink);
Tracer.getInstance().registerWithThread(spanStackToLink);
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_reset_everything_if_passed_empty_instance() {
// given
Tracer tracer = Tracer.getInstance();
tracer.startRequestWithRootSpan("foo");
Span subspan = tracer.startSubSpan("bar", SpanPurpose.LOCAL_ONLY);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
// when
Deque<Span> emptyStack = new LinkedList<>();
tracer.registerWithThread(emptyStack);
// then
assertThat(getSpanStackThreadLocal().get()).isEqualTo(emptyStack);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isNull();
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isNull();
}
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_reset_everything_if_passed_null() {
// given
Tracer tracer = Tracer.getInstance();
tracer.startRequestWithRootSpan("foo");
Span subspan = tracer.startSubSpan("bar", SpanPurpose.LOCAL_ONLY);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
// when
tracer.registerWithThread(null);
// then
assertThat(getSpanStackThreadLocal().get()).isNull();
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isNull();
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isNull();
}
代码示例来源:origin: Nike-Inc/wingtips
tracer.registerWithThread(new LinkedList<Span>());
mock(Span.class), mock(Span.class), mock(Span.class)
));
tracer.registerWithThread(nonEmptyStack);
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_override_existing_stuff() {
// given
Tracer tracer = Tracer.getInstance();
Span existingSpan = tracer.startRequestWithRootSpan("old");
Deque<Span> newSpanStack = new LinkedList<>();
Span parentSpan = Span.newBuilder("foo", SpanPurpose.LOCAL_ONLY).build();
Span subspan = Span.newBuilder("bar", SpanPurpose.LOCAL_ONLY).build();
newSpanStack.push(parentSpan);
newSpanStack.push(subspan);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(existingSpan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(existingSpan.toJSON());
// when
tracer.registerWithThread(newSpanStack);
// then
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
Deque<Span> spanStack = getSpanStackThreadLocal().get();
assertThat(spanStack).isEqualTo(newSpanStack);
}
代码示例来源:origin: Nike-Inc/riposte
Tracer.getInstance().registerWithThread(new ArrayDeque<>(Collections.singleton(spanMock)));
assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanMock);
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_work_as_advertised_if_existing_stack_is_empty() {
// given
getSpanStackThreadLocal().set(new LinkedList<Span>());
Tracer tracer = Tracer.getInstance();
Deque<Span> newSpanStack = new LinkedList<>();
Span parentSpan = Span.newBuilder("foo", SpanPurpose.LOCAL_ONLY).build();
Span subspan = Span.newBuilder("bar", SpanPurpose.LOCAL_ONLY).build();
newSpanStack.push(parentSpan);
newSpanStack.push(subspan);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isNull();
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isNull();
// when
tracer.registerWithThread(newSpanStack);
// then
// our stack was registered, so subspan should be current
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
assertThat(tracer.getCurrentSpan()).isEqualTo(subspan);
// a *copy* of the stack we passed in should have been registered, and modifying the original stack should not affect Tracer's stack
Deque<Span> spanStack = getSpanStackThreadLocal().get();
assertThat(Tracer.getInstance().containsSameSpansInSameOrder(spanStack, newSpanStack)).isTrue();
assertThat(spanStack).isNotSameAs(newSpanStack);
newSpanStack.push(subspan.generateChildSpan("subsub", SpanPurpose.LOCAL_ONLY));
assertThat(newSpanStack).hasSize(3);
assertThat(spanStack).hasSize(2);
}
代码示例来源:origin: Nike-Inc/wingtips
@Test
public void registerWithThread_should_work_as_advertised() {
// given
Tracer tracer = Tracer.getInstance();
Deque<Span> newSpanStack = new LinkedList<>();
Span parentSpan = Span.newBuilder("foo", SpanPurpose.LOCAL_ONLY).build();
Span subspan = Span.newBuilder("bar", SpanPurpose.LOCAL_ONLY).build();
newSpanStack.push(parentSpan);
newSpanStack.push(subspan);
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isNull();
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isNull();
// when
tracer.registerWithThread(newSpanStack);
// then
// our stack was registered, so subspan should be current
assertThat(MDC.get(Tracer.TRACE_ID_MDC_KEY)).isEqualTo(subspan.getTraceId());
assertThat(MDC.get(Tracer.SPAN_JSON_MDC_KEY)).isEqualTo(subspan.toJSON());
assertThat(tracer.getCurrentSpan()).isEqualTo(subspan);
// a *copy* of the stack we passed in should have been registered, and modifying the original stack should not affect Tracer's stack
Deque<Span> spanStack = getSpanStackThreadLocal().get();
assertThat(Tracer.getInstance().containsSameSpansInSameOrder(spanStack, newSpanStack)).isTrue();
assertThat(spanStack).isNotSameAs(newSpanStack);
newSpanStack.push(subspan.generateChildSpan("subsub", SpanPurpose.LOCAL_ONLY));
assertThat(newSpanStack).hasSize(3);
assertThat(spanStack).hasSize(2);
}
代码示例来源:origin: com.nike.riposte/riposte-core
MDC.setContextMap(mdcContextMapToLink);
Tracer.getInstance().registerWithThread(distributedTraceStackToLink);
代码示例来源:origin: Nike-Inc/riposte
MDC.setContextMap(mdcContextMapToLink);
Tracer.getInstance().registerWithThread(distributedTraceStackToLink);
代码示例来源:origin: Nike-Inc/riposte
MDC.setContextMap(mdcContextMap);
Tracer.getInstance().registerWithThread(distributedTraceStack);
代码示例来源:origin: Nike-Inc/wingtips
tracer.registerWithThread(reqAStack);
tracer.registerWithThread(reqBStack);
内容来源于网络,如有侵权,请联系作者删除!