-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathresources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.logs.$logId.spans.tsx
More file actions
100 lines (85 loc) · 3.15 KB
/
resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.logs.$logId.spans.tsx
File metadata and controls
100 lines (85 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/node";
import { requireUserId } from "~/services/session.server";
import { EnvironmentParamSchema } from "~/utils/pathBuilder";
import { findProjectBySlug } from "~/models/project.server";
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
import { clickhouseClient } from "~/services/clickhouseInstance.server";
// Convert ClickHouse kind to display level
function kindToLevel(
kind: string
): "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" | "LOG" {
switch (kind) {
case "DEBUG_EVENT":
case "LOG_DEBUG":
return "DEBUG";
case "LOG_INFO":
return "INFO";
case "LOG_WARN":
return "WARN";
case "LOG_ERROR":
return "ERROR";
case "LOG_LOG":
return "LOG";
case "SPAN":
case "ANCESTOR_OVERRIDE":
case "SPAN_EVENT":
default:
return "TRACE";
}
}
// Fetch related spans for a log entry from the same trace
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const { projectParam, organizationSlug, envParam, logId } = {
...EnvironmentParamSchema.parse(params),
logId: params.logId,
};
if (!logId) {
throw new Response("Log ID is required", { status: 400 });
}
const project = await findProjectBySlug(organizationSlug, projectParam, userId);
if (!project) {
throw new Response("Project not found", { status: 404 });
}
const environment = await findEnvironmentBySlug(project.id, envParam, userId);
if (!environment) {
throw new Response("Environment not found", { status: 404 });
}
// Get trace ID and run ID from query params
const url = new URL(request.url);
const traceId = url.searchParams.get("traceId");
const runId = url.searchParams.get("runId");
const currentSpanId = url.searchParams.get("spanId");
if (!traceId || !runId) {
throw new Response("Trace ID and Run ID are required", { status: 400 });
}
// Query ClickHouse for related spans in the same trace
const queryBuilder = clickhouseClient.taskEventsSearch.logsListQueryBuilder();
queryBuilder.where("environment_id = {environmentId: String}", {
environmentId: environment.id,
});
queryBuilder.where("trace_id = {traceId: String}", { traceId });
queryBuilder.where("run_id = {runId: String}", { runId });
// Order by start time to show spans in chronological order
queryBuilder.orderBy("start_time ASC");
queryBuilder.limit(50);
const [queryError, records] = await queryBuilder.execute();
if (queryError) {
throw queryError;
}
const results = records || [];
const spans = results.map((row) => ({
id: `${row.trace_id}::${row.span_id}::${row.run_id}::${row.start_time}`,
spanId: row.span_id,
parentSpanId: row.parent_span_id || null,
message: row.message.substring(0, 200), // Truncate for list view
kind: row.kind,
level: kindToLevel(row.kind),
status: row.status,
startTime: new Date(Number(row.start_time) / 1_000_000).toISOString(),
duration: Number(row.duration),
isCurrent: row.span_id === currentSpanId,
}));
return json({ spans });
};