Skip to content

Commit 00f34be

Browse files
committed
Use a dedicated logs ClickHouse client
1 parent 335f09e commit 00f34be

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

apps/webapp/app/env.server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,12 @@ const EnvironmentSchema = z
11981198
// Query feature flag
11991199
QUERY_FEATURE_ENABLED: z.string().default("1"),
12001200

1201+
// Logs page ClickHouse URL (for logs queries)
1202+
LOGS_CLICKHOUSE_URL: z
1203+
.string()
1204+
.optional()
1205+
.transform((v) => v ?? process.env.CLICKHOUSE_URL),
1206+
12011207
// Query page ClickHouse limits (for TSQL queries)
12021208
QUERY_CLICKHOUSE_URL: z
12031209
.string()

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.logs/route.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
1616
import { LogsListPresenter, LogEntry } from "~/presenters/v3/LogsListPresenter.server";
1717
import type { LogLevel } from "~/utils/logUtils";
1818
import { $replica, prisma } from "~/db.server";
19-
import { clickhouseClient } from "~/services/clickhouseInstance.server";
19+
import { logsClickhouseClient } from "~/services/clickhouseInstance.server";
2020
import { NavBar, PageTitle } from "~/components/primitives/PageHeader";
2121
import { PageBody, PageContainer } from "~/components/layout/AppLayout";
2222
import { Suspense, useCallback, useEffect, useMemo, useRef, useState, useTransition } from "react";
@@ -134,7 +134,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
134134
const plan = await getCurrentPlan(project.organizationId);
135135
const retentionLimitDays = plan?.v3Subscription?.plan?.limits.logRetentionDays.number ?? 30;
136136

137-
const presenter = new LogsListPresenter($replica, clickhouseClient);
137+
const presenter = new LogsListPresenter($replica, logsClickhouseClient);
138138

139139
const listPromise = presenter
140140
.call(project.organizationId, environment.id, {

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.logs.$logId.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
22
import { typedjson } from "remix-typedjson";
33
import { z } from "zod";
4-
import { clickhouseClient } from "~/services/clickhouseInstance.server";
4+
import { logsClickhouseClient } from "~/services/clickhouseInstance.server";
55
import { requireUserId } from "~/services/session.server";
66
import { LogDetailPresenter } from "~/presenters/v3/LogDetailPresenter.server";
77
import { findProjectBySlug } from "~/models/project.server";
@@ -43,7 +43,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
4343

4444
const [traceId, spanId, , startTime] = parts;
4545

46-
const presenter = new LogDetailPresenter($replica, clickhouseClient);
46+
const presenter = new LogDetailPresenter($replica, logsClickhouseClient);
4747

4848
let result;
4949
try {

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.logs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { findProjectBySlug } from "~/models/project.server";
66
import { findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
77
import { LogsListPresenter, type LogLevel, LogsListOptionsSchema } from "~/presenters/v3/LogsListPresenter.server";
88
import { $replica } from "~/db.server";
9-
import { clickhouseClient } from "~/services/clickhouseInstance.server";
9+
import { logsClickhouseClient } from "~/services/clickhouseInstance.server";
1010
import { getCurrentPlan } from "~/services/platform.v3.server";
1111

1212
// Valid log levels for filtering
@@ -69,7 +69,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
6969
retentionLimitDays,
7070
}) as any; // Validated by LogsListOptionsSchema at runtime
7171

72-
const presenter = new LogsListPresenter($replica, clickhouseClient);
72+
const presenter = new LogsListPresenter($replica, logsClickhouseClient);
7373
const result = await presenter.call(project.organizationId, environment.id, options);
7474

7575
return json({

apps/webapp/app/services/clickhouseInstance.server.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,60 @@ function initializeClickhouseClient() {
5353
return clickhouse;
5454
}
5555

56+
export const logsClickhouseClient = singleton(
57+
"logsClickhouseClient",
58+
initializeLogsClickhouseClient
59+
);
60+
61+
function initializeLogsClickhouseClient() {
62+
if (!env.LOGS_CLICKHOUSE_URL) {
63+
throw new Error("LOGS_CLICKHOUSE_URL is not set");
64+
}
65+
66+
const url = new URL(env.LOGS_CLICKHOUSE_URL);
67+
68+
// Remove secure param
69+
url.searchParams.delete("secure");
70+
71+
// Build logs query settings from environment variables
72+
const logsQuerySettings = {
73+
list: {
74+
max_memory_usage: env.CLICKHOUSE_LOGS_LIST_MAX_MEMORY_USAGE.toString(),
75+
max_bytes_before_external_sort:
76+
env.CLICKHOUSE_LOGS_LIST_MAX_BYTES_BEFORE_EXTERNAL_SORT.toString(),
77+
max_threads: env.CLICKHOUSE_LOGS_LIST_MAX_THREADS,
78+
...(env.CLICKHOUSE_LOGS_LIST_MAX_ROWS_TO_READ && {
79+
max_rows_to_read: env.CLICKHOUSE_LOGS_LIST_MAX_ROWS_TO_READ.toString(),
80+
}),
81+
...(env.CLICKHOUSE_LOGS_LIST_MAX_EXECUTION_TIME && {
82+
max_execution_time: env.CLICKHOUSE_LOGS_LIST_MAX_EXECUTION_TIME,
83+
}),
84+
},
85+
detail: {
86+
max_memory_usage: env.CLICKHOUSE_LOGS_DETAIL_MAX_MEMORY_USAGE.toString(),
87+
max_threads: env.CLICKHOUSE_LOGS_DETAIL_MAX_THREADS,
88+
...(env.CLICKHOUSE_LOGS_DETAIL_MAX_EXECUTION_TIME && {
89+
max_execution_time: env.CLICKHOUSE_LOGS_DETAIL_MAX_EXECUTION_TIME,
90+
}),
91+
},
92+
};
93+
94+
return new ClickHouse({
95+
url: url.toString(),
96+
name: "logs-clickhouse",
97+
keepAlive: {
98+
enabled: env.CLICKHOUSE_KEEP_ALIVE_ENABLED === "1",
99+
idleSocketTtl: env.CLICKHOUSE_KEEP_ALIVE_IDLE_SOCKET_TTL_MS,
100+
},
101+
logLevel: env.CLICKHOUSE_LOG_LEVEL,
102+
compression: {
103+
request: true,
104+
},
105+
maxOpenConnections: env.CLICKHOUSE_MAX_OPEN_CONNECTIONS,
106+
logsQuerySettings,
107+
});
108+
}
109+
56110
export const queryClickhouseClient = singleton(
57111
"queryClickhouseClient",
58112
initializeQueryClickhouseClient

0 commit comments

Comments
 (0)