HBaseSpanViewer { public static String toJsonString(final Message message) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out, Charset.defaultCharset()); appendJsonString(message, writer); writer.flush(); out.flush(); return out.toString(); } HBaseSpanViewer(Configuration conf); void close(); void startClient(); void stopClient(); List<SpanProtos.Span> getSpans(long traceid); List<SpanProtos.Span> getRootSpans(); static String toJsonString(final Message message); static void appendJsonString(final Message message,
                                        OutputStreamWriter writer); static void main(String[] args);  }
@Test public void testProtoToJson() { Span.Builder sbuilder = Span.newBuilder(); TimelineAnnotation.Builder tlbuilder = TimelineAnnotation.newBuilder(); sbuilder.clear().setTraceId(1) .setParentId(2) .setStart(3) .setStop(4) .setSpanId(5) .setProcessId("pid") .setDescription("description"); for (int i = 0; i < 3; i++) { sbuilder.addTimeline(tlbuilder.clear() .setTime(i) .setMessage("message" + i) .build()); } Span span = sbuilder.build(); try { String json = HBaseSpanViewer.toJsonString(span); String expected = "{\"trace_id\":\"1\"," + "\"parent_id\":\"2\"," + "\"start\":\"3\"," + "\"stop\":\"4\"," + "\"span_id\":\"5\"," + "\"process_id\":\"pid\"," + "\"description\":\"description\"," + "\"timeline\":[" + "{\"time\":\"0\",\"message\":\"message0\"}," + "{\"time\":\"1\",\"message\":\"message1\"}," + "{\"time\":\"2\",\"message\":\"message2\"}]}"; Assert.assertEquals(json, expected); } catch (IOException e) { Assert.fail("failed to get json from span. " + e.getMessage()); } }
@Test public void testProtoWithoutTimelineToJson() { Span.Builder sbuilder = Span.newBuilder(); sbuilder.clear().setTraceId(1) .setParentId(2) .setStart(3) .setStop(4) .setSpanId(5) .setProcessId("pid") .setDescription("description"); Span span = sbuilder.build(); try { String json = HBaseSpanViewer.toJsonString(span); String expected = "{\"trace_id\":\"1\"," + "\"parent_id\":\"2\"," + "\"start\":\"3\"," + "\"stop\":\"4\"," + "\"span_id\":\"5\"," + "\"process_id\":\"pid\"," + "\"description\":\"description\"}"; Assert.assertEquals(json, expected); } catch (IOException e) { Assert.fail("failed to get json from span. " + e.getMessage()); } }