-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
query light-weight trace info on span insert
- Loading branch information
1 parent
8171a61
commit 84d28c6
Showing
5 changed files
with
184 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
frontend/app/api/projects/[projectId]/traces/[traceId]/spans/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { and, asc, eq, inArray, sql } from 'drizzle-orm'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { searchSpans } from '@/lib/clickhouse/spans'; | ||
import { TimeRange } from '@/lib/clickhouse/utils'; | ||
import { db } from '@/lib/db/drizzle'; | ||
import { events, spans } from '@/lib/db/migrations/schema'; | ||
|
||
export async function GET( | ||
req: NextRequest, | ||
props: { params: Promise<{ projectId: string; traceId: string }> } | ||
): Promise<Response> { | ||
const params = await props.params; | ||
const projectId = params.projectId; | ||
const traceId = params.traceId; | ||
const searchQuery = req.nextUrl.searchParams.get("search"); | ||
|
||
let searchSpanIds = null; | ||
if (searchQuery) { | ||
const timeRange = { pastHours: 'all' } as TimeRange; | ||
const searchResult = await searchSpans(projectId, searchQuery, timeRange); | ||
searchSpanIds = Array.from(searchResult.spanIds); | ||
} | ||
|
||
const spanEventsQuery = db.$with('span_events').as( | ||
db.select({ | ||
spanId: events.spanId, | ||
projectId: events.projectId, | ||
events: sql`jsonb_agg(jsonb_build_object( | ||
'id', events.id, | ||
'spanId', events.span_id, | ||
'timestamp', events.timestamp, | ||
'name', events.name, | ||
'attributes', events.attributes | ||
))`.as('events') | ||
}) | ||
.from(events) | ||
.groupBy(events.spanId, events.projectId) | ||
); | ||
|
||
const spanItems = await db.with(spanEventsQuery).select({ | ||
// inputs and outputs are ignored on purpose | ||
spanId: spans.spanId, | ||
startTime: spans.startTime, | ||
endTime: spans.endTime, | ||
traceId: spans.traceId, | ||
parentSpanId: spans.parentSpanId, | ||
name: spans.name, | ||
attributes: spans.attributes, | ||
spanType: spans.spanType, | ||
events: sql`COALESCE(${spanEventsQuery.events}, '[]'::jsonb)`.as('events'), | ||
}) | ||
.from(spans) | ||
.leftJoin(spanEventsQuery, | ||
and( | ||
eq(spans.spanId, spanEventsQuery.spanId), | ||
eq(spans.projectId, spanEventsQuery.projectId) | ||
) | ||
) | ||
.where( | ||
and( | ||
eq(spans.traceId, traceId), | ||
eq(spans.projectId, projectId), | ||
...(searchSpanIds ? [inArray(spans.spanId, searchSpanIds)] : []) | ||
) | ||
) | ||
.orderBy(asc(spans.startTime)); | ||
|
||
|
||
return NextResponse.json(spanItems); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.