protected void sendSpans(List<zipkin.Span> spanList) { try { sendSpans(Codec.JSON.writeSpans(spanList)); } catch (IOException e) { Set<String> affectedTraceIds = new HashSet<>(spanList.size()); for (zipkin.Span span : spanList) { affectedTraceIds.add(String.valueOf(span.traceId)); } logger.error("An error occurred attempting to post Zipkin spans to the Zipkin server. affected_trace_ids={}, exception_cause=\"{}\"", affectedTraceIds.toString(), e.toString()); } }
@Override public byte[] encode(Span span) { return Codec.JSON.writeSpan(span); } };
@Override public List<DependencyLink> apply(SearchResponse response) { if (response.getHits() == null) return ImmutableList.of(); ImmutableList.Builder<DependencyLink> unmerged = ImmutableList.builder(); for (SearchHit hit : response.getHits()) { DependencyLink link = Codec.JSON.readDependencyLink(hit.getSourceRef().toBytes()); unmerged.add(link); } return unmerged.build(); } }
sentSpans.add(zipkinSpan(random.nextLong(), UUID.randomUUID().toString())); long expectedUncompressedPayloadSize = Codec.JSON.writeSpans(sentSpans).length; byte[] deserializableBytes = (compressPayload) ? unGzip(receivedPayloadBytes) : receivedPayloadBytes; List<zipkin.Span> receivedSpans = Codec.JSON.readSpans(deserializableBytes); assertThat(receivedSpans).isEqualTo(sentSpans); } finally {
@Override public Iterable<Span> call(byte[] bytes) throws Exception { logInitializer().run(); if (bytes.length == 0) return Collections.emptyList(); try { if (bytes[0] == '[') { return Codec.JSON.readSpans(bytes); } else { if (bytes[0] == 12 /* TType.STRUCT */) { return Codec.THRIFT.readSpans(bytes); } else { // historical kafka encoding of single thrift span per message return Collections.singletonList(Codec.THRIFT.readSpan(bytes)); } } } catch (RuntimeException e) { log.warn("unable to decode spans", e); return Collections.emptyList(); } } }
@Override public List<Span> apply(SearchResponse response) { if (response.getHits().totalHits() == 0) { return null; } ImmutableList.Builder<Span> trace = ImmutableList.builder(); for (SearchHit hit : response.getHits()) { trace.add(Codec.JSON.readSpan(hit.getSourceRef().toBytes())); } return trace.build(); } });
/** * Conditionally decodes depending on whether the input bytes are encoded as a single span or a * list. */ static List<Span> fromBytes(byte[] bytes) { // In TBinaryProtocol encoding, the first byte is the TType, in a range 0-16 // .. If the first byte isn't in that range, it isn't a thrift. // // When byte(0) == '[' (91), assume it is a list of json-encoded spans // // When byte(0) <= 16, assume it is a TBinaryProtocol-encoded thrift // .. When serializing a Span (Struct), the first byte will be the type of a field // .. When serializing a List[ThriftSpan], the first byte is the member type, TType.STRUCT(12) // .. As ThriftSpan has no STRUCT fields: so, if the first byte is TType.STRUCT(12), it is a list. if (bytes[0] == '[') { return Codec.JSON.readSpans(bytes); } else if (bytes[0] == 12 /* TType.STRUCT */) { return Codec.THRIFT.readSpans(bytes); } else { return Collections.singletonList(Codec.THRIFT.readSpan(bytes)); } } }
/** * Conditionally decodes depending on whether the input bytes are encoded as a single span or a * list. */ static List<Span> fromBytes(byte[] bytes) { // In TBinaryProtocol encoding, the first byte is the TType, in a range 0-16 // .. If the first byte isn't in that range, it isn't a thrift. // // When byte(0) == '[' (91), assume it is a list of json-encoded spans // // When byte(0) <= 16, assume it is a TBinaryProtocol-encoded thrift // .. When serializing a Span (Struct), the first byte will be the type of a field // .. When serializing a List[ThriftSpan], the first byte is the member type, TType.STRUCT(12) // .. As ThriftSpan has no STRUCT fields: so, if the first byte is TType.STRUCT(12), it is a list. if (bytes[0] == '[') { return Codec.JSON.readSpans(bytes); } else if (bytes[0] == 12 /* TType.STRUCT */) { return Codec.THRIFT.readSpans(bytes); } else { return Collections.singletonList(Codec.THRIFT.readSpan(bytes)); } } }
protected void sendSpans(List<zipkin.Span> spanList) { try { sendSpans(Codec.JSON.writeSpans(spanList)); } catch (IOException e) { Set<String> affectedTraceIds = new HashSet<>(spanList.size()); for (zipkin.Span span : spanList) { affectedTraceIds.add(String.valueOf(span.traceId)); } logger.error("An error occurred attempting to post Zipkin spans to the Zipkin server. affected_trace_ids={}, exception_cause=\"{}\"", affectedTraceIds.toString(), e.toString()); } }
@Test public void sendSpans_with_span_list_delegates_to_sendSpans_with_byte_array() throws IOException { // given List<zipkin.Span> zipkinSpans = new ArrayList<>(); for (int i = 0; i < 10; i++) { zipkinSpans.add(zipkinSpan(random.nextLong(), UUID.randomUUID().toString())); } byte[] expectedBytesPayload = Codec.JSON.writeSpans(zipkinSpans); // when implSpy.sendSpans(zipkinSpans); // then verify(implSpy).sendSpans(expectedBytesPayload); }