Search in sources :

Example 1 with SelectField

use of org.jooq.SelectField in project zipkin by openzipkin.

the class MySQLSpanStore method toTraceIdQuery.

SelectOffsetStep<? extends Record> toTraceIdQuery(DSLContext context, QueryRequest request) {
    long endTs = (request.endTs > 0 && request.endTs != Long.MAX_VALUE) ? request.endTs * 1000 : System.currentTimeMillis() * 1000;
    TableOnConditionStep<?> table = ZIPKIN_SPANS.join(ZIPKIN_ANNOTATIONS).on(schema.joinCondition(ZIPKIN_ANNOTATIONS));
    int i = 0;
    for (String key : request.annotations) {
        ZipkinAnnotations aTable = ZIPKIN_ANNOTATIONS.as("a" + i++);
        table = maybeOnService(table.join(aTable).on(schema.joinCondition(aTable)).and(aTable.A_TYPE.eq(-1)).and(aTable.A_KEY.eq(key)), aTable, request.serviceName);
    }
    for (Map.Entry<String, String> kv : request.binaryAnnotations.entrySet()) {
        ZipkinAnnotations aTable = ZIPKIN_ANNOTATIONS.as("a" + i++);
        table = maybeOnService(table.join(aTable).on(schema.joinCondition(aTable)).and(aTable.A_TYPE.eq(STRING.value)).and(aTable.A_KEY.eq(kv.getKey())).and(aTable.A_VALUE.eq(kv.getValue().getBytes(UTF_8))), aTable, request.serviceName);
    }
    List<SelectField<?>> distinctFields = new ArrayList<>(schema.spanIdFields);
    distinctFields.add(ZIPKIN_SPANS.START_TS.max());
    SelectConditionStep<Record> dsl = context.selectDistinct(distinctFields).from(table).where(ZIPKIN_SPANS.START_TS.between(endTs - request.lookback * 1000, endTs));
    if (request.serviceName != null) {
        dsl.and(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME.eq(request.serviceName));
    }
    if (request.spanName != null) {
        dsl.and(ZIPKIN_SPANS.NAME.eq(request.spanName));
    }
    if (request.minDuration != null && request.maxDuration != null) {
        dsl.and(ZIPKIN_SPANS.DURATION.between(request.minDuration, request.maxDuration));
    } else if (request.minDuration != null) {
        dsl.and(ZIPKIN_SPANS.DURATION.greaterOrEqual(request.minDuration));
    }
    return dsl.groupBy(schema.spanIdFields).orderBy(ZIPKIN_SPANS.START_TS.max().desc()).limit(request.limit);
}
Also used : ZipkinAnnotations(zipkin.storage.mysql.internal.generated.tables.ZipkinAnnotations) SelectField(org.jooq.SelectField) ArrayList(java.util.ArrayList) Record(org.jooq.Record) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Endpoint(zipkin.Endpoint)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Record (org.jooq.Record)1 SelectField (org.jooq.SelectField)1 Endpoint (zipkin.Endpoint)1 ZipkinAnnotations (zipkin.storage.mysql.internal.generated.tables.ZipkinAnnotations)1