com.nike.wingtips.Tracer.startSpanInCurrentContext()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(5.3k)|赞(0)|评价(0)|浏览(133)

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

Tracer.startSpanInCurrentContext介绍

[英]This method is here for the (hopefully rare) cases where you want to start a new span but don't control the context where your code is executed (e.g. a third party library) - this method will start a new overall request span or a new subspan depending on the current thread's span stack state at the time this method is called. In other words this method is a shortcut for the following code:

Tracer tracer = Tracer.getInstance(); 
if (tracer.getCurrentSpanStackSize() == 0) { 
return tracer.startRequestWithRootSpan(spanName); 
} 
else { 
return tracer.startSubSpan(spanName, SpanPurpose.LOCAL_ONLY); 
}

This method assumes SpanPurpose#SERVER if the returned span is an overall request span, and SpanPurpose#LOCAL_ONLY if it's a subspan. If you know the span purpose already and this behavior is not what you want (i.e. when surrounding a HTTP client or database call and you want to use SpanPurpose#CLIENT) then use the #startSpanInCurrentContext(String,SpanPurpose) method instead.

WARNING: As stated above, this method is here to support libraries where they need to create a span for some work, but do not necessarily know how or where they are going to be used in a project, and therefore don't know whether tracing has been setup yet with an overall request span. Most of the time you will know where you are in relation to overall request span or subspan, and should use the appropriate Tracer.startRequestWith*(...) or Tracer.startSubSpan(...) methods directly as those methods spit out error logging when the span stack is not in the expected state (indicating a Wingtips usage error). Using this method everywhere can swallow critical error logging that would otherwise let you know Wingtips isn't being used correctly and that your distributed tracing info is potentially unreliable.

This method is the equivalent of swallowing exceptions when Wingtips isn't being used correctly - all diagnostic debugging information will be lost. This method should not be used simply because it is convenient!
[中]

代码示例

代码示例来源:origin: Nike-Inc/wingtips

@Override
public void process(HttpRequest request, HttpContext context) {
  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, tagAndNamingStrategy, tagAndNamingAdapter),
      Span.SpanPurpose.CLIENT
    );
    
    tagAndNamingStrategy.handleRequestTagging(spanToClose, request, tagAndNamingAdapter);
    // 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());
}

代码示例来源:origin: Nike-Inc/wingtips

@Test
public void startSpanInCurrentContext_works_as_expected_with_try_with_resources() {
  // given
  Span outerSpan;
  Span innerSpan;
  // when
  try (Span autocloseableOuterSpan = Tracer.getInstance().startSpanInCurrentContext("outerSpan")) {
    outerSpan = autocloseableOuterSpan;
    // then
    assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(outerSpan);
    assertThat(outerSpan.isCompleted()).isFalse();
    // and when
    try (Span autocloseableInnerSpan = Tracer.getInstance().startSpanInCurrentContext("innerSpan")) {
      innerSpan = autocloseableInnerSpan;
      // then
      assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(innerSpan);
      assertThat(innerSpan.isCompleted()).isFalse();
      assertThat(outerSpan.isCompleted()).isFalse();
      assertThat(innerSpan.getTraceId()).isEqualTo(outerSpan.getTraceId());
      assertThat(innerSpan.getParentSpanId()).isEqualTo(outerSpan.getSpanId());
    }
  }
  // then
  assertThat(innerSpan.isCompleted()).isTrue();
  assertThat(outerSpan.isCompleted()).isTrue();
}

代码示例来源:origin: Nike-Inc/wingtips

Span spanAroundCall = Tracer.getInstance().startSpanInCurrentContext(
  getSubspanSpanName(request, tagAndNamingStrategy, tagAndNamingAdapter), SpanPurpose.CLIENT
);

代码示例来源:origin: Nike-Inc/wingtips

) throws IOException {
  Span spanAroundCall = Tracer.getInstance().startSpanInCurrentContext(
    getSubspanSpanName(wrapperRequest, tagAndNamingStrategy, tagAndNamingAdapter),
    SpanPurpose.CLIENT

代码示例来源:origin: com.nike.wingtips/wingtips-spring

) throws IOException {
  Span spanAroundCall = Tracer.getInstance().startSpanInCurrentContext(
    getSubspanSpanName(wrapperRequest, tagAndNamingStrategy, tagAndNamingAdapter),
    SpanPurpose.CLIENT

代码示例来源:origin: com.nike.riposte/riposte-core

Span subspan = Tracer.getInstance().startSpanInCurrentContext(spanName, Span.SpanPurpose.CLIENT);

代码示例来源:origin: Nike-Inc/wingtips

Span newSpan = Tracer.getInstance().startSpanInCurrentContext(desiredNewSpanName);

代码示例来源:origin: Nike-Inc/riposte

Span subspan = Tracer.getInstance().startSpanInCurrentContext(spanName, Span.SpanPurpose.CLIENT);

代码示例来源:origin: Nike-Inc/wingtips

Span newSpan = Tracer.getInstance().startSpanInCurrentContext(desiredNewSpanName, spanPurpose);

代码示例来源:origin: Nike-Inc/wingtips

Span subspan = Tracer.getInstance().startSpanInCurrentContext(
  getSubspanSpanName(wrapperRequest, tagAndNamingStrategy, tagAndNamingAdapter),
  Span.SpanPurpose.CLIENT

代码示例来源:origin: com.nike.wingtips/wingtips-spring

Span subspan = Tracer.getInstance().startSpanInCurrentContext(
  getSubspanSpanName(wrapperRequest, tagAndNamingStrategy, tagAndNamingAdapter),
  Span.SpanPurpose.CLIENT

代码示例来源:origin: Nike-Inc/riposte

Tracer.getInstance().startSpanInCurrentContext(spanName, Span.SpanPurpose.CLIENT);

相关文章