/** * Wrapper method to add timeline annotiation to current span with given message */ public static void addTimelineAnnotation(String msg) { Span span = Tracer.getCurrentSpan(); if (span != null) { span.addTimelineAnnotation(msg); } }
/** * Wrapper method to add key-value pair to TraceInfo of actual span */ public static void addKVAnnotation(String key, String value){ Span span = Tracer.getCurrentSpan(); if (span != null) { span.addKVAnnotation(key, value); } }
List<Span> roots = new LinkedList<>(); TraceTree traceTree = new TraceTree(spans); roots.addAll(traceTree.getSpansByParent().find(createTableSpan.getSpanId())); assertEquals("creating table", createTableSpan.getDescription()); traceTree = new TraceTree(spans); roots.clear(); roots.addAll(traceTree.getSpansByParent().find(putSpan.getSpanId())); assertEquals(1, roots.size());
throws IOException { jgen.writeStartObject(); if (span.getSpanId().isValid()) { jgen.writeStringField("a", span.getSpanId().toString()); if (span.getStartTimeMillis() != 0) { jgen.writeNumberField("b", span.getStartTimeMillis()); if (span.getStopTimeMillis() != 0) { jgen.writeNumberField("e", span.getStopTimeMillis()); if (!span.getDescription().isEmpty()) { jgen.writeStringField("d", span.getDescription()); String tracerId = span.getTracerId(); if (!tracerId.isEmpty()) { jgen.writeStringField("r", tracerId); for (SpanId parent : span.getParents()) { jgen.writeString(parent.toString()); Map<String, String> traceInfoMap = span.getKVAnnotations(); if (!traceInfoMap.isEmpty()) { jgen.writeObjectFieldStart("n"); span.getTimelineAnnotations(); if (!timelineAnnotations.isEmpty()) { jgen.writeArrayFieldStart("t");
/** * Converts a given HTrace span to a Zipkin Span. * <ul> * <li>First set the start annotation. [CS, SR], depending whether it is a client service or not. * <li>Set other id's, etc [TraceId's etc] * <li>Create binary annotations based on data from HTrace Span object. * <li>Set the last annotation. [SS, CR] * </ul> */ public Span convert(org.apache.htrace.core.Span hTraceSpan) { Span zipkinSpan = new Span(); String serviceName = hTraceSpan.getTracerId().toLowerCase(); Endpoint ep = new Endpoint(ipv4Address, (short) getPort(serviceName), serviceName); List<Annotation> annotationList = createZipkinAnnotations(hTraceSpan, ep); List<BinaryAnnotation> binaryAnnotationList = createZipkinBinaryAnnotations(hTraceSpan, ep); zipkinSpan.setTrace_id(hTraceSpan.getSpanId().getHigh()); if (hTraceSpan.getParents().length > 0) { if (hTraceSpan.getParents().length > 1) { LOG.error("zipkin doesn't support spans with multiple parents. Omitting " + "other parents for " + hTraceSpan); } zipkinSpan.setParent_id(hTraceSpan.getParents()[0].getLow()); } zipkinSpan.setId(hTraceSpan.getSpanId().getLow()); zipkinSpan.setName(hTraceSpan.getDescription()); zipkinSpan.setAnnotations(annotationList); zipkinSpan.setBinary_annotations(binaryAnnotationList); return zipkinSpan; }
@Override public int compare(Span a, Span b) { int cmp = a.getTracerId().compareTo(b.getTracerId()); if (cmp != 0) { return cmp; } return a.getSpanId().compareTo(b.getSpanId()); } };
headers.put("SpanId", span.toString()); headers.put("TracerId", span.getTracerId()); headers.put("Description", span.getDescription()); String body = span.toJson();
@Test public void testHTraceToZipkin() throws IOException { Span rootSpan = new MilliSpan.Builder(). description(ROOT_SPAN_DESC). parents(new SpanId[] { } ). spanId(new SpanId(100, 100)). tracerId("test"). begin(System.currentTimeMillis()). build(); Span innerOne = rootSpan.child("Some good work"); Span innerTwo = innerOne.child("Some more good work"); innerTwo.stop(); innerOne.stop(); rootSpan.addKVAnnotation("foo", "bar"); rootSpan.addTimelineAnnotation("timeline"); rootSpan.stop(); for (Span s : new Span[] {rootSpan, innerOne, innerTwo}) { com.twitter.zipkin.gen.Span zs = new HTraceToZipkinConverter(12345, (short) 12).convert(s); assertSpansAreEquivalent(s, zs); } }
@Test(timeout=120000) public void testSimpleTraces() throws IOException, InterruptedException { Tracer tracer = newTracer(); Span rootSpan = new MilliSpan.Builder(). description("root"). spanId(new SpanId(100, 100)). tracerId("test"). begin(System.currentTimeMillis()). build(); TraceScope rootScope = tracer.newScope("root"); TraceScope innerOne = tracer.newScope("innerOne"); TraceScope innerTwo = tracer.newScope("innerTwo"); innerTwo.close(); Assert.assertTrue(flumeServer.nextEventBodyAsString().contains("innerTwo")); innerOne.close(); Assert.assertTrue(flumeServer.nextEventBodyAsString().contains("innerOne")); rootSpan.addKVAnnotation("foo", "bar"); rootSpan.addTimelineAnnotation("timeline"); rootScope.close(); Assert.assertTrue(flumeServer.nextEventBodyAsString().contains("root")); tracer.close(); }
/** * Add annotations from the htrace Span. */ private List<Annotation> createZipkinAnnotations(org.apache.htrace.core.Span hTraceSpan, Endpoint ep) { List<Annotation> annotationList = new ArrayList<Annotation>(); // add first zipkin annotation. annotationList.add(createZipkinAnnotation(zipkinCoreConstants.CLIENT_SEND, hTraceSpan.getStartTimeMillis(), ep, true)); annotationList.add(createZipkinAnnotation(zipkinCoreConstants.SERVER_RECV, hTraceSpan.getStartTimeMillis(), ep, true)); // add HTrace time annotation for (TimelineAnnotation ta : hTraceSpan.getTimelineAnnotations()) { annotationList.add(createZipkinAnnotation(ta.getMessage(), ta.getTime(), ep, true)); } // add last zipkin annotation annotationList.add(createZipkinAnnotation(zipkinCoreConstants.SERVER_SEND, hTraceSpan.getStopTimeMillis(), ep, false)); annotationList.add(createZipkinAnnotation(zipkinCoreConstants.CLIENT_RECV, hTraceSpan.getStopTimeMillis(), ep, false)); return annotationList; }
private void assertSpansAreEquivalent(Span s, com.twitter.zipkin.gen.Span zs) { assertTrue("zipkin doesn't support multiple parents to a single span.", s.getParents().length <= 1); if (s.getParents().length == 1) { assertEquals(s.getParents()[0].getLow(), zs.getParent_id()); } assertEquals(s.getSpanId().getLow(), zs.getId()); Assert.assertNotNull(zs.getAnnotations()); if (ROOT_SPAN_DESC.equals(zs.getName())) { assertEquals(5, zs.getAnnotations().size());// two start, two stop + one timeline annotation assertEquals(1, zs.getBinary_annotations().size()); } else { assertEquals(4, zs.getAnnotations().size()); } } }
@Override public String call() throws InterruptedException { startLatch.countDown(); // Prevent any task from exiting until every task has started assertTrue(continueLatch.await(WAIT_TIME_SECONDS, TimeUnit.SECONDS)); // Annotate on the presumed child trace Tracer.getCurrentSpan().addTimelineAnnotation( Thread.currentThread().getName()); return Tracer.getCurrentSpan().getDescription(); } };
@Override public void receiveSpan(Span span) { if (running.get()) { try { this.queue.add(span); } catch (IllegalStateException e) { LOG.error("Error trying to append span (" + span.getDescription() + ") to the queue. Blocking Queue was full."); } } } }
SpansByParent(Collection<Span> spans) { TreeSet<Span> treeSet = new TreeSet<Span>(COMPARATOR); parentToSpans = new HashMap<SpanId, LinkedList<Span>>(); for (Span span : spans) { treeSet.add(span); for (SpanId parent : span.getParents()) { LinkedList<Span> list = parentToSpans.get(parent); if (list == null) { list = new LinkedList<Span>(); parentToSpans.put(parent, list); } list.add(span); } if (span.getParents().length == 0) { LinkedList<Span> list = parentToSpans.get(SpanId.INVALID); if (list == null) { list = new LinkedList<Span>(); parentToSpans.put(SpanId.INVALID, list); } list.add(span); } } this.treeSet = treeSet; }
long annoStartTime = System.currentTimeMillis() * 1000; Thread.sleep(500); ms.addTimelineAnnotation("anno"); Thread.sleep(500); long annoEndTime = System.currentTimeMillis() * 1000; Thread.sleep(500); ms.stop(); long endTime = System.currentTimeMillis() * 1000;
/** * Add a result to this aggregate result. * @param time Time in nanoseconds * @param span Span. To be kept if the time taken was over 1 second */ public void addResult(long time, Span span) { if (span == null) { return; } stats.addValue(TimeUnit.MILLISECONDS.convert(time, TimeUnit.NANOSECONDS)); if (TimeUnit.SECONDS.convert(time, TimeUnit.NANOSECONDS) >= 1) { traces.add(span.getTracerId()); } }
Tracer.getCurrentSpan().getTimelineAnnotations().isEmpty()); assertEquals("Duplicated child span descriptions.", TASK_COUNT, results.size());
throws IOException { jgen.writeStartObject(); if (span.getSpanId().isValid()) { jgen.writeStringField("a", span.getSpanId().toString()); if (span.getStartTimeMillis() != 0) { jgen.writeNumberField("b", span.getStartTimeMillis()); if (span.getStopTimeMillis() != 0) { jgen.writeNumberField("e", span.getStopTimeMillis()); if (!span.getDescription().isEmpty()) { jgen.writeStringField("d", span.getDescription()); String tracerId = span.getTracerId(); if (!tracerId.isEmpty()) { jgen.writeStringField("r", tracerId); for (SpanId parent : span.getParents()) { jgen.writeString(parent.toString()); Map<String, String> traceInfoMap = span.getKVAnnotations(); if (!traceInfoMap.isEmpty()) { jgen.writeObjectFieldStart("n"); span.getTimelineAnnotations(); if (!timelineAnnotations.isEmpty()) { jgen.writeArrayFieldStart("t");
/** * Converts a given HTrace span to a Zipkin Span. * <ul> * <li>First set the start annotation. [CS, SR], depending whether it is a client service or not. * <li>Set other id's, etc [TraceId's etc] * <li>Create binary annotations based on data from HTrace Span object. * <li>Set the last annotation. [SS, CR] * </ul> */ public Span convert(org.apache.htrace.core.Span hTraceSpan) { Span zipkinSpan = new Span(); String serviceName = hTraceSpan.getTracerId().toLowerCase(); Endpoint ep = new Endpoint(ipv4Address, (short) getPort(serviceName), serviceName); List<Annotation> annotationList = createZipkinAnnotations(hTraceSpan, ep); List<BinaryAnnotation> binaryAnnotationList = createZipkinBinaryAnnotations(hTraceSpan, ep); zipkinSpan.setTrace_id(hTraceSpan.getSpanId().getHigh()); if (hTraceSpan.getParents().length > 0) { if (hTraceSpan.getParents().length > 1) { LOG.error("zipkin doesn't support spans with multiple parents. Omitting " + "other parents for " + hTraceSpan); } zipkinSpan.setParent_id(hTraceSpan.getParents()[0].getLow()); } zipkinSpan.setId(hTraceSpan.getSpanId().getLow()); zipkinSpan.setName(hTraceSpan.getDescription()); zipkinSpan.setAnnotations(annotationList); zipkinSpan.setBinary_annotations(binaryAnnotationList); return zipkinSpan; }