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

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

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

Tracer.handleSpanCloseMethod介绍

[英]Handles the implementation of Span#close() (for java.io.Closeable) for spans to allow them to be used in try-with-resources statements. We do the work here instead of in Span#close() itself since we have more visibility into whether a span is valid to be closed or not given the current state of the span stack.

  • If the given span is already completed ( Span#isCompleted() returns true) then an error will be logged and nothing will be done.

  • If the span is the current span ( #getCurrentSpan() equals the given span), then #completeRequestSpan() or #completeSubSpan() will be called, whichever is appropriate.

  • If the span is not the current span ( #getCurrentSpan() does not equal the given span), then this may or may not be an error depending on whether the given span is managed by Tracer or not.

  • If the span is managed by us (i.e. it is contained in the span stack somewhere even though it's not the current span) then this is a wingtips usage error - the span should not be completed yet - and an error will be logged and the given span will be completed and logged to the "invalid span logger".

    • Otherwise the span is not managed by us, and since there may be valid use cases for manually managing spans we must assume the call was intentional. No error will be logged, and the span will be completed and logged to the "valid span logger".
    • In either case, the current span stack and MDC info will be left untouched if the given span is not the current span.

NOTE: This is intentionally package-scoped. Only Span#close() should ever call this method.
[中]处理Span的Span#close()(对于java.io.Closeable)的实现,以允许在try with resources语句中使用它们。我们在这里做这项工作,而不是在Span#close()本身,因为考虑到Span堆栈的当前状态,我们可以更清楚地了解Span是否有效地被关闭。
*如果给定的跨度已经完成(span#isCompleted()返回true),则会记录一个错误,不会执行任何操作。
如果span是当前span(#getCurrentSpan()等于给定span),则将调用#completeRequestSpan()或#completeSubSpan(),以适当的为准。
如果范围是不是
当前范围(#getCurrentSpan()不等于给定范围),则这可能是错误,也可能不是错误,这取决于给定范围是否由跟踪程序管理。
*如果翼展由我们管理(即,尽管不是当前翼展,但它仍包含在翼展堆栈的某个位置),则这是翼尖使用错误-翼展尚未完成-将记录一个错误,给定的翼展将完成并记录到“无效翼展记录器”。
*否则,span不是由我们管理的,因为手动管理span可能存在有效的用例,所以我们必须假设调用是故意的。不会记录任何错误,量程将完成并记录到“有效量程记录器”。
*在这两种情况下,如果给定的跨度不是当前跨度,则当前跨度堆栈和MDC信息将保持不变。
注意:这是故意限定包范围的。只有Span#close()应该调用此方法。

代码示例

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

Tracer.getInstance().handleSpanCloseMethod(this);

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

@Test
public void handleSpanCloseMethod_completes_the_span_as_expected_overall_request_span() {
  // given
  Span overallSpan = Tracer.getInstance().startRequestWithRootSpan("root");
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(overallSpan);
  assertThat(overallSpan.isCompleted()).isFalse();
  // when
  Tracer.getInstance().handleSpanCloseMethod(overallSpan);
  // then
  assertThat(overallSpan.isCompleted()).isTrue();
  assertThat(Tracer.getInstance().getCurrentSpan()).isNull();
}

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

@Test
public void handleSpanCloseMethod_completes_the_span_as_expected_subspan() {
  // given
  Span parentSpan = Tracer.getInstance().startRequestWithRootSpan("root");
  Span subspan = Tracer.getInstance().startSubSpan("subspan", SpanPurpose.LOCAL_ONLY);
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(subspan);
  assertThat(subspan.isCompleted()).isFalse();
  // when
  Tracer.getInstance().handleSpanCloseMethod(subspan);
  // then
  assertThat(subspan.isCompleted()).isTrue();
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(parentSpan);
}

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

@Test
public void handleSpanCloseMethod_does_nothing_if_span_is_already_completed() {
  // given
  Span rootSpan = Tracer.getInstance().startRequestWithRootSpan("root");
  Span subspan = Tracer.getInstance().startSubSpan("subspan", SpanPurpose.LOCAL_ONLY);
  Tracer.getInstance().completeSubSpan();
  assertThat(subspan.isCompleted()).isTrue();
  assertThat(rootSpan.isCompleted()).isFalse();
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(rootSpan);
  assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isEqualTo(singletonList(rootSpan));
  // when
  Tracer.getInstance().handleSpanCloseMethod(subspan);
  // then
  assertThat(rootSpan.isCompleted()).isFalse();
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(rootSpan);
  assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isEqualTo(singletonList(rootSpan));
}

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

Tracer.getInstance().handleSpanCloseMethod(parentSpan);
Tracer.getInstance().handleSpanCloseMethod(subspan1);

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

Tracer.getInstance().handleSpanCloseMethod(invalidSpan);

相关文章