diff --git a/traceql/clickhouse_transpiler/traces_data.js b/traceql/clickhouse_transpiler/traces_data.js index 4c0f0662..fc3ab193 100644 --- a/traceql/clickhouse_transpiler/traces_data.js +++ b/traceql/clickhouse_transpiler/traces_data.js @@ -7,23 +7,26 @@ const processFn = (sel, ctx) => { const table = !ctx.isCluster ? ctx.tracesTable : ctx.tracesDistTable const withMain = new Sql.With('index_grouped', sel) const withTraceIds = new Sql.With('trace_ids', (new Sql.Select()) + .select('trace_id') + .from(new Sql.WithReference(withMain))) + const withTraceIdsSpanIds = new Sql.With('trace_span_ids', (new Sql.Select()) .select('trace_id', 'span_id') .from(new Sql.WithReference(withMain)) .join('span_id', 'array')) return (new Sql.Select()) - .with(withMain, withTraceIds) + .with(withMain, withTraceIds, withTraceIdsSpanIds) .select( [new Sql.Raw('lower(hex(traces.trace_id))'), 'trace_id'], - [new Sql.Raw('arrayMap(x -> lower(hex(x)), groupArray(traces.span_id))'), 'span_id'], - [new Sql.Raw('groupArray(traces.duration_ns)'), 'duration'], - [new Sql.Raw('groupArray(traces.timestamp_ns)'), 'timestamp_ns'], + [new Sql.Raw(`arrayMap(x -> lower(hex(x)), groupArrayIf(traces.span_id, (traces.trace_id, traces.span_id) IN ${new Sql.WithReference(withTraceIdsSpanIds)}))`), 'span_id'], + [new Sql.Raw(`groupArrayIf(traces.duration_ns, (traces.trace_id, traces.span_id) IN ${new Sql.WithReference(withTraceIdsSpanIds)})`), 'duration'], + [new Sql.Raw(`groupArrayIf(traces.timestamp_ns, (traces.trace_id, traces.span_id) IN ${new Sql.WithReference(withTraceIdsSpanIds)})`), 'timestamp_ns'], [new Sql.Raw('min(traces.timestamp_ns)'), 'start_time_unix_nano'], [new Sql.Raw( 'toFloat64(max(traces.timestamp_ns + traces.duration_ns) - min(traces.timestamp_ns)) / 1000000' ), 'duration_ms'], [new Sql.Raw('argMin(traces.name, traces.timestamp_ns)', 'root_service_name'), 'root_service_name'] ).from([table, 'traces']).where(Sql.And( - new Sql.In(new Sql.Raw('(traces.trace_id, traces.span_id)'), 'in', new Sql.WithReference(withTraceIds)) + new Sql.In(new Sql.Raw('traces.trace_id'), 'in', new Sql.WithReference(withTraceIds)) )).groupBy('traces.trace_id') .orderBy(['start_time_unix_nano', 'desc']) } diff --git a/traceql/index.js b/traceql/index.js index 2c297dcc..37beedf9 100644 --- a/traceql/index.js +++ b/traceql/index.js @@ -28,10 +28,19 @@ const search = async (query, limit, from, to) => { } const scrpit = parser.ParseScript(query) const complexity = await evaluateComplexity(ctx, scrpit.rootToken) + let res = [] if (complexity > 10000000) { - return await processComplexResult(ctx, scrpit.rootToken, complexity) + res = await processComplexResult(ctx, scrpit.rootToken, complexity) + } else { + res = await processSmallResult(ctx, scrpit.rootToken) } - return await processSmallResult(ctx, scrpit.rootToken) + res.forEach(t => + t.spanSets.forEach( + ss => ss.spans.sort( + (a, b) => b.startTimeUnixNano.localeCompare(a.startTimeUnixNano)) + ) + ) + return res } /** @@ -59,7 +68,6 @@ async function processComplexResult (ctx, script, complexity) { for (let i = 0; i < maxFilter; i++) { ctx.randomFilter = [maxFilter, i] const sql = planner(ctx) - console.log(sql.toString()) const response = await rawRequest(sql + ' FORMAT JSON', null, DATABASE_NAME()) if (response.data.data.length === parseInt(ctx.limit)) { const minStart = response.data.data.reduce((acc, row) =>