Skip to content

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

feat: add is_deleted to clickhouse (#3887)

Max Deichmann5499da8cc3d4
diff --git a/packages/shared/clickhouse/migrations/0001_traces.up.sql b/packages/shared/clickhouse/migrations/0001_traces.up.sql
index bbbcf2120c..2a4d21d892 100644
--- a/packages/shared/clickhouse/migrations/0001_traces.up.sql
+++ b/packages/shared/clickhouse/migrations/0001_traces.up.sql
@@ -14,12 +14,13 @@ CREATE TABLE traces (
`output` Nullable(String) CODEC(ZSTD(1)),
`session_id` Nullable(String),
`created_at` DateTime64(3) DEFAULT now(),
- `updated_at` DateTime64(3) DEFAULT now(),
- event_ts DateTime64(3),
+ updated_at DateTime64(3) DEFAULT now(),
+ `event_ts` DateTime64(3),
+ `is_deleted` UInt8,
INDEX idx_id id TYPE bloom_filter(0.001) GRANULARITY 1,
INDEX idx_res_metadata_key mapKeys(metadata) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_res_metadata_value mapValues(metadata) TYPE bloom_filter(0.01) GRANULARITY 1
-) ENGINE = ReplacingMergeTree Partition by toYYYYMM(timestamp)
+) ENGINE = ReplacingMergeTree(event_ts, is_deleted) Partition by toYYYYMM(timestamp)
PRIMARY KEY (
project_id,
toDate(timestamp)
diff --git a/packages/shared/clickhouse/migrations/0002_observations.up.sql b/packages/shared/clickhouse/migrations/0002_observations.up.sql
index 73ae2b702e..d03a558dfb 100644
--- a/packages/shared/clickhouse/migrations/0002_observations.up.sql
+++ b/packages/shared/clickhouse/migrations/0002_observations.up.sql
@@ -36,12 +36,13 @@ CREATE TABLE observations (
`created_at` DateTime64(3) DEFAULT now(),
`updated_at` DateTime64(3) DEFAULT now(),
event_ts DateTime64(3),
+ is_deleted UInt8,
INDEX idx_id id TYPE bloom_filter() GRANULARITY 1,
INDEX idx_trace_id trace_id TYPE bloom_filter() GRANULARITY 1,
INDEX idx_project_id project_id TYPE bloom_filter() GRANULARITY 1,
INDEX idx_res_metadata_key mapKeys(metadata) TYPE bloom_filter() GRANULARITY 1,
INDEX idx_res_metadata_value mapValues(metadata) TYPE bloom_filter() GRANULARITY 1
-) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)
+) ENGINE = ReplacingMergeTree(event_ts, is_deleted) Partition by toYYYYMM(start_time)
PRIMARY KEY (
project_id,
`type`,
diff --git a/packages/shared/clickhouse/migrations/0003_scores.up.sql b/packages/shared/clickhouse/migrations/0003_scores.up.sql
index 0a8d3e3722..487da20cb0 100644
--- a/packages/shared/clickhouse/migrations/0003_scores.up.sql
+++ b/packages/shared/clickhouse/migrations/0003_scores.up.sql
@@ -15,9 +15,10 @@ CREATE TABLE scores (
`created_at` DateTime64(3) DEFAULT now(),
`updated_at` DateTime64(3) DEFAULT now(),
event_ts DateTime64(3),
+ `is_deleted` UInt8,
INDEX idx_id id TYPE bloom_filter(0.001) GRANULARITY 1,
INDEX idx_project_trace_observation (project_id, trace_id, observation_id) TYPE bloom_filter(0.001) GRANULARITY 1
-) ENGINE = ReplacingMergeTree Partition by toYYYYMM(timestamp)
+) ENGINE = ReplacingMergeTree(event_ts, is_deleted) Partition by toYYYYMM(timestamp)
PRIMARY KEY (
project_id,
toDate(timestamp),
diff --git a/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql b/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql
index ae61f4df3f..f44620cb79 100644
--- a/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql
+++ b/packages/shared/clickhouse/migrations/0004_observations_wide.up.sql
@@ -51,8 +51,9 @@ CREATE TABLE observations_wide
trace_bookmarked Bool,
trace_tags Array(String),
trace_session_id Nullable(String),
- trace_event_ts DateTime64(3)
-) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)
+ trace_event_ts DateTime64(3),
+ is_deleted UInt8,
+) ENGINE = ReplacingMergeTree(event_ts, is_deleted) Partition by toYYYYMM(start_time)
PRIMARY KEY (
project_id,
`type`,
diff --git a/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql b/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql
index 2486a4511d..99a993df21 100644
--- a/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql
+++ b/packages/shared/clickhouse/migrations/0005_traces_wide.up.sql
@@ -51,8 +51,9 @@ CREATE TABLE traces_wide
trace_bookmarked Bool,
trace_tags Array(String),
trace_session_id Nullable(String),
- trace_event_ts DateTime64(3)
-) ENGINE = ReplacingMergeTree Partition by toYYYYMM(start_time)
+ trace_event_ts DateTime64(3),
+ is_deleted UInt8
+) ENGINE = ReplacingMergeTree(event_ts, is_deleted) Partition by toYYYYMM(start_time)
PRIMARY KEY (
project_id,
`type`,
diff --git a/packages/shared/src/server/definitions.ts b/packages/shared/src/server/definitions.ts
index cab3829d3a..4b38d1cf36 100644
--- a/packages/shared/src/server/definitions.ts
+++ b/packages/shared/src/server/definitions.ts
@@ -39,6 +39,7 @@ export const observationRecordBaseSchema = z.object({
prompt_id: z.string().nullish(),
prompt_name: z.string().nullish(),
prompt_version: z.number().nullish(),
+ is_deleted: z.number(),
});
export type ObservationRecordBaseType = z.infer<
typeof observationRecordBaseSchema
@@ -84,6 +85,7 @@ export const traceRecordBaseSchema = z.object({
input: z.string().nullish(),
output: z.string().nullish(),
session_id: z.string().nullish(),
+ is_deleted: z.number(),
});
export type TraceRecordBaseType = z.infer<typeof traceRecordBaseSchema>;
@@ -116,6 +118,7 @@ export const scoreRecordBaseSchema = z.object({
config_id: z.string().nullish(),
data_type: z.enum(["NUMERIC", "CATEGORICAL", "BOOLEAN"]).nullish(),
string_value: z.string().nullish(),
+ is_deleted: z.number(),
});
export type ScoreRecordBaseType = z.infer<typeof scoreRecordBaseSchema>;
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 2d6efde7e5..3d8083018a 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -140,6 +140,7 @@ export class IngestionService {
created_at: Date.now(),
updated_at: Date.now(),
event_ts: new Date(scoreEvent.timestamp).getTime(),
+ is_deleted: 0,
};
});
@@ -242,7 +243,6 @@ export class IngestionService {
observationRecords,
clickhouseObservationRecord,
});
-
// Backward compat: create wrapper trace for SDK < 2.0.0 events that do not have a traceId
if (!finalObservationRecord.trace_id) {
const traceId = randomUUID();
@@ -257,6 +257,7 @@ export class IngestionService {
bookmarked: false,
public: false,
event_ts: Date.now(),
+ is_deleted: 0,
};
this.clickHouseWriter.addToQueue(TableName.Traces, wrapperTraceRecord);
@@ -284,7 +285,12 @@ export class IngestionService {
immutableEntityKeys[TableName.Scores]
);
- return scoreRecordInsertSchema.parse(mergedRecord);
+ const parsedRecord = scoreRecordInsertSchema.parse(mergedRecord);
+ parsedRecord.event_ts = Math.max(
+ ...scoreRecords.map((r) => r.event_ts),
+ clickhouseScoreRecord?.event_ts ?? -Infinity
+ );
+ return parsedRecord;
}
private async mergeTraceRecords(params: {
@@ -302,7 +308,12 @@ export class IngestionService {
immutableEntityKeys[TableName.Traces]
);
- return traceRecordInsertSchema.parse(mergedRecord);
+ const parsedRecord = traceRecordInsertSchema.parse(mergedRecord);
+ parsedRecord.event_ts = Math.max(
+ ...traceRecords.map((r) => r.event_ts),
+ clickhouseTraceRecord?.event_ts ?? -Infinity
+ );
+ return parsedRecord;
}
private async mergeObservationRecords(params: {
@@ -338,7 +349,14 @@ export class IngestionService {
observationRecord: parsedObservationRecord,
});
- return { ...parsedObservationRecord, ...generationUsage };
+ return {
+ ...parsedObservationRecord,
+ ...generationUsage,
+ event_ts: Math.max(
+ ...observationRecords.map((r) => r.event_ts),
+ clickhouseObservationRecord?.event_ts ?? -Infinity
+ ),
+ };
}
private mergeRecords<T extends InsertRecord>(
@@ -631,6 +649,7 @@ export class IngestionService {
created_at: Date.now(),
updated_at: Date.now(),
event_ts: new Date(trace.timestamp).getTime(),
+ is_deleted: 0,
};
return traceRecord;
@@ -739,6 +758,7 @@ export class IngestionService {
created_at: Date.now(),
updated_at: Date.now(),
event_ts: new Date(obs.timestamp).getTime(),
+ is_deleted: 0,
};
return observationRecord;