本文整理汇总了Java中com.nike.wingtips.Tracer.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java Tracer.getInstance方法的具体用法?Java Tracer.getInstance怎么用?Java Tracer.getInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.nike.wingtips.Tracer
的用法示例。
在下文中一共展示了Tracer.getInstance方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: intercept
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution
) throws IOException {
Tracer tracer = Tracer.getInstance();
Span spanAroundCall = null;
try {
if (surroundCallsWithSubspan) {
// Will start a new trace if necessary, or a subspan if a trace is already in progress.
spanAroundCall = tracer.startSpanInCurrentContext(getSubspanSpanName(request), SpanPurpose.CLIENT);
}
HttpRequest wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request);
propagateTracingHeaders(wrapperRequest, tracer.getCurrentSpan());
return execution.execute(wrapperRequest, body);
}
finally {
if (spanAroundCall != null) {
// Span.close() contains the logic we want - if the spanAroundCall was an overall span (new trace)
// then tracer.completeRequestSpan() will be called, otherwise it's a subspan and
// tracer.completeSubSpan() will be called.
spanAroundCall.close();
}
}
}
示例3: 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);
}
示例4: process
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
Tracer tracer = Tracer.getInstance();
if (surroundCallsWithSubspan) {
// Will start a new trace if necessary, or a subspan if a trace is already in progress.
Span spanToClose = tracer.startSpanInCurrentContext(getSubspanSpanName(request), Span.SpanPurpose.CLIENT);
// Add the subspan to the HttpContext so that the response interceptor can retrieve and close it.
context.setAttribute(SPAN_TO_CLOSE_HTTP_CONTEXT_ATTR_KEY, spanToClose);
}
propagateTracingHeaders(request, tracer.getCurrentSpan());
}
示例5: decorateProtocolExec
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
@Override
protected ClientExecChain decorateProtocolExec(final ClientExecChain protocolExec) {
final boolean myHttpClientSurroundCallsWithSubspan = surroundCallsWithSubspan;
return new ClientExecChain() {
@Override
@SuppressWarnings("TryFinallyCanBeTryWithResources")
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
HttpClientContext clientContext,
HttpExecutionAware execAware) throws IOException, HttpException {
Tracer tracer = Tracer.getInstance();
Span spanAroundCall = null;
if (myHttpClientSurroundCallsWithSubspan) {
// Will start a new trace if necessary, or a subspan if a trace is already in progress.
spanAroundCall = tracer.startSpanInCurrentContext(getSubspanSpanName(request), SpanPurpose.CLIENT);
}
try {
propagateTracingHeaders(request, tracer.getCurrentSpan());
return protocolExec.execute(route, request, clientContext, execAware);
}
finally {
if (spanAroundCall != null) {
// Span.close() contains the logic we want - if the spanAroundCall was an overall span (new
// trace) then tracer.completeRequestSpan() will be called, otherwise it's a subspan and
// tracer.completeSubSpan() will be called.
spanAroundCall.close();
}
}
}
};
}
示例6: intercept
import com.nike.wingtips.Tracer; //导入方法依赖的package包/类
@Override
@SuppressWarnings("deprecation")
public ListenableFuture<ClientHttpResponse> intercept(
HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution
) throws IOException {
HttpRequest wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request);
Tracer tracer = Tracer.getInstance();
// Handle subspan stuff if desired.
SpanAroundAsyncCallFinisher subspanFinisher = null;
TracingState originalThreadInfo = null;
if (surroundCallsWithSubspan) {
originalThreadInfo = TracingState.getCurrentThreadTracingState();
// This will start a new trace if necessary, or a subspan if a trace is already in progress.
tracer.startSpanInCurrentContext(getSubspanSpanName(request), Span.SpanPurpose.CLIENT);
// Create the callback that will complete the subspan when the request finishes.
subspanFinisher = new SpanAroundAsyncCallFinisher(TracingState.getCurrentThreadTracingState());
}
try {
// Whether we created a subspan or not we want to add the tracing headers with the current span's info.
propagateTracingHeaders(wrapperRequest, tracer.getCurrentSpan());
// Execute the request/interceptor chain, and add the callback to finish the subspan (if one exists).
ListenableFuture<ClientHttpResponse> result = execution.executeAsync(wrapperRequest, body);
if (subspanFinisher != null) {
result.addCallback(subspanFinisher);
}
return result;
}
catch(Throwable t) {
// Something went wrong in the execution.executeAsync(...) call so we complete the subspan now (if one
// exists)
if (subspanFinisher != null) {
subspanFinisher.finishCallSpan();
}
throw t;
}
finally {
// Reset back to the original tracing state that was on this thread when this method began (only relevant
// if surroundCallsWithSubspan is true).
if (surroundCallsWithSubspan) {
unlinkTracingFromCurrentThread(originalThreadInfo);
}
}
}
示例7: 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);
}
}