@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(); } } }
/** * 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)); } } }
byte[] deserializableBytes = (compressPayload) ? unGzip(receivedPayloadBytes) : receivedPayloadBytes; List<zipkin.Span> receivedSpans = Codec.JSON.readSpans(deserializableBytes); assertThat(receivedSpans).isEqualTo(sentSpans); } finally {