Search in sources :

Example 26 with Span

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);
    }
}
Also used : JsonReader(com.google.gson.stream.JsonReader) LinkedList(java.util.LinkedList) List(java.util.List) Span(zipkin.Span) LinkedList(java.util.LinkedList) IOException(java.io.IOException) MalformedJsonException(com.google.gson.stream.MalformedJsonException)

Example 27 with Span

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();
}
Also used : ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) List(java.util.List) Span(zipkin.Span) Endpoint(zipkin.Endpoint)

Example 28 with Span

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);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Util.sortedList(zipkin.internal.Util.sortedList) ArrayList(java.util.ArrayList) Span(zipkin.Span) LinkedHashMap(java.util.LinkedHashMap)

Example 29 with Span

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));
}
Also used : Endpoint(zipkin.Endpoint) Span(zipkin.Span) Test(org.junit.Test)

Example 30 with Span

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();
}
Also used : Span(zipkin.Span) Test(org.junit.Test)

Aggregations

Span (zipkin.Span)104 Test (org.junit.Test)84 Endpoint (zipkin.Endpoint)21 BinaryAnnotation (zipkin.BinaryAnnotation)12 ArrayList (java.util.ArrayList)11 Annotation (zipkin.Annotation)10 CodecTest (zipkin.CodecTest)9 CorrectForClockSkew.isLocalSpan (zipkin.internal.CorrectForClockSkew.isLocalSpan)9 ByteBuffer (java.nio.ByteBuffer)8 List (java.util.List)8 Buffer (okio.Buffer)8 LinkedHashMap (java.util.LinkedHashMap)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 ImmutableList (com.google.common.collect.ImmutableList)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 LinkedList (java.util.LinkedList)4 Constants (zipkin.Constants)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 IOException (java.io.IOException)3 Arrays.asList (java.util.Arrays.asList)3