use of zipkin.Span in project zipkin by openzipkin.
the class JsonCodec method readTraces.
public List<List<Span>> readTraces(byte[] bytes) {
JsonReader reader = jsonReader(bytes);
// cause we don't know how long it will be
List<List<Span>> result = new LinkedList<>();
try {
reader.beginArray();
while (reader.hasNext()) {
reader.beginArray();
// cause we don't know how long it will be
List<Span> trace = new LinkedList<>();
while (reader.hasNext()) {
trace.add(SPAN_ADAPTER.fromJson(reader));
}
reader.endArray();
result.add(trace);
}
reader.endArray();
return result;
} catch (Exception e) {
throw exceptionReading("List<List<Span>>", bytes, e);
}
}
use of zipkin.Span in project zipkin by openzipkin.
the class JsonCodec method writeTraces.
@Override
public byte[] writeTraces(List<List<Span>> traces) {
// Get the encoded size of the nested list so that we don't need to grow the buffer
int sizeInBytes = overheadInBytes(traces);
for (int i = 0; i < traces.size(); i++) {
List<Span> spans = traces.get(i);
sizeInBytes += overheadInBytes(spans);
for (int j = 0; j < spans.size(); j++) {
sizeInBytes += SPAN_ADAPTER.sizeInBytes(spans.get(j));
}
}
Buffer out = new Buffer(sizeInBytes);
// start list of traces
out.writeByte('[');
for (Iterator<List<Span>> trace = traces.iterator(); trace.hasNext(); ) {
writeList(SPAN_ADAPTER, trace.next(), out);
if (trace.hasNext())
out.writeByte(',');
}
// stop list of traces
out.writeByte(']');
return out.toByteArray();
}
use of zipkin.Span in project zipkin by openzipkin.
the class MergeById method apply.
public static List<Span> apply(Iterable<Span> spans) {
if (spans == null || !spans.iterator().hasNext())
return Collections.emptyList();
List<Span> result = new ArrayList<>();
Map<Long, List<Span>> spanIdToSpans = new LinkedHashMap<>();
for (Span span : spans) {
if (!spanIdToSpans.containsKey(span.id)) {
spanIdToSpans.put(span.id, new LinkedList<>());
}
spanIdToSpans.get(span.id).add(span);
}
for (List<Span> spansToMerge : spanIdToSpans.values()) {
if (spansToMerge.size() == 1) {
result.add(spansToMerge.get(0));
} else {
Span.Builder builder = spansToMerge.get(0).toBuilder();
for (int i = 1, length = spansToMerge.size(); i < length; i++) {
builder.merge(spansToMerge.get(i));
}
result.add(builder.build());
}
}
// Apply timestamp so that sorting will be helpful
for (int i = 0; i < result.size(); i++) {
result.set(i, ApplyTimestampAndDuration.apply(result.get(i)));
}
return sortedList(result);
}
use of zipkin.Span in project zipkin by openzipkin.
the class DependenciesTest method noEmptyLinks.
/** Some use empty string for the {@link Constants#CLIENT_ADDR} to defer naming to the server. */
@Test
public void noEmptyLinks() {
Endpoint someClient = Endpoint.create("", 172 << 24 | 17 << 16 | 4);
List<Span> trace = asList(Span.builder().traceId(20L).id(20L).name("get").timestamp(TODAY * 1000).duration(350L * 1000).addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, someClient)).addBinaryAnnotation(BinaryAnnotation.address(SERVER_ADDR, WEB_ENDPOINT)).build(), Span.builder().traceId(20L).parentId(20L).id(21L).name("get").timestamp((TODAY + 50) * 1000).duration(250L * 1000).addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, WEB_ENDPOINT)).addBinaryAnnotation(BinaryAnnotation.address(SERVER_ADDR, APP_ENDPOINT)).build(), Span.builder().traceId(20L).parentId(21L).id(22L).name("get").timestamp((TODAY + 150) * 1000).duration(50L * 1000).addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, APP_ENDPOINT)).addBinaryAnnotation(BinaryAnnotation.address(SERVER_ADDR, DB_ENDPOINT)).build());
processDependencies(trace);
assertThat(store().getDependencies(TODAY + 1000, null)).containsOnly(DependencyLink.create("web", "app", 1), DependencyLink.create("app", "db", 1));
}
use of zipkin.Span in project zipkin by openzipkin.
the class QueryRequestTest method matchesImplicitTimestamp.
/** When a span comes in without a timestamp, use the implicit one based on annotations. */
@Test
public void matchesImplicitTimestamp() {
Span asyncReceive = Span.builder().traceId(10L).id(10L).name("receive").addAnnotation(Annotation.create((TODAY) * 1000, SERVER_RECV, APP_ENDPOINT)).build();
QueryRequest request = QueryRequest.builder().endTs(TODAY).build();
assertThat(request.test(asList(asyncReceive))).isTrue();
}
Aggregations