SelectSpansAndAnnotations implements Function<DSLContext, List<Span>> { static void processAnnotationRecord(Record a, V1Span.Builder span, @Nullable Endpoint endpoint) { Integer type = a.getValue(ZIPKIN_ANNOTATIONS.A_TYPE); if (type == null) return; if (type == -1) { span.addAnnotation( a.getValue(ZIPKIN_ANNOTATIONS.A_TIMESTAMP), a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), endpoint); } else { switch (type) { case V1BinaryAnnotation.TYPE_STRING: span.addBinaryAnnotation( a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), new String(a.getValue(ZIPKIN_ANNOTATIONS.A_VALUE), UTF_8), endpoint); break; case V1BinaryAnnotation.TYPE_BOOLEAN: if (endpoint == null) break; String aKey = a.getValue(ZIPKIN_ANNOTATIONS.A_KEY); if (!aKey.equals("sa") && !aKey.equals("ca") && !aKey.equals("ma")) break; byte[] value = a.getValue(ZIPKIN_ANNOTATIONS.A_VALUE); if (value == null || value.length != 1 || value[0] != 1) break; span.addBinaryAnnotation(a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), endpoint); break; default: } } } SelectSpansAndAnnotations(Schema schema); @Override List<Span> apply(DSLContext context);  }
@Test public void processAnnotationRecord_nulls() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(null, null, null, null); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, null); assertThat(builder) .usingRecursiveComparison().isEqualTo(V1Span.newBuilder().traceId(1).id(1)); }
@Test public void processAnnotationRecord_annotation() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(-1, 0L, "foo", null); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, null); assertThat(builder.build().annotations().get(0)) .isEqualTo(V1Annotation.create(0L, "foo", null)); }
@Test public void processAnnotationRecord_tag() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(6, null, "foo", new byte[0]); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, null); assertThat(builder.build().binaryAnnotations().get(0)) .isEqualTo(V1BinaryAnnotation.createString("foo", "", null)); }
@Test public void processAnnotationRecord_address() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(0, null, "ca", new byte[] {1}); Endpoint ep = Endpoint.newBuilder().serviceName("foo").build(); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, ep); assertThat(builder.build().binaryAnnotations().get(0)) .isEqualTo(V1BinaryAnnotation.createAddress("ca", ep)); }
@Test public void processAnnotationRecord_address_skipMissingEndpoint() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(0, null, "ca", new byte[] {1}); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, null); assertThat(builder.build().binaryAnnotations()).isEmpty(); }
@Test public void processAnnotationRecord_address_skipWrongKey() { Record4<Integer, Long, String, byte[]> annotationRecord = annotationRecord(0, null, "sr", new byte[] {1}); Endpoint ep = Endpoint.newBuilder().serviceName("foo").build(); V1Span.Builder builder = V1Span.newBuilder().traceId(1).id(1); SelectSpansAndAnnotations.processAnnotationRecord(annotationRecord, builder, ep); assertThat(builder.build().binaryAnnotations()).isEmpty(); }