Search in sources :

Example 1 with Aggregation

use of zipkin2.elasticsearch.internal.client.Aggregation in project zipkin by openzipkin.

the class ElasticsearchSpanStore method getTraces.

@Override
public Call<List<List<Span>>> getTraces(QueryRequest request) {
    if (!searchEnabled)
        return Call.emptyList();
    long endMillis = request.endTs();
    long beginMillis = Math.max(endMillis - request.lookback(), EARLIEST_MS);
    SearchRequest.Filters filters = new SearchRequest.Filters();
    filters.addRange("timestamp_millis", beginMillis, endMillis);
    if (request.serviceName() != null) {
        filters.addTerm("localEndpoint.serviceName", request.serviceName());
    }
    if (request.remoteServiceName() != null) {
        filters.addTerm("remoteEndpoint.serviceName", request.remoteServiceName());
    }
    if (request.spanName() != null) {
        filters.addTerm("name", request.spanName());
    }
    for (Map.Entry<String, String> kv : request.annotationQuery().entrySet()) {
        if (kv.getValue().isEmpty()) {
            filters.addTerm("_q", kv.getKey());
        } else {
            filters.addTerm("_q", kv.getKey() + "=" + kv.getValue());
        }
    }
    if (request.minDuration() != null) {
        filters.addRange("duration", request.minDuration(), request.maxDuration());
    }
    // We need to filter to traces that contain at least one span that matches the request,
    // but the zipkin API is supposed to order traces by first span, regardless of if it was
    // filtered or not. This is not possible without either multiple, heavyweight queries
    // or complex multiple indexing, defeating much of the elegance of using elasticsearch for this.
    // So we fudge and order on the first span among the filtered spans - in practice, there should
    // be no significant difference in user experience since span start times are usually very
    // close to each other in human time.
    Aggregation traceIdTimestamp = Aggregation.terms("traceId", request.limit()).addSubAggregation(Aggregation.min("timestamp_millis")).orderBy("timestamp_millis", "desc");
    List<String> indices = indexNameFormatter.formatTypeAndRange(TYPE_SPAN, beginMillis, endMillis);
    if (indices.isEmpty())
        return Call.emptyList();
    SearchRequest esRequest = SearchRequest.create(indices).filters(filters).addAggregation(traceIdTimestamp);
    HttpCall<List<String>> traceIdsCall = search.newCall(esRequest, BodyConverters.KEYS);
    Call<List<List<Span>>> result = traceIdsCall.flatMap(new GetSpansByTraceId(search, indices)).map(groupByTraceId);
    // clash on lower-64 bit. When strict trace ID is enabled, we only filter client-side on clash.
    return strictTraceId ? result.map(StrictTraceId.filterTraces(request)) : result;
}
Also used : SearchRequest(zipkin2.elasticsearch.internal.client.SearchRequest) Span(zipkin2.Span) Aggregation(zipkin2.elasticsearch.internal.client.Aggregation) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map)

Aggregations

Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Map (java.util.Map)1 Span (zipkin2.Span)1 Aggregation (zipkin2.elasticsearch.internal.client.Aggregation)1 SearchRequest (zipkin2.elasticsearch.internal.client.SearchRequest)1