本文整理汇总了Java中com.nike.wingtips.Tracer.startRequestWithRootSpan方法的典型用法代码示例。如果您正苦于以下问题:Java Tracer.startRequestWithRootSpan方法的具体用法?Java Tracer.startRequestWithRootSpan怎么用?Java Tracer.startRequestWithRootSpan使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.nike.wingtips.Tracer
的用法示例。
在下文中一共展示了Tracer.startRequestWithRootSpan方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startTrace
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
protected void startTrace(HttpRequest request) {
Tracer tracer = Tracer.getInstance();
// Start the distributed trace.
RequestWithHeaders requestWrapper = new RequestWithHeadersNettyAdapter(request);
final Span parentSpan = HttpRequestTracingUtils.fromRequestWithHeaders(requestWrapper, userIdHeaderKeys);
if (parentSpan != null) {
logger.debug("Found Parent Span {}", parentSpan.toString());
tracer.startRequestWithChildSpan(parentSpan, getSpanName(request));
}
else {
Span newSpan = tracer.startRequestWithRootSpan(
getSpanName(request),
HttpRequestTracingUtils.getUserIdFromRequestWithHeaders(requestWrapper, userIdHeaderKeys)
);
logger.debug("Parent Span not found, starting a new trace with root span {}", newSpan);
}
}
示例2: getCurrentThreadTracingState_works_as_expected
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
@Test
public void getCurrentThreadTracingState_works_as_expected() {
// given
Tracer tracer = Tracer.getInstance();
tracer.startRequestWithRootSpan("request-" + UUID.randomUUID().toString());
Deque<Span> currentSpanStack = tracer.getCurrentSpanStackCopy();
Map<String, String> currentMdcInfo = MDC.getCopyOfContextMap();
assertThat(currentSpanStack.size()).isGreaterThanOrEqualTo(1);
assertThat(currentMdcInfo).isNotEmpty();
// when
TracingState currentTracingState = TracingState.getCurrentThreadTracingState();
// then
assertThat(currentTracingState.spanStack).isEqualTo(currentSpanStack);
assertThat(currentTracingState.mdcInfo).isEqualTo(currentMdcInfo);
}
示例3: doFilterInternal
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
/**
* Performs the distributed tracing work for each request's overall span. Guaranteed to only be called once per
* request.
*/
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// Surround the tracing filter logic with a try/finally that guarantees the original tracing and MDC info found
// on the current thread at the beginning of this method is restored to this thread before this method
// returns, even if the request ends up being an async request. Otherwise there's the possibility of
// incorrect tracing information sticking around on this thread and potentially polluting other requests.
TracingState originalThreadInfo = TracingState.getCurrentThreadTracingState();
try {
// See if there's trace info in the incoming request's headers. If so it becomes the parent trace.
Tracer tracer = Tracer.getInstance();
final Span parentSpan = HttpSpanFactory.fromHttpServletRequest(request, getUserIdHeaderKeys());
Span newSpan;
if (parentSpan != null) {
logger.debug("Found parent Span {}", parentSpan);
newSpan = tracer.startRequestWithChildSpan(parentSpan, HttpSpanFactory.getSpanName(request));
} else {
newSpan = tracer.startRequestWithRootSpan(
HttpSpanFactory.getSpanName(request),
HttpSpanFactory.getUserIdFromHttpServletRequest(request, getUserIdHeaderKeys())
);
logger.debug("Parent span not found, starting a new span {}", newSpan);
}
// Put the new span's trace info into the request attributes.
request.setAttribute(TraceHeaders.TRACE_SAMPLED, newSpan.isSampleable());
request.setAttribute(TraceHeaders.TRACE_ID, newSpan.getTraceId());
request.setAttribute(TraceHeaders.SPAN_ID, newSpan.getSpanId());
request.setAttribute(TraceHeaders.PARENT_SPAN_ID, newSpan.getParentSpanId());
request.setAttribute(TraceHeaders.SPAN_NAME, newSpan.getSpanName());
request.setAttribute(Span.class.getName(), newSpan);
// Make sure we set the trace ID on the response header now before the response is committed (if we wait
// until after the filter chain then the response might already be committed, silently preventing us
// from setting the response header)
response.setHeader(TraceHeaders.TRACE_ID, newSpan.getTraceId());
TracingState originalRequestTracingState = TracingState.getCurrentThreadTracingState();
try {
filterChain.doFilter(request, response);
} finally {
if (isAsyncRequest(request)) {
// Async, so we need to attach a listener to complete the original tracing state when the async
// servlet request finishes.
setupTracingCompletionWhenAsyncRequestCompletes(request, originalRequestTracingState);
}
else {
// Not async, so we need to complete the request span now.
tracer.completeRequestSpan();
}
}
}
finally {
//noinspection deprecation
unlinkTracingFromCurrentThread(originalThreadInfo);
}
}