use of com.nike.wingtips.Span.TimestampedAnnotation in project riposte by Nike-Inc.
the class DTraceStartHandlerTest method startTrace_creates_new_trace_if_no_parent_available_and_sets_expected_span_name_and_tags_and_annotations.
@DataProvider(value = { "true | true", "true | false", "false | true", "false | false" }, splitBy = "\\|")
@Test
public void startTrace_creates_new_trace_if_no_parent_available_and_sets_expected_span_name_and_tags_and_annotations(boolean addWireReceiveStartAnnotation, boolean stateIsNull) {
// given
assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
shouldAddWireReceiveStartAnnotation = addWireReceiveStartAnnotation;
if (stateIsNull) {
doReturn(null).when(stateAttributeMock).get();
}
DTraceStartHandler handlerSpy = spy(handler);
RiposteHandlerInternalUtil handlerUtilSpy = spy(new RiposteHandlerInternalUtil());
Whitebox.setInternalState(handlerSpy, "handlerUtils", handlerUtilSpy);
String fallbackSpanName = "fallback-span-name-" + UUID.randomUUID().toString();
doReturn(fallbackSpanName).when(handlerUtilSpy).determineFallbackOverallRequestSpanName(any());
String expectedSpanName = (stateIsNull) ? fallbackSpanName : initialSpanNameFromStrategy.get();
// when
long nanosBefore = System.nanoTime();
handlerSpy.startTrace(httpRequest, ctxMock);
long nanosAfter = System.nanoTime();
// then
Span span = Tracer.getInstance().getCurrentSpan();
assertThat(span.getTraceId(), notNullValue());
assertThat(span.getParentSpanId(), nullValue());
assertThat(span.getSpanId(), notNullValue());
assertThat(span.getSpanName(), is(expectedSpanName));
assertThat(span.getUserId(), nullValue());
if (!stateIsNull) {
strategyInitialSpanNameArgs.get().verifyArgs(requestInfoMock, tagAndNamingAdapterMock);
strategyRequestTaggingArgs.get().verifyArgs(span, requestInfoMock, tagAndNamingAdapterMock);
}
if (addWireReceiveStartAnnotation) {
long expectedMaxWireReceiveStartTimestamp = span.getSpanStartTimeEpochMicros() + TimeUnit.NANOSECONDS.toMicros(nanosAfter - nanosBefore);
TimestampedAnnotation wireReceiveStartAnnotation = findAnnotationInSpan(span, distributedTracingConfig.getServerSpanNamingAndTaggingStrategy().wireReceiveStartAnnotationName());
Assertions.assertThat(wireReceiveStartAnnotation.getTimestampEpochMicros()).isBetween(span.getSpanStartTimeEpochMicros(), expectedMaxWireReceiveStartTimestamp);
}
}
use of com.nike.wingtips.Span.TimestampedAnnotation in project riposte by Nike-Inc.
the class DTraceStartHandlerTest method startTrace_creates_trace_from_parent_if_available_and_sets_expected_span_name_and_tags_and_annotations.
@Test
public void startTrace_creates_trace_from_parent_if_available_and_sets_expected_span_name_and_tags_and_annotations() {
// given
String parentTraceId = UUID.randomUUID().toString();
String parentParentSpanId = UUID.randomUUID().toString();
String parentSpanId = UUID.randomUUID().toString();
String parentSpanName = UUID.randomUUID().toString();
String parentTraceEnabled = "true";
String parentUserId = UUID.randomUUID().toString();
httpRequest.headers().set(TraceHeaders.TRACE_ID, parentTraceId);
httpRequest.headers().set(TraceHeaders.PARENT_SPAN_ID, parentParentSpanId);
httpRequest.headers().set(TraceHeaders.SPAN_ID, parentSpanId);
httpRequest.headers().set(TraceHeaders.SPAN_NAME, parentSpanName);
httpRequest.headers().set(TraceHeaders.TRACE_SAMPLED, parentTraceEnabled);
httpRequest.headers().set(USER_ID_HEADER_KEY, parentUserId);
assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());
// when
long nanosBefore = System.nanoTime();
handler.startTrace(httpRequest, ctxMock);
long nanosAfter = System.nanoTime();
// then
Span span = Tracer.getInstance().getCurrentSpan();
assertThat(span.getTraceId(), is(parentTraceId));
assertThat(span.getParentSpanId(), is(parentSpanId));
assertThat(span.getSpanId(), notNullValue());
assertThat(span.getSpanId(), not(parentSpanId));
assertThat(span.getSpanName(), is(initialSpanNameFromStrategy.get()));
assertThat(span.getUserId(), is(parentUserId));
assertThat(span.isSampleable(), is(Boolean.valueOf(parentTraceEnabled)));
strategyInitialSpanNameArgs.get().verifyArgs(requestInfoMock, tagAndNamingAdapterMock);
strategyRequestTaggingArgs.get().verifyArgs(span, requestInfoMock, tagAndNamingAdapterMock);
long expectedMaxWireReceiveStartTimestamp = span.getSpanStartTimeEpochMicros() + TimeUnit.NANOSECONDS.toMicros(nanosAfter - nanosBefore);
TimestampedAnnotation wireReceiveStartAnnotation = findAnnotationInSpan(span, "wr.start");
Assertions.assertThat(wireReceiveStartAnnotation.getTimestampEpochMicros()).isBetween(span.getSpanStartTimeEpochMicros(), expectedMaxWireReceiveStartTimestamp);
}
use of com.nike.wingtips.Span.TimestampedAnnotation in project riposte by Nike-Inc.
the class VerifyDistributedTracingConfigBehaviorAndTagsComponentTest method verifySpanAnnotation.
private void verifySpanAnnotation(Span span, String annotationName, boolean annotationShouldExist, AtomicInteger expectedNumAnnotationsCounter, AtomicLong minAnnotationTimestamp) {
TimestampedAnnotation annotation = findSpanAnnotation(span, annotationName);
if (annotationShouldExist) {
assertThat(annotation).isNotNull();
expectedNumAnnotationsCounter.incrementAndGet();
assertThat(annotation.getTimestampEpochMicros()).isGreaterThanOrEqualTo(minAnnotationTimestamp.get());
minAnnotationTimestamp.set(annotation.getTimestampEpochMicros());
} else {
assertThat(annotation).isNull();
}
}
Aggregations