Search in sources :

Example 1 with SearchRequest

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

the class ElasticsearchSpanStore method getTraces.

@Override
public Call<List<List<Span>>> getTraces(Iterable<String> traceIds) {
    Set<String> normalizedTraceIds = new LinkedHashSet<>();
    for (String traceId : traceIds) {
        // make sure we have a 16 or 32 character trace ID
        traceId = Span.normalizeTraceId(traceId);
        // Unless we are strict, truncate the trace ID to 64bit (encoded as 16 characters)
        if (!strictTraceId && traceId.length() == 32)
            traceId = traceId.substring(16);
        normalizedTraceIds.add(traceId);
    }
    if (normalizedTraceIds.isEmpty())
        return Call.emptyList();
    SearchRequest request = SearchRequest.create(asList(allSpanIndices)).terms("traceId", normalizedTraceIds);
    return search.newCall(request, BodyConverters.SPANS).map(groupByTraceId);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SearchRequest(zipkin2.elasticsearch.internal.client.SearchRequest)

Example 2 with SearchRequest

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

the class ElasticsearchSpanStore method getServiceNames.

@Override
public Call<List<String>> getServiceNames() {
    if (!searchEnabled)
        return Call.emptyList();
    long endMillis = System.currentTimeMillis();
    long beginMillis = endMillis - namesLookback;
    List<String> indices = indexNameFormatter.formatTypeAndRange(TYPE_SPAN, beginMillis, endMillis);
    if (indices.isEmpty())
        return Call.emptyList();
    SearchRequest request = SearchRequest.create(indices).filters(new SearchRequest.Filters().addRange("timestamp_millis", beginMillis, endMillis)).addAggregation(Aggregation.terms("localEndpoint.serviceName", Integer.MAX_VALUE));
    return search.newCall(request, BodyConverters.KEYS);
}
Also used : SearchRequest(zipkin2.elasticsearch.internal.client.SearchRequest)

Example 3 with SearchRequest

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

the class ElasticsearchAutocompleteTags method getValues.

@Override
public Call<List<String>> getValues(String key) {
    if (key == null)
        throw new NullPointerException("key == null");
    if (key.isEmpty())
        throw new IllegalArgumentException("key was empty");
    if (!enabled)
        return Call.emptyList();
    long endMillis = System.currentTimeMillis();
    long beginMillis = endMillis - namesLookback;
    List<String> indices = indexNameFormatter.formatTypeAndRange(TYPE_AUTOCOMPLETE, beginMillis, endMillis);
    if (indices.isEmpty())
        return Call.emptyList();
    SearchRequest.Filters filters = new SearchRequest.Filters().addTerm("tagKey", key);
    SearchRequest request = SearchRequest.create(indices).filters(filters).addAggregation(Aggregation.terms("tagValue", Integer.MAX_VALUE));
    return search.newCall(request, BodyConverters.KEYS);
}
Also used : SearchRequest(zipkin2.elasticsearch.internal.client.SearchRequest)

Example 4 with SearchRequest

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

the class ElasticsearchSpanStore method getTrace.

@Override
public Call<List<Span>> getTrace(String traceId) {
    // make sure we have a 16 or 32 character trace ID
    traceId = Span.normalizeTraceId(traceId);
    // Unless we are strict, truncate the trace ID to 64bit (encoded as 16 characters)
    if (!strictTraceId && traceId.length() == 32)
        traceId = traceId.substring(16);
    SearchRequest request = SearchRequest.create(asList(allSpanIndices)).term("traceId", traceId);
    return search.newCall(request, BodyConverters.SPANS);
}
Also used : SearchRequest(zipkin2.elasticsearch.internal.client.SearchRequest)

Example 5 with SearchRequest

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

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