Search in sources :

Example 1 with HttpCall

use of zipkin2.elasticsearch.internal.client.HttpCall 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)

Example 2 with HttpCall

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

the class BulkCallBuilder method build.

/**
 * Creates a bulk request when there is more than one object to store
 */
public HttpCall<Void> build() {
    QueryStringEncoder urlBuilder = new QueryStringEncoder("/_bulk");
    if (pipeline != null)
        urlBuilder.addParam("pipeline", pipeline);
    if (waitForRefresh)
        urlBuilder.addParam("refresh", "wait_for");
    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc, () -> PooledByteBufAllocator.DEFAULT);
    HttpCall.RequestSupplier request = new BulkRequestSupplier(entries, shouldAddType, RequestHeaders.of(HttpMethod.POST, urlBuilder.toString(), HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8), alloc);
    return http.newCall(request, CHECK_FOR_ERRORS, tag);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpCall(zipkin2.elasticsearch.internal.client.HttpCall) RequestContext(com.linecorp.armeria.common.RequestContext) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Aggregations

RequestContext (com.linecorp.armeria.common.RequestContext)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)1 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 HttpCall (zipkin2.elasticsearch.internal.client.HttpCall)1 SearchRequest (zipkin2.elasticsearch.internal.client.SearchRequest)1