Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: implement preflight checks for delete operations in events, observations, and traces (#11723)

Valery Meleshkin7259baff17e2
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index b7c2654b0b..728beb7525 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -1828,17 +1828,56 @@ export const deleteEventsByTraceIds = async (
projectId: string,
traceIds: string[],
) => {
- const query = `
- DELETE FROM events
- WHERE project_id = {projectId: String}
- AND trace_id IN ({traceIds: Array(String)});
- `;
- await commandClickhouse({
- query,
+ const preflight = await queryClickhouse<{
+ min_ts: string;
+ max_ts: string;
+ cnt: string;
+ }>({
+ query: `
+ SELECT
+ min(start_time) - INTERVAL 1 HOUR as min_ts,
+ max(start_time) + INTERVAL 1 HOUR as max_ts,
+ count(*) as cnt
+ FROM events
+ WHERE project_id = {projectId: String} AND trace_id IN ({traceIds: Array(String)})
+ `,
params: { projectId, traceIds },
clickhouseConfigs: {
request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
},
+ tags: {
+ feature: "tracing",
+ type: "events",
+ kind: "delete-preflight",
+ projectId,
+ },
+ });
+
+ const count = Number(preflight[0]?.cnt ?? 0);
+ if (count === 0) {
+ logger.info(
+ `deleteEventsByTraceIds: no rows found for project ${projectId}, skipping DELETE`,
+ );
+ return;
+ }
+
+ await commandClickhouse({
+ query: `
+ DELETE FROM events
+ WHERE project_id = {projectId: String}
+ AND trace_id IN ({traceIds: Array(String)})
+ AND start_time >= {minTs: String}::DateTime64(3)
+ AND start_time <= {maxTs: String}::DateTime64(3)
+ `,
+ params: {
+ projectId,
+ traceIds,
+ minTs: preflight[0].min_ts,
+ maxTs: preflight[0].max_ts,
+ },
+ clickhouseConfigs: {
+ request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
+ },
tags: {
feature: "tracing",
type: "events",
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index d633204e0d..5be7a7c3ec 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -1226,16 +1226,52 @@ export const deleteObservationsByTraceIds = async (
projectId: string,
traceIds: string[],
) => {
- const query = `
- DELETE FROM observations
- WHERE project_id = {projectId: String}
- AND trace_id IN ({traceIds: Array(String)});
- `;
+ const preflight = await queryClickhouse<{
+ min_ts: string;
+ max_ts: string;
+ cnt: string;
+ }>({
+ query: `
+ SELECT
+ min(start_time) - INTERVAL 1 HOUR as min_ts,
+ max(start_time) + INTERVAL 1 HOUR as max_ts,
+ count(*) as cnt
+ FROM observations
+ WHERE project_id = {projectId: String} AND trace_id IN ({traceIds: Array(String)})
+ `,
+ params: { projectId, traceIds },
+ clickhouseConfigs: {
+ request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
+ },
+ tags: {
+ feature: "tracing",
+ type: "observation",
+ kind: "delete-preflight",
+ projectId,
+ },
+ });
+
+ const count = Number(preflight[0]?.cnt ?? 0);
+ if (count === 0) {
+ logger.info(
+ `deleteObservationsByTraceIds: no rows found for project ${projectId}, skipping DELETE`,
+ );
+ return;
+ }
+
await commandClickhouse({
- query: query,
+ query: `
+ DELETE FROM observations
+ WHERE project_id = {projectId: String}
+ AND trace_id IN ({traceIds: Array(String)})
+ AND start_time >= {minTs: String}::DateTime64(3)
+ AND start_time <= {maxTs: String}::DateTime64(3)
+ `,
params: {
projectId,
traceIds,
+ minTs: preflight[0].min_ts,
+ maxTs: preflight[0].max_ts,
},
clickhouseConfigs: {
request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
diff --git a/packages/shared/src/server/repositories/traces.ts b/packages/shared/src/server/repositories/traces.ts
index f9059f0aea..2ac82f0b8c 100644
--- a/packages/shared/src/server/repositories/traces.ts
+++ b/packages/shared/src/server/repositories/traces.ts
@@ -34,6 +34,7 @@ import { recordDistribution } from "../instrumentation";
import type { AnalyticsTraceEvent } from "../analytics-integrations/types";
import { measureAndReturn } from "../clickhouse/measureAndReturn";
import { DEFAULT_RENDERING_PROPS, RenderingProps } from "../utils/rendering";
+import { logger } from "../logger";
/**
* Checks if trace exists in clickhouse.
@@ -880,17 +881,51 @@ export const deleteTraces = async (projectId: string, traceIds: string[]) => {
},
},
fn: async (input) => {
- const query = `
- DELETE FROM traces
- WHERE project_id = {projectId: String}
- AND id IN ({traceIds: Array(String)});
- `;
- await commandClickhouse({
- query: query,
+ // Pre-flight query with time bounds computed
+ const preflight = await queryClickhouse<{
+ min_ts: string;
+ max_ts: string;
+ cnt: string;
+ }>({
+ query: `
+ SELECT
+ min(timestamp) - INTERVAL 1 HOUR as min_ts,
+ max(timestamp) + INTERVAL 1 HOUR as max_ts,
+ count(*) as cnt
+ FROM traces
+ WHERE project_id = {projectId: String} AND id IN ({traceIds: Array(String)})
+ `,
params: input.params,
clickhouseConfigs: {
request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
},
+ tags: { ...input.tags, kind: "delete-preflight" },
+ });
+
+ const count = Number(preflight[0]?.cnt ?? 0);
+ if (count === 0) {
+ logger.info(
+ `deleteTraces: no rows found for project ${projectId}, skipping DELETE`,
+ );
+ return;
+ }
+
+ await commandClickhouse({
+ query: `
+ DELETE FROM traces
+ WHERE project_id = {projectId: String}
+ AND id IN ({traceIds: Array(String)})
+ AND timestamp >= {minTs: String}::DateTime64(3)
+ AND timestamp <= {maxTs: String}::DateTime64(3)
+ `,
+ params: {
+ ...input.params,
+ minTs: preflight[0].min_ts,
+ maxTs: preflight[0].max_ts,
+ },
+ clickhouseConfigs: {
+ request_timeout: env.LANGFUSE_CLICKHOUSE_DELETION_TIMEOUT_MS,
+ },
tags: input.tags,
});
},