Skip to content

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

chore: group events on S3 by clickhouse entity (#3733)

Steffen Schmitze443730e667e
diff --git a/packages/shared/src/server/clickhouse.ts b/packages/shared/src/server/clickhouse.ts
index b1ad3fb575..9a103d36c0 100644
--- a/packages/shared/src/server/clickhouse.ts
+++ b/packages/shared/src/server/clickhouse.ts
@@ -1,7 +1,8 @@
import { createClient } from "@clickhouse/client";
import { env } from "../env";
-import { eventTypes, IngestionEventType } from "./ingestion/types";
+import { eventTypes } from "./ingestion/types";
+import { LangfuseNotFoundError } from "../errors";
export enum ClickhouseEntityType {
Trace = "trace",
@@ -11,9 +12,9 @@ export enum ClickhouseEntityType {
}
export function getClickhouseEntityType(
- event: IngestionEventType,
+ eventType: string,
): ClickhouseEntityType {
- switch (event.type) {
+ switch (eventType) {
case eventTypes.TRACE_CREATE:
return ClickhouseEntityType.Trace;
case eventTypes.OBSERVATION_CREATE:
@@ -28,6 +29,8 @@ export function getClickhouseEntityType(
return ClickhouseEntityType.Score;
case eventTypes.SDK_LOG:
return ClickhouseEntityType.SdkLog;
+ default:
+ throw new LangfuseNotFoundError(`Unknown event type: ${eventType}`);
}
}
diff --git a/web/src/pages/api/public/ingestion.ts b/web/src/pages/api/public/ingestion.ts
index f797f0384f..da94312272 100644
--- a/web/src/pages/api/public/ingestion.ts
+++ b/web/src/pages/api/public/ingestion.ts
@@ -19,6 +19,7 @@ import {
type AuthHeaderValidVerificationResult,
type LegacyIngestionEventType,
type IngestionEventType,
+ getClickhouseEntityType,
} from "@langfuse/shared/src/server";
import { telemetry } from "@/src/features/telemetry";
import { jsonSchema } from "@langfuse/shared";
@@ -228,13 +229,12 @@ export default async function handler(
// In this case, we upload the full batch into the Redis queue.
const results = await Promise.allSettled(
sortedBatch.map(async (event) => {
- const eventName = event.type.split("-").shift();
// We upload the event in an array to the S3 bucket.
// This should allow us to eventually batch events together and increase cost-efficiency through
// reduced write operations.
return event.type !== eventTypes.SDK_LOG
? s3Client.uploadJson(
- `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${authCheck.scope.projectId}/${eventName}/${event.body.id}/${event.id}.json`,
+ `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${authCheck.scope.projectId}/${getClickhouseEntityType(event.type)}/${event.body.id}/${event.id}.json`,
[event],
)
: Promise.resolve();
diff --git a/worker/src/queues/ingestionQueue.ts b/worker/src/queues/ingestionQueue.ts
index 395355d509..6b40224a27 100644
--- a/worker/src/queues/ingestionQueue.ts
+++ b/worker/src/queues/ingestionQueue.ts
@@ -56,10 +56,6 @@ export const ingestionQueueProcessor: Processor = async (
const s3Client = getS3StorageServiceClient(
env.LANGFUSE_S3_EVENT_UPLOAD_BUCKET,
);
- const eventName = job.data.payload.data.type.split("-").shift();
- if (!eventName) {
- throw new Error("Event name not found");
- }
logger.info("Processing ingestion event", {
projectId: job.data.payload.authCheck.scope.projectId,
@@ -68,7 +64,7 @@ export const ingestionQueueProcessor: Processor = async (
// Download all events from folder into a local array
const eventFiles = await s3Client.listFiles(
- `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${eventName}/${job.data.payload.data.eventBodyId}/`,
+ `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${getClickhouseEntityType(job.data.payload.data.type)}/${job.data.payload.data.eventBodyId}/`,
);
const events: IngestionEventType[] = (
@@ -110,7 +106,7 @@ export const ingestionQueueProcessor: Processor = async (
ClickhouseWriter.getInstance(),
clickhouseClient,
).mergeAndWrite(
- getClickhouseEntityType(events[0]),
+ getClickhouseEntityType(events[0].type),
job.data.payload.authCheck.scope.projectId,
job.data.payload.data.eventBodyId,
events,
diff --git a/worker/src/queues/legacyIngestionQueue.ts b/worker/src/queues/legacyIngestionQueue.ts
index e9b963592a..95ec1b0808 100644
--- a/worker/src/queues/legacyIngestionQueue.ts
+++ b/worker/src/queues/legacyIngestionQueue.ts
@@ -12,6 +12,7 @@ import {
S3StorageService,
ingestionBatchEvent,
ingestionEvent,
+ getClickhouseEntityType,
} from "@langfuse/shared/src/server";
import {
@@ -60,9 +61,8 @@ export const legacyIngestionQueueProcessor: Processor = async (
ingestionEvents = (
await Promise.all(
job.data.payload.data.map(async (record) => {
- const eventName = record.type.split("-").shift();
const file = await s3Client.download(
- `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${eventName}/${record.eventBodyId}/${record.eventId}.json`,
+ `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}${job.data.payload.authCheck.scope.projectId}/${getClickhouseEntityType(record.type)}/${record.eventBodyId}/${record.eventId}.json`,
);
const parsedFile = JSON.parse(file);
const parsed = ingestionBatchEvent.safeParse(parsedFile);