@Test public void fromJson_returns_null_if_startTimeEpochMicros_field_is_missing() { // given Span validSpan = createFilledOutSpan(true); String validJson = SpanParser.convertSpanToJSON(validSpan); String invalidJson = validJson.replace( String.format(",\"%s\":\"%s\"", SpanParser.START_TIME_EPOCH_MICROS_FIELD, String.valueOf(validSpan.getSpanStartTimeEpochMicros()) ), "" ); // when Span result = SpanParser.fromJSON(invalidJson); // then assertThat(result).isNull(); }
@Test public void fromKeyValueString_returns_null_if_startTimeEpochMicros_field_is_missing() { // given Span validSpan = createFilledOutSpan(true); String validKeyValStr = SpanParser.convertSpanToKeyValueFormat(validSpan); String invalidKeyValStr = validKeyValStr.replace( String.format(",%s=\"%s\"", SpanParser.START_TIME_EPOCH_MICROS_FIELD, String.valueOf(validSpan.getSpanStartTimeEpochMicros()) ), "" ); // when Span result = SpanParser.fromKeyValueString(invalidKeyValStr); // then assertThat(result).isNull(); }
@Test public void builder_build_ignores_passed_in_spanStartTimeNanos_if_spanStartTimeEpochMicros_is_null() { // given Span.Builder builder = Span .newBuilder("stuff", SpanPurpose.LOCAL_ONLY) .withSpanStartTimeNanos(42L) .withSpanStartTimeEpochMicros(null); // when long beforeNanos = System.nanoTime(); long beforeEpochMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()); Span span = builder.build(); long afterNanos = System.nanoTime(); long afterEpochMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()); // then assertThat(span.getSpanStartTimeNanos()).isBetween(beforeNanos, afterNanos); assertThat(span.getSpanStartTimeEpochMicros()).isBetween(beforeEpochMicros, afterEpochMicros); }
@Test public void public_constructor_works_as_expected_for_completed_span() { // when Span span = new Span( traceId, parentSpanId, spanId, spanName, sampleableForFullyCompleteSpan, userId, spanPurposeForFullyCompletedSpan, startTimeEpochMicrosForFullyCompleteSpan, startTimeNanosForFullyCompleteSpan, durationNanosForFullyCompletedSpan, tags, annotations ); // then assertThat(span.getTraceId()).isEqualTo(traceId); assertThat(span.getParentSpanId()).isEqualTo(parentSpanId); assertThat(span.getSpanId()).isEqualTo(spanId); assertThat(span.getSpanName()).isEqualTo(spanName); assertThat(span.isSampleable()).isEqualTo(sampleableForFullyCompleteSpan); assertThat(span.getUserId()).isEqualTo(userId); assertThat(span.getSpanStartTimeEpochMicros()).isEqualTo(startTimeEpochMicrosForFullyCompleteSpan); assertThat(span.getSpanStartTimeNanos()).isEqualTo(startTimeNanosForFullyCompleteSpan); assertThat(span.getSpanPurpose()).isEqualTo(spanPurposeForFullyCompletedSpan); assertThat(span.isCompleted()).isTrue(); assertThat(span.getDurationNanos()).isEqualTo(durationNanosForFullyCompletedSpan); assertThat(span.getTags()).isEqualTo(tags); assertThat(span.getTimestampedAnnotations()).isEqualTo(annotations); }
@Test public void public_constructor_works_as_expected_for_incomplete_span() { // when Span span = new Span( traceId, parentSpanId, spanId, spanName, sampleableForFullyCompleteSpan, userId, spanPurposeForFullyCompletedSpan, startTimeEpochMicrosForFullyCompleteSpan, startTimeNanosForFullyCompleteSpan, null, tags, annotations ); // then assertThat(span.getTraceId()).isEqualTo(traceId); assertThat(span.getParentSpanId()).isEqualTo(parentSpanId); assertThat(span.getSpanId()).isEqualTo(spanId); assertThat(span.getSpanName()).isEqualTo(spanName); assertThat(span.isSampleable()).isEqualTo(sampleableForFullyCompleteSpan); assertThat(span.getUserId()).isEqualTo(userId); assertThat(span.getSpanStartTimeEpochMicros()).isEqualTo(startTimeEpochMicrosForFullyCompleteSpan); assertThat(span.getSpanStartTimeNanos()).isEqualTo(startTimeNanosForFullyCompleteSpan); assertThat(span.getSpanPurpose()).isEqualTo(spanPurposeForFullyCompletedSpan); assertThat(span.isCompleted()).isFalse(); assertThat(span.getDurationNanos()).isNull(); assertThat(span.getTags()).isEqualTo(tags); assertThat(span.getTimestampedAnnotations()).isEqualTo(annotations); }
@Test public void equals_returns_false_and_hashCode_different_if_spanStartTimeEpochMicros_is_different() { // given Span fullSpan1 = createFilledOutSpan(true); Span fullSpan2 = createFilledOutSpan(true); Whitebox.setInternalState(fullSpan2, "spanStartTimeEpochMicros", fullSpan1.getSpanStartTimeEpochMicros() + 1); // expect assertThat(fullSpan1.equals(fullSpan2)).isFalse(); assertThat(fullSpan1.hashCode()).isNotEqualTo(fullSpan2.hashCode()); }
assertThat(zipkinSpan.name).isEqualTo(wingtipsSpan.getSpanName()); assertThat(zipkinSpan.parentId).isNull(); assertThat(zipkinSpan.timestamp).isEqualTo(wingtipsSpan.getSpanStartTimeEpochMicros()); assertThat(zipkinSpan.traceId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getTraceId())); assertThat(zipkinSpan.duration).isEqualTo(durationMicros);
1 + TimeUnit.NANOSECONDS.toMicros(nanoTimeAfterMethodCall - nanoTimeBeforeSpanCreation); long expectedMinTimestamp = span.getSpanStartTimeEpochMicros() + minPossibleOffsetMicros; long expectedMaxTimestamp = span.getSpanStartTimeEpochMicros() + maxPossibleOffsetMicros;
public static void verifySpanDeepEquals( Span spanToVerify, Span expectedSpan, boolean allowStartTimeNanosFudgeFactor ) { assertThat(spanToVerify.getSpanStartTimeEpochMicros()).isEqualTo(expectedSpan.getSpanStartTimeEpochMicros()); if (allowStartTimeNanosFudgeFactor) { assertThat(spanToVerify.getSpanStartTimeNanos()) .isCloseTo(expectedSpan.getSpanStartTimeNanos(), Offset.offset(TimeUnit.MILLISECONDS.toNanos(1))); } else { assertThat(spanToVerify.getSpanStartTimeNanos()).isEqualTo(expectedSpan.getSpanStartTimeNanos()); } assertThat(spanToVerify.isCompleted()).isEqualTo(expectedSpan.isCompleted()); assertThat(spanToVerify.getTraceId()).isEqualTo(expectedSpan.getTraceId()); assertThat(spanToVerify.getSpanId()).isEqualTo(expectedSpan.getSpanId()); assertThat(spanToVerify.getParentSpanId()).isEqualTo(expectedSpan.getParentSpanId()); assertThat(spanToVerify.getSpanName()).isEqualTo(expectedSpan.getSpanName()); assertThat(spanToVerify.isSampleable()).isEqualTo(expectedSpan.isSampleable()); assertThat(spanToVerify.getUserId()).isEqualTo(expectedSpan.getUserId()); assertThat(spanToVerify.getDurationNanos()).isEqualTo(expectedSpan.getDurationNanos()); assertThat(spanToVerify.getSpanPurpose()).isEqualTo(expectedSpan.getSpanPurpose()); assertThat(spanToVerify.getTags()).isEqualTo(expectedSpan.getTags()); assertThat(spanToVerify.getTimestampedAnnotations()).isEqualTo(expectedSpan.getTimestampedAnnotations()); }
assertThat(zipkinSpan.name).isEqualTo(wingtipsSpan.getSpanName()); assertThat(zipkinSpan.parentId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getParentSpanId())); assertThat(zipkinSpan.timestamp).isEqualTo(wingtipsSpan.getSpanStartTimeEpochMicros()); assertThat(zipkinSpan.traceId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getTraceId())); assertThat(zipkinSpan.duration).isEqualTo(durationMicros);
assertThat(span.getSpanName()).isEqualTo("noparent"); assertThat(span.getParentSpanId()).isNull(); assertThat(span.getSpanStartTimeEpochMicros()).isBetween(beforeEpochMicros, afterEpochMicros); assertThat(span.getSpanStartTimeNanos()).isBetween(beforeNanoTime, afterNanoTime); assertThat(span.isCompleted()).isFalse();
assertThat(result.getUserId()).isNull(); assertThat(result.getSpanPurpose()).isEqualTo(spanPurpose); assertThat(result.getSpanStartTimeEpochMicros()).isBetween(beforeCallEpochMicros, afterCallEpochMicros); assertThat(result.getSpanStartTimeNanos()).isBetween(beforeCallNanos, afterCallNanos); assertThat(result.getDurationNanos()).isNull();
assertThat(result.getUserId()).isNull(); assertThat(result.getSpanPurpose()).isEqualTo(spanPurpose); assertThat(result.getSpanStartTimeEpochMicros()).isBetween(beforeCallEpochMicros, afterCallEpochMicros); assertThat(result.getSpanStartTimeNanos()).isBetween(beforeCallNanos, afterCallNanos); assertThat(result.isCompleted()).isFalse();
assertThat(span.getSpanName()).isEqualTo("noparent"); assertThat(span.getParentSpanId()).isNull(); assertThat(span.getSpanStartTimeEpochMicros()).isBetween(beforeEpochMicros, afterEpochMicros); assertThat(span.getSpanStartTimeNanos()).isBetween(beforeNanoTime, afterNanoTime); assertThat(span.isCompleted()).isFalse();
assertThat(span.getSpanName()).isEqualTo("childspan"); assertThat(span.getParentSpanId()).isEqualTo(parentSpan.getSpanId()); assertThat(span.getSpanStartTimeEpochMicros()).isBetween(beforeEpochMicros, afterEpochMicros); assertThat(span.getSpanStartTimeNanos()).isBetween(beforeNanoTime, afterNanoTime); assertThat(span.isCompleted()).isFalse();
assertThat(span.getSpanName()).isEqualTo("childspan"); assertThat(span.getParentSpanId()).isEqualTo(parentSpan.getSpanId()); assertThat(span.getSpanStartTimeEpochMicros()).isBetween(beforeEpochMicros, afterEpochMicros); assertThat(span.getSpanStartTimeNanos()).isBetween(beforeNanoTime, afterNanoTime); assertThat(span.isCompleted()).isFalse();
assertThat(zipkinSpan.name()).isEqualTo(wingtipsSpan.getSpanName()); assertThat(zipkinSpan.parentId()).isNull(); assertThat(zipkinSpan.timestamp()).isEqualTo(wingtipsSpan.getSpanStartTimeEpochMicros()); assertThat(zipkinSpan.traceId()).isEqualTo(wingtipsSpan.getTraceId()); assertThat(zipkinSpan.duration()).isEqualTo(durationMicros);
.parentId(parentId) .traceId(traceId) .timestamp(wingtipsSpan.getSpanStartTimeEpochMicros()) .duration(durationMicros) .localEndpoint(zipkinEndpoint)
@Override public zipkin.Span convertWingtipsSpanToZipkinSpan(Span wingtipsSpan, Endpoint zipkinEndpoint, String localComponentNamespace) { String traceId = wingtipsSpan.getTraceId(); long startEpochMicros = wingtipsSpan.getSpanStartTimeEpochMicros(); long durationMicros = TimeUnit.NANOSECONDS.toMicros(wingtipsSpan.getDurationNanos()); zipkin.Span.Builder builder = createNewZipkinSpanBuilderWithSpanPurposeAnnotations(wingtipsSpan, startEpochMicros, durationMicros, zipkinEndpoint, localComponentNamespace) .id(nullSafeLong(wingtipsSpan.getSpanId())) .name(wingtipsSpan.getSpanName()) .parentId(nullSafeLong(wingtipsSpan.getParentSpanId())) .timestamp(startEpochMicros) .traceIdHigh(traceId.length() == 32 ? nullSafeLong(traceId, 0) : 0) .traceId(nullSafeLong(traceId)) .duration(durationMicros); addAllTagsToBuilderAsBinaryAnnotations(builder, wingtipsSpan.getTags(), zipkinEndpoint); addAllAnnotationsToBuilder(builder, wingtipsSpan.getTimestampedAnnotations(), zipkinEndpoint); return builder.build(); }
@Override public zipkin.Span convertWingtipsSpanToZipkinSpan(Span wingtipsSpan, Endpoint zipkinEndpoint, String localComponentNamespace) { String traceId = wingtipsSpan.getTraceId(); long startEpochMicros = wingtipsSpan.getSpanStartTimeEpochMicros(); long durationMicros = TimeUnit.NANOSECONDS.toMicros(wingtipsSpan.getDurationNanos()); zipkin.Span.Builder builder = createNewZipkinSpanBuilderWithSpanPurposeAnnotations(wingtipsSpan, startEpochMicros, durationMicros, zipkinEndpoint, localComponentNamespace) .id(nullSafeLong(wingtipsSpan.getSpanId())) .name(wingtipsSpan.getSpanName()) .parentId(nullSafeLong(wingtipsSpan.getParentSpanId())) .timestamp(startEpochMicros) .traceIdHigh(traceId.length() == 32 ? nullSafeLong(traceId, 0) : 0) .traceId(nullSafeLong(traceId)) .duration(durationMicros); addAllTagsToBuilderAsBinaryAnnotations(builder, wingtipsSpan.getTags(), zipkinEndpoint); addAllAnnotationsToBuilder(builder, wingtipsSpan.getTimestampedAnnotations(), zipkinEndpoint); return builder.build(); }