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

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

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

Tracer.getCurrentSpan介绍

[英]The Span set as the "current" one for this thread.

NOTE: If #currentSpanStackThreadLocal is null or empty for this thread it will try to reconstitute the Span from the logging org.slf4j.MDC. This is useful in some situations, for example async request processing where the thread changes but the MDC is smart enough to transfer the span anyway. In any case as a caller you don't have to care - you'll just get the Span appropriate for the caller, or null if one hasn't been set up yet.
[中]为该线程设置为“当前”的跨度。
注意:如果此线程的#currentSpanStackThreadLocal为null或为空,它将尝试从日志记录组织中重建跨度。slf4j。争取民主变革运动。这在某些情况下很有用,例如异步请求处理,其中线程发生变化,但MDC足够智能,可以传输跨度。在任何情况下,作为一个调用者,你都不必在意——你只需要得到适合调用者的范围,如果还没有设置,则为null。

代码示例

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

private HttpEntity getHttpEntityWithUserIdHeader() {
  HttpHeaders headers = new HttpHeaders();
  String userId = Tracer.getInstance().getCurrentSpan().getUserId();
  if (userIdHeaderKeys == null || userIdHeaderKeys.isEmpty() || userId == null) {
    return new HttpEntity(headers);
  }
  headers.set(userIdHeaderKeys.get(0), userId);
  return new HttpEntity(headers);
}

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

@Test
public void linkTracingAndMdcToCurrentThread_should_clear_mdc_if_state_is_available_but_state_mdc_info_is_null() {
  // given
  MDC.put("foo", "bar");
  Tracer.getInstance().startRequestWithRootSpan("blahtrace");
  assertThat(MDC.getCopyOfContextMap().isEmpty(), is(false));
  assertThat(Tracer.getInstance().getCurrentSpan(), notNullValue());
  // when
  handler.linkTracingAndMdcToCurrentThread(ctxMock);
  // then
  assertThat(MDC.getCopyOfContextMap().isEmpty(), is(true));
  assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), nullValue());
}

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

private HttpEntity getHttpEntityWithUserIdHeader() {
  HttpHeaders headers = new HttpHeaders();
  String userId = Tracer.getInstance().getCurrentSpan().getUserId();
  if (userIdHeaderKeys.isEmpty() || userId == null) {
    return new HttpEntity(headers);
  }
  headers.set(userIdHeaderKeys.get(0), userId);
  return new HttpEntity(headers);
}

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

public static ChannelHandlerContextMocks mockChannelHandlerContextWithTraceInfo(String userId) {
  if (Tracer.getInstance().getCurrentSpan() == null) {
    Tracer.getInstance().startRequestWithRootSpan("mockChannelHandlerContext", userId);
  }
  ChannelHandlerContextMocks channelHandlerMocks = mockChannelHandlerContext();
  when(channelHandlerMocks.mockHttpProcessingState.getLoggerMdcContextMap()).thenReturn(MDC.getCopyOfContextMap());
  when(channelHandlerMocks.mockHttpProcessingState.getDistributedTraceStack()).thenReturn(Tracer.getInstance().getCurrentSpanStackCopy());
  return channelHandlerMocks;
}

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

@GetMapping(path = SPAN_INFO_CALL_PATH)
@SuppressWarnings("unused")
public EndpointSpanInfoDto getSpanInfoCall(HttpServletRequest request) {
  logger.info("Span info endpoint hit. Sleeping...");
  sleepThread(SLEEP_TIME_MILLIS);
  return new EndpointSpanInfoDto(request, Tracer.getInstance().getCurrentSpan(), userIdHeaderKeys);
}

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

@GetMapping(path = SPAN_INFO_CALL_PATH)
@SuppressWarnings("unused")
public EndpointSpanInfoDto getSpanInfoCall(HttpServletRequest request) {
  logger.info("Span info endpoint hit. Sleeping...");
  sleepThread(SLEEP_TIME_MILLIS);
  return new EndpointSpanInfoDto(request, Tracer.getInstance().getCurrentSpan(), userIdHeaderKeys);
}

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

protected String extractDistributedTraceId(RequestInfo requestInfo, ChannelHandlerContext ctx) {
  String traceId = (requestInfo == null) ? null : requestInfo.getHeaders().get(TraceHeaders.TRACE_ID);
  if (traceId == null) {
    traceId = supplierWithTracingAndMdc(
      () -> {
        Span currentSpanFromTracer = Tracer.getInstance().getCurrentSpan();
        if (currentSpanFromTracer != null)
          return currentSpanFromTracer.getTraceId();
        return null;
      },
      ctx
    ).get();
  }
  return traceId;
}

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

protected String extractDistributedTraceId(RequestInfo requestInfo, ChannelHandlerContext ctx) {
  String traceId = (requestInfo == null) ? null : requestInfo.getHeaders().get(TraceHeaders.TRACE_ID);
  if (traceId == null) {
    traceId = supplierWithTracingAndMdc(
      () -> {
        Span currentSpanFromTracer = Tracer.getInstance().getCurrentSpan();
        if (currentSpanFromTracer != null)
          return currentSpanFromTracer.getTraceId();
        return null;
      },
      ctx
    ).get();
  }
  return traceId;
}

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

protected CloseableHttpResponse propagateHeadersAndExecute(
    HttpRoute route,
    HttpRequestWrapper request,
    HttpClientContext clientContext,
    HttpExecutionAware execAware
  ) throws IOException, HttpException {
    propagateTracingHeaders(request, Tracer.getInstance().getCurrentSpan());
    return protocolExec.execute(route, request, clientContext, execAware);
  }
};

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

@Override
  public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                     HttpClientContext clientContext,
                     HttpExecutionAware execAware) throws IOException, HttpException {
    capturedSpan = Tracer.getInstance().getCurrentSpan();
    
    doReturn(statusLineMock).when(response).getStatusLine();
    
    if (exceptionToThrow != null) {
      throw exceptionToThrow;
    }
    // Return a graceful 500 from the response
    doReturn(500).when(statusLineMock).getStatusCode();
    return response;
  }
}

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

/**
 * Calls {@link WingtipsSpringUtil#propagateTracingHeaders(HttpMessage, Span)} to propagate the current span's
 * tracing state on the given request's headers, then returns
 * {@link ClientHttpRequestExecution#execute(HttpRequest, byte[])} to execute the request.
 *
 * @return The result of calling {@link ClientHttpRequestExecution#execute(HttpRequest, byte[])}.
 */
protected ClientHttpResponse propagateTracingHeadersAndExecuteRequest(
  HttpRequestWrapperWithModifiableHeaders wrapperRequest, byte[] body, ClientHttpRequestExecution execution
) throws IOException {
  propagateTracingHeaders(wrapperRequest, Tracer.getInstance().getCurrentSpan());
  
  // Execute the request/interceptor chain.
  return execution.execute(wrapperRequest, body);
}

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

/**
 * Calls {@link WingtipsSpringUtil#propagateTracingHeaders(HttpMessage, Span)} to propagate the current span's
 * tracing state on the given request's headers, then returns
 * {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])} to execute the request.
 *
 * @return The result of calling {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])}.
 */
protected ListenableFuture<ClientHttpResponse> propagateTracingHeadersAndExecute(
  HttpRequestWrapperWithModifiableHeaders wrapperRequest, byte[] body, AsyncClientHttpRequestExecution execution
) throws IOException {
  propagateTracingHeaders(wrapperRequest, Tracer.getInstance().getCurrentSpan());
  // Execute the request/interceptor chain.
  return execution.executeAsync(wrapperRequest, body);
}

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

@Test
public void getCurrentSpan_should_return_current_span() throws Exception {
  // given
  Tracer tracer = Tracer.getInstance();
  tracer.startRequestWithRootSpan("test-span");
  // when
  Span span = tracer.getCurrentSpan();
  // then
  assertThat(span).isNotNull();
  assertThat(span.getSpanName()).isEqualTo("test-span");
}

代码示例来源: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 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 completeSubSpan_should_do_nothing_if_the_span_stack_is_null() {
  // given: a null span stack
  assertThat(Tracer.getInstance().getCurrentSpan()).isNull();
  assertThat(getSpanStackSize()).isEqualTo(0);
  assertThat(getSpanStackFromTracer()).isNull();
  // when: completeSubSpan() is called
  Tracer.getInstance().completeSubSpan();
  // then: nothing should be done because the stack is null
  assertThat(Tracer.getInstance().getCurrentSpan()).isNull();
  assertThat(getSpanStackSize()).isEqualTo(0);
  assertThat(getSpanStackFromTracer()).isNull();
}

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

@Override
  public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    capturedSpan = Tracer.getInstance().getCurrentSpan();
    captureSpanCopyAtTimeOfDoFilter = Span.newBuilder(capturedSpan).build();
  }
}

代码示例来源: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

@Test
public void startRequestWithRootSpan_wipes_out_any_existing_spans_on_the_stack() {
  // given: Tracer already has some Spans on the stack
  Tracer.getInstance().startRequestWithRootSpan("span1");
  Tracer.getInstance().startSubSpan("span2", SpanPurpose.LOCAL_ONLY);
  assertThat(getSpanStackSize()).isEqualTo(2);
  // when: Tracer.startRequestWithRootSpan(String) is called to start a span without a parent
  Tracer.getInstance().startRequestWithRootSpan("noparent");
  // then: a new span is started for it, and the other spans on the stack are removed
  assertThat(getSpanStackSize()).isEqualTo(1);
  Span span = Tracer.getInstance().getCurrentSpan();
  assertThat(span).isNotNull();
  assertThat(span.getSpanName()).isEqualTo("noparent");
}

代码示例来源: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);
}

相关文章