Skip to content

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

feat: script to replay missing events (#4966)

Max Deichmanne7e6cf1b9981
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2d2e467eb6..1f65fa0104 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -849,6 +849,9 @@ importers:
cors:
specifier: ^2.8.5
version: 2.8.5
+ csv-parse:
+ specifier: ^5.6.0
+ version: 5.6.0
dd-trace:
specifier: ^5.23.1
version: 5.23.1
@@ -6173,6 +6176,9 @@ packages:
resolution: {integrity: sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==}
+ resolution: {integrity: sha512-l3nz3euub2QMg5ouu5U09Ew9Wf6/wQ8I++ch1loQ0ljmzhmfZYrH9fflS22i/PQEvsPvxCwxgz5q7UB8K1JO4Q==}
+
resolution: {integrity: sha512-wgQlVIUJF13Quxiv5e1gstZ08rnZj2XaLHGoFMYXz7SkNfCDOOteKBE6SYRfA9WxxI/iBc3ajfDoc6hb/MRAHQ==}
peerDependencies:
@@ -18610,6 +18616,8 @@ snapshots:
+
dependencies:
cose-base: 1.0.3
diff --git a/worker/package.json b/worker/package.json
index 33a2c42268..d0b2c1365d 100644
--- a/worker/package.json
+++ b/worker/package.json
@@ -14,7 +14,8 @@
"build": "tsc",
"dev": "dotenv -e ../.env -- nodemon src/index.ts",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
- "lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix"
+ "lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
+ "refill-ingestion-events": "dotenv -e ../.env -- tsx src/scripts/refill-ingestion-events.ts"
},
"author": "[email protected]",
"dependencies": {
@@ -38,6 +39,7 @@
"backoff": "^2.5.0",
"bullmq": "^5.12.10",
"cors": "^2.8.5",
+ "csv-parse": "^5.6.0",
"dd-trace": "^5.23.1",
"decimal.js": "^10.4.3",
"dotenv": "^16.4.5",
diff --git a/worker/src/scripts/refill-ingestion-events.ts b/worker/src/scripts/refill-ingestion-events.ts
new file mode 100644
index 0000000000..0148f53885
--- /dev/null
+++ b/worker/src/scripts/refill-ingestion-events.ts
@@ -0,0 +1,115 @@
+// This script can be used to manually refill ingestion events.
+// Execute with caution in production.
+
+import { readFileSync, writeFileSync } from "fs";
+import { parse } from "csv-parse/sync";
+import {
+ QueueJobs,
+ IngestionQueue,
+ eventTypes,
+} from "@langfuse/shared/src/server";
+import { logger } from "@langfuse/shared/src/server";
+import { redis } from "@langfuse/shared/src/server";
+
+const main = async () => {
+ // Read and parse CSV file
+ // Find events.csv in root directory
+ logger.info(`Current working directory: ${process.cwd()}`);
+ const fileContent = readFileSync("./../events.csv", "utf-8");
+
+ type CsvType = {
+ keys: String;
+ processed?: string;
+ };
+
+ const records: CsvType[] = parse(fileContent, {
+ columns: true,
+ skip_empty_lines: true,
+ });
+
+ // Store records in JSON file for backup
+ const jsonContent = JSON.stringify(records, null, 2);
+ writeFileSync("./../events.json", jsonContent);
+ logger.info("Stored records backup in events.json");
+
+ // Read from backup JSON file
+ const backupContent = readFileSync("./../events.json", "utf-8");
+ const jsonRecords = JSON.parse(backupContent) as CsvType[];
+
+ // Filter out already processed records
+ const unprocessedRecords = jsonRecords.filter(
+ (record) => record.processed !== "true",
+ );
+
+ console.log(`Found ${unprocessedRecords.length} unprocessed records`);
+
+ // Create queue connection
+ const ingestionQueue = IngestionQueue.getInstance();
+
+ // Process each record
+ const BATCH_SIZE = 50; // Process 100 events at a time
+
+ for (let i = 0; i < unprocessedRecords.length; i += BATCH_SIZE) {
+ const batch = unprocessedRecords.slice(i, i + BATCH_SIZE);
+ const events = batch.map((record) => {
+ const [projectId, type, eventBodyId, eventIdWithExt] =
+ record.keys.split("/");
+ const eventId = eventIdWithExt.replace(".json", "");
+
+ const eventType = (type +
+ "-create") as (typeof eventTypes)[keyof typeof eventTypes];
+
+ return {
+ name: QueueJobs.IngestionJob as const,
+ data: {
+ timestamp: new Date(),
+ id: eventId,
+ payload: {
+ data: {
+ type: eventType,
+ eventBodyId: eventBodyId,
+ },
+ authCheck: {
+ validKey: true as const,
+ scope: {
+ projectId: projectId,
+ accessLevel: "all" as const,
+ },
+ },
+ },
+ name: QueueJobs.IngestionJob as const,
+ },
+ };
+ });
+
+ // Add batch to queue
+ await ingestionQueue?.addBulk(events);
+
+ // Mark records as processed
+ // batch.forEach((record, index) => {
+ // const recordIndex = records.findIndex((r) => r.keys === record.keys);
+ // if (recordIndex !== -1) {
+ // records[recordIndex].processed = "true";
+ // }
+ // });
+
+ logger.info(`Processed batch of ${events.length} events`);
+ }
+
+ // Write updated records back to JSON
+ // const jsonString = JSON.stringify(records, null, 2);
+ // writeFileSync("./../events.json", jsonString);
+
+ logger.info("Done processing events");
+};
+
+if (require.main === module) {
+ main()
+ .catch((err) => {
+ console.error("Error running script:", err);
+ })
+ .finally(() => {
+ redis?.disconnect();
+ process.exit(0);
+ });
+}