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

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

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

Span.complete介绍

[英]Indicates that this Span is completed/finished/finalized and sets #getDurationNanos() to be System#nanoTime() minus #getSpanStartTimeNanos(). After this is called then #isCompleted() will return true and #getDurationNanos() will return the value calculated here. An IllegalStateException will be thrown if this method is called after the span has already been completed.

NOTE: This is intentionally package scoped to make sure completions and logging/span output logic happens centrally through Tracer.
[中]指示此跨度已完成/完成/最终确定,并将#getDurationNanos()设置为系统#nanoTime()减去#getSpanStartTimeNanos()。调用此函数后,#isCompleted()将返回true,#getDurationNanos()将返回此处计算的值。如果在跨度完成后调用此方法,则将引发IllegalStateException。
注意:这是故意限定包范围的,以确保完成和日志记录/span输出逻辑通过Tracer集中发生。

代码示例

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

/**
   * Calls {@link Span#complete()} on the given span.
   */
  public static void completeSpan(Span span) {
    span.complete();
  }
}

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

/**
 * Calls {@link Span#complete()} to complete the span and logs it (but only if the span's {@link Span#isSampleable()} returns true). If the span is valid then it will
 * be logged to {@link #validSpanLogger}, and if it is invalid then it will be logged to {@link #invalidSpanLogger}.
 *
 * @param span The span to complete and log
 * @param containsIncorrectTimingInfo Pass in true if you know the given span contains incorrect timing information (e.g. a child sub-span that wasn't completed normally
 *                                    when it was supposed to have been completed), pass in false if the span's timing info is good. This affects how the span is logged.
 */
protected void completeAndLogSpan(Span span, boolean containsIncorrectTimingInfo) {
  // Complete the span.
  if (span.isCompleted()) {
    classLogger.error(
      "WINGTIPS USAGE ERROR - An attempt was made to complete a span that was already completed. This call will be ignored. "
      + "wingtips_usage_error=true, already_completed_span=true, trace_id={}, span_id={}",
      span.getTraceId(), span.getSpanId(), new Exception("Stack trace for debugging purposes")
    );
    return;
  }
  else
    span.complete();
  // Log the span if it was sampleable.
  if (span.isSampleable()) {
    String infoTag = containsIncorrectTimingInfo ? "[INCORRECT_TIMING] " : "";
    Logger loggerToUse = containsIncorrectTimingInfo ? invalidSpanLogger : validSpanLogger;
    loggerToUse.info("{}[DISTRIBUTED_TRACING] {}", infoTag, serializeSpanToDesiredStringRepresentation(span));
  }
  // Notify listeners.
  notifySpanCompleted(span);
}

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

@Test(expected = IllegalStateException.class)
public void complete_should_throw_IllegalStateException_if_span_is_already_completed() {
  // given
  Span validSpan = Span.generateRootSpanForNewTrace(spanName, spanPurpose).build();
  validSpan.complete();
  assertThat(validSpan.isCompleted()).isTrue();
  // expect
  validSpan.complete();
  fail("Expected IllegalStateException but no exception was thrown");
}

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

@Test
public void complete_method_should_complete_the_span_with_correct_duration() throws InterruptedException {
  for (int i = 0; i < 10; i++) {
    // given
    Span validSpan = Span.generateRootSpanForNewTrace(spanName, spanPurpose).build();
    assertThat(validSpan.isCompleted()).isFalse();
    // when
    Thread.sleep((long) (Math.random() * 10));
    long beforeCompleteNanoTime = System.nanoTime();
    validSpan.complete();
    long afterCompleteNanoTime = System.nanoTime();
    // then
    assertThat(validSpan.isCompleted()).isTrue();
    long lowerBoundDuration = beforeCompleteNanoTime - validSpan.getSpanStartTimeNanos();
    long upperBoundDuration = afterCompleteNanoTime - validSpan.getSpanStartTimeNanos();
    assertThat(validSpan.getDurationNanos()).isBetween(lowerBoundDuration, upperBoundDuration);
  }
}

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

@Test
public void spanLifecycleListener_spanCompleted_is_not_called_when_request_span_was_completed_already() {
  // given
  SpanLifecycleListener listener1 = mock(SpanLifecycleListener.class);
  SpanLifecycleListener listener2 = mock(SpanLifecycleListener.class);
  Tracer tracer = Tracer.getInstance();
  tracer.addSpanLifecycleListener(listener1);
  tracer.addSpanLifecycleListener(listener2);
  Span span = tracer.startRequestWithRootSpan("newspan");
  span.complete();
  verify(listener1).spanStarted(span);
  verify(listener1, times(0)).spanCompleted(span);
  verify(listener2).spanStarted(span);
  verify(listener2, times(0)).spanCompleted(span);
  // when
  tracer.completeRequestSpan();
  // then
  verify(listener1, never()).spanCompleted(span);
  verify(listener2, never()).spanCompleted(span);
}

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

@Test
public void spanLifecycleListener_spanCompleted_is_not_called_if_subspan_was_already_completed() {
  // given
  SpanLifecycleListener listener1 = mock(SpanLifecycleListener.class);
  SpanLifecycleListener listener2 = mock(SpanLifecycleListener.class);
  Tracer tracer = Tracer.getInstance();
  tracer.addSpanLifecycleListener(listener1);
  tracer.addSpanLifecycleListener(listener2);
  tracer.startRequestWithRootSpan("newspan");
  Span subspan = tracer.startSubSpan("subspan", SpanPurpose.LOCAL_ONLY);
  subspan.complete();
  verify(listener1).spanStarted(subspan);
  verify(listener1, times(0)).spanCompleted(subspan);
  verify(listener2).spanStarted(subspan);
  verify(listener2, times(0)).spanCompleted(subspan);
  // when
  tracer.completeSubSpan();
  // then
  verify(listener1, never()).spanCompleted(subspan);
  verify(listener2, never()).spanCompleted(subspan);
}

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

@Test
public void getDuration_should_be_null_until_span_is_completed() {
  // given
  Span validSpan = Span.generateRootSpanForNewTrace(spanName, spanPurpose).build();
  assertThat(validSpan.getDurationNanos()).isNull();
  // when
  validSpan.complete();
  // then
  assertThat(validSpan.getDurationNanos()).isNotNull();
}

相关文章