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);
}
Aggregations