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

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

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

Span.close介绍

[英]Handles the implementation of Closeable#close() for spans to allow them to be used in try-with-resources statements or other libraries that work with Closeable objects.

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

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

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

  • If this span is managed by Tracer (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 - this span should not be completed yet - and an error will be logged and this span will be completed and logged to the "invalid span logger".

    • Otherwise this 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 this span will be completed and logged to the "valid span logger".
    • In either case, Tracer's current span stack and MDC info will be left untouched if this span is not the current span.
      [中]处理跨度的Closeable#close()的实现,以允许在try with resources语句或其他处理Closeable对象的库中使用它们。
      *如果这个跨度已经完成(#isCompleted()返回true),那么将记录一个错误,并且不会执行任何操作。
      如果此范围是当前范围(Tracer#getCurrentSpan()等于给定范围),则将调用Tracer#completeRequestSpan()或Tracer#completeSubSpan(),以适当的为准。
      如果此范围不是
      当前范围(Tracer#getCurrentSpan()不等于此范围),则这可能是错误,也可能不是错误,具体取决于此范围是否由Tracer管理。
      *如果该跨度由跟踪器管理(即,它包含在跨度堆栈的某个位置,即使它不是当前跨度),则这是翼尖使用错误-该跨度尚不应完成-将记录一个错误,该跨度将完成并记录到“无效跨度记录器”。
      *否则,我们不会管理这个跨度,而且由于手动管理跨度可能存在有效的用例,我们必须假设调用是有意的。不会记录任何错误,该量程将被完成并记录到“有效量程记录器”中。
      *无论哪种情况,如果该跨度不是当前跨度,跟踪器的当前跨度堆栈和MDC信息都将保持不变。

代码示例

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

@Override
public void process(HttpResponse response, HttpContext context) {
  // See if there's a subspan passed to us from the request interceptor.
  Span spanToClose = (Span) context.getAttribute(SPAN_TO_CLOSE_HTTP_CONTEXT_ATTR_KEY);
  if (spanToClose != null) {
    // There was a subspan. Finalize and close it.
    try {
      // Handle response/error tagging and final span name.
      //      The request should be found in the context attributes - try to extract it from there.
      //      We have no access to any error, so we pass null for the error arg.
      HttpRequest request = null;
      Object requestRawObj = context.getAttribute(HttpCoreContext.HTTP_REQUEST);
      if (requestRawObj instanceof HttpRequest) {
        request = (HttpRequest) requestRawObj;
      }
      tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName(
        spanToClose, request, response, null, tagAndNamingAdapter
      );
    } finally {
      // Span.close() contains the logic we want - if the spanToClose was an overall span (new trace)
      //      then tracer.completeRequestSpan() will be called, otherwise it's a subspan and
      //      tracer.completeSubSpan() will be called.
      spanToClose.close();
    }
  }
}

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

@Override
  public void run() {
    Span span = Tracer.getInstance().getCurrentSpan();
    //noinspection TryFinallyCanBeTryWithResources
    try {
      // Add the tags from the response.
      tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName(
        span, request, response, error, tagAndNamingAdapter
      );
    }
    finally {
      // 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.
      span.close();
    }
  }
},

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

@Override
  public void run() {
    Span span = Tracer.getInstance().getCurrentSpan();
    //noinspection TryFinallyCanBeTryWithResources
    try {
      // Add the tags from the response.
      tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName(
        span, request, response, error, tagAndNamingAdapter
      );
    }
    finally {
      // 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.
      span.close();
    }
  }
},

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

@Test
public void process_response_closes_span_no_matter_what() {
  // given
  Span spanMock = mock(Span.class);
  httpContext = spy(httpContext);
  httpContext.setAttribute(SPAN_TO_CLOSE_HTTP_CONTEXT_ATTR_KEY, spanMock);
  RuntimeException expectedEx = new RuntimeException("boom");
  doThrow(expectedEx).when(httpContext).getAttribute(HttpCoreContext.HTTP_REQUEST);
  // when
  Throwable ex = catchThrowable(() -> interceptor.process(responseMock, httpContext));
  // then
  assertThat(ex).isSameAs(expectedEx);
  verify(httpContext).getAttribute(HttpCoreContext.HTTP_REQUEST);
  verify(spanMock).close();
}

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

spanAroundCall.close();

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

spanAroundCall.close();

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

);
verify(spanMock).close();

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

@Test
public void close_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
  overallSpan.close();
  // then
  assertThat(overallSpan.isCompleted()).isTrue();
  assertThat(Tracer.getInstance().getCurrentSpan()).isNull();
}

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

@Test
public void close_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
  subspan.close();
  // then
  assertThat(subspan.isCompleted()).isTrue();
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(parentSpan);
}

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

spanAroundCall.close();

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

parent.close();

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

spanAroundCall.close();

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

spanAroundCall.close();

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

@Test
public void close_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
  subspan.close();
  // then
  assertThat(rootSpan.isCompleted()).isFalse();
  assertThat(Tracer.getInstance().getCurrentSpan()).isSameAs(rootSpan);
  assertThat(Tracer.getInstance().getCurrentSpanStackCopy()).isEqualTo(singletonList(rootSpan));
}

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

parent.close();

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

parent.close();

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

parent.close();

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

parentSpan.close();
subspan1.close();

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

invalidSpan.close();

相关文章