Skip to content

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

chore: set created_at in ClickHouse based on min S3 timestamp (#4955)

Steffen Schmitzfe70d92211c2
diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.ts
index bea6d90e6e..4b4b5932d1 100644
--- a/packages/shared/src/server/services/StorageService.ts
+++ b/packages/shared/src/server/services/StorageService.ts
@@ -30,7 +30,7 @@ export interface StorageService {
download(path: string): Promise<string>;
- listFiles(prefix: string): Promise<string[]>;
+ listFiles(prefix: string): Promise<{ file: string; createdAt: Date }[]>;
getSignedUrl(
fileName: string,
@@ -201,7 +201,9 @@ class AzureBlobStorageService implements StorageService {
}
}
- public async listFiles(prefix: string): Promise<string[]> {
+ public async listFiles(
+ prefix: string,
+ ): Promise<{ file: string; createdAt: Date }[]> {
try {
await this.createContainerIfNotExists();
@@ -209,7 +211,10 @@ class AzureBlobStorageService implements StorageService {
const files = [];
for await (const blob of result) {
if (blob.name.startsWith(prefix)) {
- files.push(blob.name);
+ files.push({
+ file: blob.name,
+ createdAt: blob?.properties?.createdOn ?? new Date(),
+ });
}
}
return files;
@@ -364,7 +369,9 @@ class S3StorageService implements StorageService {
}
}
- public async listFiles(prefix: string): Promise<string[]> {
+ public async listFiles(
+ prefix: string,
+ ): Promise<{ file: string; createdAt: Date }[]> {
const listCommand = new ListObjectsV2Command({
Bucket: this.bucketName,
Prefix: prefix,
@@ -373,7 +380,11 @@ class S3StorageService implements StorageService {
try {
const response = await this.client.send(listCommand);
return (
- response.Contents?.flatMap((file) => (file.Key ? [file.Key] : [])) ?? []
+ response.Contents?.flatMap((file) =>
+ file.Key
+ ? [{ file: file.Key, createdAt: file.LastModified ?? new Date() }]
+ : [],
+ ) ?? []
);
} catch (err) {
logger.error(`Failed to list files from S3 ${prefix}`, err);
diff --git a/worker/src/queues/ingestionQueue.ts b/worker/src/queues/ingestionQueue.ts
index c9f56a1b10..1601da2153 100644
--- a/worker/src/queues/ingestionQueue.ts
+++ b/worker/src/queues/ingestionQueue.ts
@@ -97,10 +97,15 @@ export const ingestionQueueProcessorBuilder = (
`${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${getClickhouseEntityType(job.data.payload.data.type)}/${job.data.payload.data.eventBodyId}/`,
);
+ const firstS3WriteTime =
+ eventFiles
+ .map((fileRef) => fileRef.createdAt)
+ .sort()
+ .shift() ?? new Date();
const events: IngestionEventType[] = (
await Promise.all(
- eventFiles.map(async (key) => {
- const file = await s3Client.download(key);
+ eventFiles.map(async (fileRef) => {
+ const file = await s3Client.download(fileRef.file);
const parsedFile = JSON.parse(file);
return Array.isArray(parsedFile) ? parsedFile : [parsedFile];
}),
@@ -126,6 +131,7 @@ export const ingestionQueueProcessorBuilder = (
getClickhouseEntityType(events[0].type),
job.data.payload.authCheck.scope.projectId,
job.data.payload.data.eventBodyId,
+ firstS3WriteTime,
events,
);
} catch (e) {
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index f152e6ae90..acb87722b9 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -84,6 +84,7 @@ export class IngestionService {
eventType: IngestionEntityTypes,
projectId: string,
eventBodyId: string,
+ createdAtTimestamp: Date,
events: IngestionEventType[],
): Promise<void> {
logger.debug(
@@ -95,18 +96,21 @@ export class IngestionService {
return await this.processTraceEventList({
projectId,
entityId: eventBodyId,
+ createdAtTimestamp,
traceEventList: events as TraceEventType[],
});
case "observation":
return await this.processObservationEventList({
projectId,
entityId: eventBodyId,
+ createdAtTimestamp,
observationEventList: events as ObservationEvent[],
});
case "score": {
return await this.processScoreEventList({
projectId,
entityId: eventBodyId,
+ createdAtTimestamp,
scoreEventList: events as ScoreEventType[],
});
}
@@ -116,9 +120,10 @@ export class IngestionService {
private async processScoreEventList(params: {
projectId: string;
entityId: string;
+ createdAtTimestamp: Date;
scoreEventList: ScoreEventType[];
}) {
- const { projectId, entityId, scoreEventList } = params;
+ const { projectId, entityId, createdAtTimestamp, scoreEventList } = params;
if (scoreEventList.length === 0) return;
const timeSortedEvents =
@@ -186,6 +191,8 @@ export class IngestionService {
clickhouseScoreRecord,
scoreRecords,
});
+ finalScoreRecord.created_at =
+ clickhouseScoreRecord?.created_at ?? createdAtTimestamp.getTime();
this.clickHouseWriter.addToQueue(TableName.Scores, finalScoreRecord);
}
@@ -193,9 +200,10 @@ export class IngestionService {
private async processTraceEventList(params: {
projectId: string;
entityId: string;
+ createdAtTimestamp: Date;
traceEventList: TraceEventType[];
}) {
- const { projectId, entityId, traceEventList } = params;
+ const { projectId, entityId, createdAtTimestamp, traceEventList } = params;
if (traceEventList.length === 0) return;
const timeSortedEvents =
@@ -239,6 +247,8 @@ export class IngestionService {
clickhouseTraceRecord,
traceRecords,
});
+ finalTraceRecord.created_at =
+ clickhouseTraceRecord?.created_at ?? createdAtTimestamp.getTime();
// If the trace has a sessionId, we upsert the corresponding session into Postgres.
if (finalTraceRecord.session_id) {
@@ -292,9 +302,11 @@ export class IngestionService {
private async processObservationEventList(params: {
projectId: string;
entityId: string;
+ createdAtTimestamp: Date;
observationEventList: ObservationEvent[];
}) {
- const { projectId, entityId, observationEventList } = params;
+ const { projectId, entityId, createdAtTimestamp, observationEventList } =
+ params;
if (observationEventList.length === 0) return;
const timeSortedEvents =
@@ -346,6 +358,8 @@ export class IngestionService {
observationRecords,
clickhouseObservationRecord,
});
+ finalObservationRecord.created_at =
+ clickhouseObservationRecord?.created_at ?? createdAtTimestamp.getTime();
// Backward compat: create wrapper trace for SDK < 2.0.0 events that do not have a traceId
if (!finalObservationRecord.trace_id) {
diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
index 7c56592483..9a5c002f6b 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
@@ -73,6 +73,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(timestamp),
traceEventList: eventList,
});
await clickhouseWriter.flushAll(true);
@@ -442,21 +443,25 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: spanId,
+ createdAtTimestamp: new Date(),
observationEventList: spanEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
ingestionService.processScoreEventList({
projectId,
entityId: scoreId,
+ createdAtTimestamp: new Date(),
scoreEventList,
}),
]);
@@ -778,11 +783,13 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
]);
@@ -918,26 +925,31 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: spanId,
+ createdAtTimestamp: new Date(),
observationEventList: spanEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: eventId,
+ createdAtTimestamp: new Date(),
observationEventList: eventEventList,
}),
ingestionService.processScoreEventList({
projectId,
entityId: scoreId,
+ createdAtTimestamp: new Date(),
scoreEventList,
}),
]);
@@ -1009,6 +1021,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList: traceEventList1,
});
@@ -1032,6 +1045,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList: traceEventList2,
});
@@ -1088,6 +1102,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
});
@@ -1135,6 +1150,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
});
@@ -1190,6 +1206,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processScoreEventList({
projectId,
entityId: scoreId,
+ createdAtTimestamp: new Date(),
scoreEventList,
});
@@ -1312,6 +1329,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: observationId,
+ createdAtTimestamp: new Date(),
observationEventList,
});
@@ -1451,6 +1469,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: observationId,
+ createdAtTimestamp: new Date(),
observationEventList,
});
@@ -1504,6 +1523,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: observationId,
+ createdAtTimestamp: new Date(),
observationEventList: observationEventList1,
});
await clickhouseWriter.flushAll(true);
@@ -1525,6 +1545,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: observationId,
+ createdAtTimestamp: new Date(),
observationEventList: observationEventList2,
});
await clickhouseWriter.flushAll(true);
@@ -1574,6 +1595,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
});
@@ -1635,11 +1657,13 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList1,
}),
]);
@@ -1680,6 +1704,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList2,
});
@@ -1771,11 +1796,13 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
]);
@@ -1871,11 +1898,13 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
]);
@@ -1939,6 +1968,7 @@ describe("Ingestion end-to-end tests", () => {
await ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
});
@@ -2052,11 +2082,13 @@ describe("Ingestion end-to-end tests", () => {
ingestionService.processTraceEventList({
projectId,
entityId: traceId,
+ createdAtTimestamp: new Date(),
traceEventList,
}),
ingestionService.processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: generationEventList,
}),
]);
diff --git a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
index e890be3314..6ea88a680f 100644
--- a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
+++ b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
@@ -427,6 +427,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -507,6 +508,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -585,6 +587,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -665,6 +668,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -748,6 +752,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -837,6 +842,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -921,6 +927,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -1002,6 +1009,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -1088,6 +1096,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();
@@ -1159,6 +1168,7 @@ describe("Token Cost Calculation", () => {
await (mockIngestionService as any).processObservationEventList({
projectId,
entityId: generationId,
+ createdAtTimestamp: new Date(),
observationEventList: events,
});
expect(mockAddToClickhouseWriter).toHaveBeenCalled();