Search in sources :

Example 1 with Pair

use of zipkin.internal.Pair in project zipkin by openzipkin.

the class MySQLSpanStore method getTraces.

List<List<Span>> getTraces(@Nullable QueryRequest request, @Nullable Long traceIdHigh, @Nullable Long traceIdLow, boolean raw) {
    if (traceIdHigh != null && !strictTraceId)
        traceIdHigh = null;
    final Map<Pair<Long>, List<Span>> spansWithoutAnnotations;
    final Map<Row3<Long, Long, Long>, List<Record>> dbAnnotations;
    try (Connection conn = datasource.getConnection()) {
        Condition traceIdCondition = request != null ? schema.spanTraceIdCondition(toTraceIdQuery(context.get(conn), request)) : schema.spanTraceIdCondition(traceIdHigh, traceIdLow);
        spansWithoutAnnotations = context.get(conn).select(schema.spanFields).from(ZIPKIN_SPANS).where(traceIdCondition).stream().map(r -> Span.builder().traceIdHigh(maybeGet(r, ZIPKIN_SPANS.TRACE_ID_HIGH, 0L)).traceId(r.getValue(ZIPKIN_SPANS.TRACE_ID)).name(r.getValue(ZIPKIN_SPANS.NAME)).id(r.getValue(ZIPKIN_SPANS.ID)).parentId(r.getValue(ZIPKIN_SPANS.PARENT_ID)).timestamp(r.getValue(ZIPKIN_SPANS.START_TS)).duration(r.getValue(ZIPKIN_SPANS.DURATION)).debug(r.getValue(ZIPKIN_SPANS.DEBUG)).build()).collect(groupingBy((Span s) -> Pair.create(s.traceIdHigh, s.traceId), LinkedHashMap::new, Collectors.<Span>toList()));
        dbAnnotations = context.get(conn).select(schema.annotationFields).from(ZIPKIN_ANNOTATIONS).where(schema.annotationsTraceIdCondition(spansWithoutAnnotations.keySet())).orderBy(ZIPKIN_ANNOTATIONS.A_TIMESTAMP.asc(), ZIPKIN_ANNOTATIONS.A_KEY.asc()).stream().collect(groupingBy((Record a) -> row(maybeGet(a, ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, 0L), a.getValue(ZIPKIN_ANNOTATIONS.TRACE_ID), a.getValue(ZIPKIN_ANNOTATIONS.SPAN_ID)), LinkedHashMap::new, // LinkedHashMap preserves order while grouping
        Collectors.<Record>toList()));
    } catch (SQLException e) {
        throw new RuntimeException("Error querying for " + request + ": " + e.getMessage());
    }
    List<Span> allSpans = new ArrayList<>(spansWithoutAnnotations.size());
    for (List<Span> spans : spansWithoutAnnotations.values()) {
        for (Span s : spans) {
            Span.Builder span = s.toBuilder();
            Row3<Long, Long, Long> key = row(s.traceIdHigh, s.traceId, s.id);
            if (dbAnnotations.containsKey(key)) {
                for (Record a : dbAnnotations.get(key)) {
                    Endpoint endpoint = endpoint(a);
                    int type = a.getValue(ZIPKIN_ANNOTATIONS.A_TYPE);
                    if (type == -1) {
                        span.addAnnotation(Annotation.create(a.getValue(ZIPKIN_ANNOTATIONS.A_TIMESTAMP), a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), endpoint));
                    } else {
                        span.addBinaryAnnotation(BinaryAnnotation.create(a.getValue(ZIPKIN_ANNOTATIONS.A_KEY), a.getValue(ZIPKIN_ANNOTATIONS.A_VALUE), Type.fromValue(type), endpoint));
                    }
                }
            }
            allSpans.add(span.build());
        }
    }
    return GroupByTraceId.apply(allSpans, strictTraceId, !raw);
}
Also used : Condition(org.jooq.Condition) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Span(zipkin.Span) DependencyLinkSpan(zipkin.internal.DependencyLinkSpan) Endpoint(zipkin.Endpoint) LinkedHashMap(java.util.LinkedHashMap) Endpoint(zipkin.Endpoint) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Record(org.jooq.Record) Pair(zipkin.internal.Pair) Row3(org.jooq.Row3)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Condition (org.jooq.Condition)1 Record (org.jooq.Record)1 Row3 (org.jooq.Row3)1 Endpoint (zipkin.Endpoint)1 Span (zipkin.Span)1 DependencyLinkSpan (zipkin.internal.DependencyLinkSpan)1 Pair (zipkin.internal.Pair)1