brave.Tracer.withSpanInScope()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(278)

本文整理了Java中brave.Tracer.withSpanInScope()方法的一些代码示例,展示了Tracer.withSpanInScope()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Tracer.withSpanInScope()方法的具体详情如下:
包路径:brave.Tracer
类名称:Tracer
方法名:withSpanInScope

Tracer.withSpanInScope介绍

[英]Makes the given span the "current span" and returns an object that exits that scope on close. Calls to #currentSpan() and #currentSpanCustomizer() will affect this span until the return value is closed.

The most convenient way to use this method is via the try-with-resources idiom. Ex.

// Assume a framework interceptor uses this method to set the inbound span as currentcatch (RuntimeException | Error e)  
span.error(e); 
throw e; 
} finally  
span.finish(); 
} 
// An unrelated framework interceptor can now lookup the correct parent for outbound requests 
Span parent = tracer.currentSpan() 
Span span = tracer.nextSpan().name("outbound").start(); // parent is implicitly looked up 
try (SpanInScope ws = tracer.withSpanInScope(span))  
return outboundRequest.invoke(); 
// note: try-with-resources closes the scope *before* the catch block 
} catch (RuntimeException | Error e)  
span.error(e); 
throw e; 
} finally  
span.finish(); 
} 
}

When tracing in-process commands, prefer #startScopedSpan(String) which scopes by default.

Note: While downstream code might affect the span, calling this method, and calling close on the result have no effect on the input. For example, calling close on the result does not finish the span. Not only is it safe to call close, you must call close to end the scope, or risk leaking resources associated with the scope.
[中]使给定范围成为“当前范围”,并在关闭时返回一个退出该范围的对象。对#currentSpan()和#currentSpan customizer()的调用将影响此跨度,直到返回值关闭。
使用此方法最方便的方法是使用try with resources习惯用法。前任。

// Assume a framework interceptor uses this method to set the inbound span as currentcatch (RuntimeException | Error e)  
span.error(e); 
throw e; 
} finally  
span.finish(); 
} 
// An unrelated framework interceptor can now lookup the correct parent for outbound requests 
Span parent = tracer.currentSpan() 
Span span = tracer.nextSpan().name("outbound").start(); // parent is implicitly looked up 
try (SpanInScope ws = tracer.withSpanInScope(span))  
return outboundRequest.invoke(); 
// note: try-with-resources closes the scope *before* the catch block 
} catch (RuntimeException | Error e)  
span.error(e); 
throw e; 
} finally  
span.finish(); 
} 
}

在跟踪进程内命令时,首选#startScopedSpan(字符串),默认情况下其作用域为。
注意:虽然下游代码可能会影响span,但调用此方法和对结果调用close对输入没有影响。例如,对结果调用close不会完成跨度。调用close不仅安全,还必须调用close来结束作用域,否则可能会泄漏与作用域相关的资源。

代码示例

代码示例来源:origin: openzipkin/brave

public Filter before() {
 return (request, response) -> {
  Span span = handler.handleReceive(extractor, request, request.raw());
  request.attribute(Tracer.SpanInScope.class.getName(), tracer.withSpanInScope(span));
 };
}

代码示例来源:origin: openzipkin/brave

@Override
public void filter(ClientRequestContext request) {
 Span span = handler.handleSend(injector, request.getHeaders(), request);
 request.setProperty(SpanInScope.class.getName(), tracer.withSpanInScope(span));
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

@Override
protected R getFallback() {
  Span span = this.span.get();
  try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) {
    span.tag(FALLBACK_METHOD_NAME_KEY, getFallbackMethodName());
    return doGetFallback();
  }
  finally {
    span.finish();
    this.span.set(null);
  }
}

代码示例来源:origin: openzipkin/brave

@Override public void onCancel() {
 SpanInScope scope = tracer.withSpanInScope(span);
 try { // retrolambda can't resolve this try/finally
  delegate().onCancel();
 } finally {
  scope.close();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void onComplete() {
 SpanInScope scope = tracer.withSpanInScope(span);
 try { // retrolambda can't resolve this try/finally
  delegate().onComplete();
 } finally {
  scope.close();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void onReady() {
  SpanInScope scope = tracer.withSpanInScope(span);
  try { // retrolambda can't resolve this try/finally
   delegate().onReady();
  } finally {
   scope.close();
  }
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void send(Message message) throws JMSException {
 Span span = createAndStartProducerSpan(null, message);
 SpanInScope ws = tracer.withSpanInScope(span); // animal-sniffer mistakes this for AutoCloseable
 try {
  delegate.send(message);
 } catch (RuntimeException | JMSException | Error e) {
  span.error(e);
  throw e;
 } finally {
  ws.close();
  span.finish();
 }
}

代码示例来源:origin: openzipkin/brave

@Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
 if (requestEvent.getType() == RequestEvent.Type.START) {
  Span span = serverHandler.handleReceive(extractor, requestEvent.getContainerRequest());
  return new TracingRequestEventListener(span, tracer.withSpanInScope(span));
 }
 return null;
}

代码示例来源:origin: openzipkin/brave

@Override public void onMessage(Message message) {
 Span listenerSpan = startMessageListenerSpan(message);
 try (SpanInScope ws = tracer.withSpanInScope(listenerSpan)) {
  delegate.onMessage(message);
 } catch (Throwable t) {
  listenerSpan.error(t);
  throw t;
 } finally {
  listenerSpan.finish();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void onException(JMSException exception) {
  Span span = tracer.currentSpan();
  if (span == null) {
   delegate.onException(exception);
   return;
  }
  try (SpanInScope ws = tracer.withSpanInScope(span)) {
   delegate.onException(exception);
  } finally {
   span.error(exception);
  }
 }
}

代码示例来源:origin: openzipkin/brave

/**
 * Returns the {@link Tracer#nextSpan(TraceContextOrSamplingFlags)} or null if {@link
 * #CURRENT_TRACER} and tracing isn't available.
 */
@Nullable public Span next(TraceContextOrSamplingFlags extracted) {
 Tracer tracer = tracer();
 if (tracer == null) return null;
 Span next = tracer.nextSpan(extracted);
 Object[] spanAndScope = {next, tracer.withSpanInScope(next)};
 getCurrentSpanInScopeStack().addFirst(spanAndScope);
 return next;
}

代码示例来源:origin: openzipkin/brave

@Override public void onMessage(ReqT message) {
 SpanInScope scope = tracer.withSpanInScope(span);
 try { // retrolambda can't resolve this try/finally
  parser.onMessageReceived(message, span.customizer());
  delegate().onMessage(message);
 } finally {
  scope.close();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void onMessage(RespT message) {
 SpanInScope scope = tracer.withSpanInScope(span);
 try { // retrolambda can't resolve this try/finally
  parser.onMessageReceived(message, span.customizer());
  delegate().onMessage(message);
 } finally {
  scope.close();
 }
}

代码示例来源:origin: openzipkin/brave

/**
 * Returns the {@link Tracer#nextSpan()} or null if {@link #CURRENT_TRACER} and tracing isn't
 * available.
 */
@Nullable public Span next() {
 Tracer tracer = tracer();
 if (tracer == null) return null;
 Span next = tracer.nextSpan();
 Object[] spanAndScope = {next, tracer.withSpanInScope(next)};
 getCurrentSpanInScopeStack().addFirst(spanAndScope);
 return next;
}

代码示例来源:origin: openzipkin/brave

@Override public void onClose(Status status, Metadata trailers) {
  SpanInScope scope = tracer.withSpanInScope(span);
  try {
   super.onClose(status, trailers);
   parser.onClose(status, trailers, span.customizer());
  } finally {
   scope.close();
   span.finish();
  }
 }
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  if (tracer().currentSpan() != null) {
    // clear any previous trace
    tracer().withSpanInScope(null);
  }
  String uri = exchange.getRequest().getPath().pathWithinApplication().value();
  if (log.isDebugEnabled()) {
    log.debug("Received a request to uri [" + uri + "]");
  }
  return new MonoWebFilterTrace(chain.filter(exchange), exchange, this);
}

代码示例来源:origin: openzipkin/brave

void newChildWithSpanInScope(Tracer tracer, TraceContext context) {
 Span span = tracer.newChild(context).name("encode").start();
 try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
  span.tag("foo", "bar");
  span.tag("baz", "qux");
 } finally {
  span.finish();
 }
}

代码示例来源:origin: openzipkin/brave

void joinWithSpanInScope(Tracer tracer, TraceContext context) {
 Span span = tracer.joinSpan(context).name("encode").start();
 try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
  span.tag("foo", "bar");
  span.tag("baz", "qux");
 } finally {
  span.finish();
 }
}

代码示例来源:origin: openzipkin/brave

@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request,
  byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
 Span span = handler.handleSend(injector, request.getHeaders(), request);
 try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
  ListenableFuture<ClientHttpResponse> result = execution.executeAsync(request, body);
  result.addCallback(new TraceListenableFutureCallback(span, handler));
  return result;
 } catch (IOException | RuntimeException | Error e) {
  handler.handleReceive(null, e, span);
  throw e;
 }
}

代码示例来源:origin: openzipkin/brave

void nextWithSpanInScope(Tracer tracer, TraceContextOrSamplingFlags extracted) {
 Span span = tracer.nextSpan(extracted).name("encode").start();
 try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
  span.tag("foo", "bar");
  span.tag("baz", "qux");
 } finally {
  span.finish();
 }
}

相关文章