Skip to content

Commit 69fba38

Browse files
authored
Coerces non-string binary annotations to Span 2 tags (openzipkin#1689)
This completes one-way conversion by reusing the same approach as exists in stackdriver-zipkin.
1 parent 66ab03a commit 69fba38

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

zipkin/src/main/java/zipkin/internal/Span2Converter.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package zipkin.internal;
1515

16+
import java.nio.ByteBuffer;
1617
import java.util.ArrayList;
1718
import java.util.Collections;
1819
import java.util.List;
@@ -25,7 +26,6 @@
2526
import zipkin.internal.Span2.Kind;
2627

2728
import static zipkin.BinaryAnnotation.Type.BOOL;
28-
import static zipkin.BinaryAnnotation.Type.STRING;
2929
import static zipkin.Constants.CLIENT_ADDR;
3030
import static zipkin.Constants.LOCAL_COMPONENT;
3131
import static zipkin.Constants.SERVER_ADDR;
@@ -187,14 +187,39 @@ void processBinaryAnnotations(Span source) {
187187
sa = b.endpoint;
188188
} else if (Constants.MESSAGE_ADDR.equals(b.key)) {
189189
ma = b.endpoint;
190+
} else {
191+
forEndpoint(source, b.endpoint).putTag(b.key, b.value[0] == 1 ? "true" : "false");
190192
}
191193
continue;
192194
}
195+
193196
Span2.Builder currentSpan = forEndpoint(source, b.endpoint);
194-
if (b.type == STRING) {
195-
// don't add marker "lc" tags
196-
if (Constants.LOCAL_COMPONENT.equals(b.key) && b.value.length == 0) continue;
197-
currentSpan.putTag(b.key, new String(b.value, Util.UTF_8));
197+
switch (b.type) {
198+
case BOOL:
199+
break; // already handled
200+
case STRING:
201+
// don't add marker "lc" tags
202+
if (Constants.LOCAL_COMPONENT.equals(b.key) && b.value.length == 0) continue;
203+
currentSpan.putTag(b.key, new String(b.value, Util.UTF_8));
204+
break;
205+
case BYTES:
206+
Buffer buffer = new Buffer(Buffer.base64UrlSizeInBytes(b.value));
207+
String encoded = new String(buffer.writeBase64Url(b.value).toByteArray(), Util.UTF_8);
208+
currentSpan.putTag(b.key, encoded);
209+
break;
210+
case I16:
211+
currentSpan.putTag(b.key, Short.toString(ByteBuffer.wrap(b.value).getShort()));
212+
break;
213+
case I32:
214+
currentSpan.putTag(b.key, Integer.toString(ByteBuffer.wrap(b.value).getInt()));
215+
break;
216+
case I64:
217+
currentSpan.putTag(b.key, Long.toString(ByteBuffer.wrap(b.value).getLong()));
218+
break;
219+
case DOUBLE:
220+
double wrapped = Double.longBitsToDouble(ByteBuffer.wrap(b.value).getLong());
221+
currentSpan.putTag(b.key, Double.toString(wrapped));
222+
break;
198223
}
199224
}
200225

zipkin/src/test/java/zipkin/internal/Span2ConverterTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
*/
1414
package zipkin.internal;
1515

16+
import java.nio.ByteBuffer;
1617
import org.junit.Test;
1718
import zipkin.Annotation;
1819
import zipkin.BinaryAnnotation;
20+
import zipkin.BinaryAnnotation.Type;
1921
import zipkin.Constants;
2022
import zipkin.Endpoint;
2123
import zipkin.Span;
@@ -722,4 +724,41 @@ public class Span2ConverterTest {
722724
assertThat(Span2Converter.fromSpan(shared))
723725
.containsExactly(first, second);
724726
}
727+
728+
// test converted from stackdriver-zipkin
729+
@Test public void convertBinaryAnnotations() {
730+
byte[] boolBuffer = ByteBuffer.allocate(1).put((byte) 1).array();
731+
byte[] shortBuffer = ByteBuffer.allocate(2).putShort((short) 20).array();
732+
byte[] intBuffer = ByteBuffer.allocate(4).putInt(32800).array();
733+
byte[] longBuffer = ByteBuffer.allocate(8).putLong(2147483700L).array();
734+
byte[] doubleBuffer = ByteBuffer.allocate(8).putDouble(3.1415).array();
735+
byte[] bytesBuffer = "any carnal pleasure".getBytes(Util.UTF_8);
736+
Span span = Span.builder()
737+
.traceId(1)
738+
.name("test")
739+
.id(2)
740+
.addBinaryAnnotation(BinaryAnnotation.create("bool", boolBuffer, Type.BOOL, frontend))
741+
.addBinaryAnnotation(BinaryAnnotation.create("short", shortBuffer, Type.I16, frontend))
742+
.addBinaryAnnotation(BinaryAnnotation.create("int", intBuffer, Type.I32, frontend))
743+
.addBinaryAnnotation(BinaryAnnotation.create("long", longBuffer, Type.I64, frontend))
744+
.addBinaryAnnotation(BinaryAnnotation.create("double", doubleBuffer, Type.DOUBLE, frontend))
745+
.addBinaryAnnotation(BinaryAnnotation.create("bytes", bytesBuffer, Type.BYTES, frontend))
746+
.build();
747+
748+
Span2 span2 = Span2.builder()
749+
.traceId(1)
750+
.name("test")
751+
.id(2)
752+
.localEndpoint(frontend)
753+
.putTag("bool", "true")
754+
.putTag("short", "20")
755+
.putTag("int", "32800")
756+
.putTag("long", "2147483700")
757+
.putTag("double", "3.1415")
758+
.putTag("bytes", "YW55IGNhcm5hbCBwbGVhc3VyZQ==") // from https://en.wikipedia.org/wiki/Base64
759+
.build();
760+
761+
assertThat(Span2Converter.fromSpan(span))
762+
.containsExactly(span2);
763+
}
725764
}

0 commit comments

Comments
 (0)