/** * @param request The incoming request. * @return A new {@link Span} for the overall request. This inspects the incoming request's headers to determine * if it should continue an existing trace with a child span, or whether a brand new trace needs to be started. * {@link #getInitialSpanName(HttpServletRequest, HttpTagAndSpanNamingStrategy, HttpTagAndSpanNamingAdapter)} * is used to generate the initial span name. */ protected Span createNewSpanForRequest(HttpServletRequest request) { // See if there's trace info in the incoming request's headers. If so it becomes the parent trace. Tracer tracer = Tracer.getInstance(); final Span parentSpan = HttpSpanFactory.fromHttpServletRequest(request, getUserIdHeaderKeys()); Span newSpan; if (parentSpan != null) { logger.debug("Found parent Span {}", parentSpan); newSpan = tracer.startRequestWithChildSpan( parentSpan, getInitialSpanName(request, tagAndNamingStrategy, tagAndNamingAdapter) ); } else { newSpan = tracer.startRequestWithRootSpan( getInitialSpanName(request, tagAndNamingStrategy, tagAndNamingAdapter), HttpSpanFactory.getUserIdFromHttpServletRequest(request, getUserIdHeaderKeys()) ); logger.debug("Parent span not found, starting a new span {}", newSpan); } return newSpan; }
@Test(expected = IllegalArgumentException.class) public void startRequestWithChildSpan_throws_IllegalArgumentException_if_passed_null_parent() { // expect Tracer.getInstance().startRequestWithChildSpan(null, "somechildspan"); fail("Expected IllegalArgumentException but no exception was thrown"); }
@Test public void startRequestWithChildSpan_wipes_out_any_existing_spans_on_the_stack() { // given: Tracer already has some Spans on the stack, and we have a parent span we're going to use Tracer.getInstance().startRequestWithRootSpan("span1"); Tracer.getInstance().startSubSpan("span2", SpanPurpose.LOCAL_ONLY); assertThat(getSpanStackSize()).isEqualTo(2); Span newSpanParent = Span.generateRootSpanForNewTrace("parentspan", SpanPurpose.CLIENT).build(); // when: Tracer.startRequestWithChildSpan(Span, String) is called to start a span with a parent Tracer.getInstance().startRequestWithChildSpan(newSpanParent, "childspan"); // then: a new span is started that has the given parent, 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("childspan"); }
Tracer.getInstance().startRequestWithChildSpan(parentSpan, "childspan"); long afterNanoTime = System.nanoTime(); long afterEpochMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
Tracer.getInstance().startRequestWithChildSpan(parentSpan, "childspan"); long afterNanoTime = System.nanoTime(); long afterEpochMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
if (parentSpan != null) { logger.debug("Found Parent Span {}", parentSpan.toString()); newSpan = tracer.startRequestWithChildSpan( parentSpan, getFallbackSpanName(nettyRequest)
if (parentSpan != null) { logger.debug("Found Parent Span {}", parentSpan.toString()); newSpan = tracer.startRequestWithChildSpan( parentSpan, getFallbackSpanName(nettyRequest)