🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore: decouple upload file from url signing in storage service (#8878)
Henrique Cadioli18deee643479
diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.tsindex 8ef324696e..6f30569840 100644--- a/packages/shared/src/server/services/StorageService.ts+++ b/packages/shared/src/server/services/StorageService.ts@@ -24,11 +24,18 @@ type UploadFile = {fileName: string;fileType: string;data: Readable | string;+};++type UploadWithSignedUrl = UploadFile & {expiresInSeconds: number;};export interface StorageService {- uploadFile(params: UploadFile): Promise<{ signedUrl: string }>; // eslint-disable-line no-unused-vars+ uploadFile(params: UploadFile): Promise<void>; // eslint-disable-line no-unused-vars++ uploadWithSignedUrl(+ params: UploadWithSignedUrl, // eslint-disable-line no-unused-vars+ ): Promise<{ signedUrl: string }>;uploadJson(path: string, body: Record<string, unknown>[]): Promise<void>; // eslint-disable-line no-unused-vars@@ -161,8 +168,8 @@ class AzureBlobStorageService implements StorageService {}}- public async uploadFile(params: UploadFile): Promise<{ signedUrl: string }> {- const { fileName, data, expiresInSeconds } = params;+ public async uploadFile(params: UploadFile): Promise<void> {+ const { fileName, data } = params;try {await this.createContainerIfNotExists();@@ -196,6 +203,23 @@ class AzureBlobStorageService implements StorageService {throw new Error("Unsupported data type. Must be Readable or string.");}+ return;+ } catch (err) {+ logger.error(+ `Failed to upload file to Azure Blob Storage ${fileName}`,+ err,+ );+ throw Error("Failed to upload file to Azure Blob Storage");+ }+ }++ public async uploadWithSignedUrl(+ params: UploadWithSignedUrl,+ ): Promise<{ signedUrl: string }> {+ const { fileName, data, fileType, expiresInSeconds } = params;+ try {+ await this.uploadFile({ fileName, data, fileType });+return {signedUrl: await this.getSignedUrl(fileName, expiresInSeconds, false),};@@ -454,8 +478,7 @@ class S3StorageService implements StorageService {fileName,fileType,data,- expiresInSeconds,- }: UploadFile): Promise<{ signedUrl: string }> {+ }: UploadFile): Promise<void> {try {await new Upload({client: this.client,@@ -467,6 +490,22 @@ class S3StorageService implements StorageService {}),}).done();+ return;+ } catch (err) {+ logger.error(`Failed to upload file to ${fileName}`, err);+ throw new Error(`Failed to upload to S3: ${err}`);+ }+ }++ public async uploadWithSignedUrl({+ fileName,+ fileType,+ data,+ expiresInSeconds,+ }: UploadWithSignedUrl): Promise<{ signedUrl: string }> {+ try {+ await this.uploadFile({ fileName, data, fileType });+const signedUrl = await this.getSignedUrl(fileName, expiresInSeconds);return { signedUrl };@@ -663,8 +702,7 @@ class GoogleCloudStorageService implements StorageService {fileName,fileType,data,- expiresInSeconds,- }: UploadFile): Promise<{ signedUrl: string }> {+ }: UploadFile): Promise<void> {try {const file = this.bucket.file(fileName);const options = {@@ -674,27 +712,18 @@ class GoogleCloudStorageService implements StorageService {if (typeof data === "string") {await file.save(data, options);- const signedUrl = await this.getSignedUrl(fileName, expiresInSeconds);- return { signedUrl };+ return;} else if (data instanceof Readable) {return new Promise((resolve, reject) => {const writeStream = file.createWriteStream(options);data.pipe(writeStream)- .on("error", (err) => {+ .on("error", (err: unknown) => {reject(err);})- .on("finish", async () => {- try {- const signedUrl = await this.getSignedUrl(- fileName,- expiresInSeconds,- );- resolve({ signedUrl });- } catch (err) {- reject(err);- }+ .on("finish", () => {+ resolve();});});} else {@@ -709,6 +738,25 @@ class GoogleCloudStorageService implements StorageService {}}+ public async uploadWithSignedUrl({+ fileName,+ fileType,+ data,+ expiresInSeconds,+ }: UploadWithSignedUrl): Promise<{ signedUrl: string }> {+ try {+ await this.uploadFile({ fileName, data, fileType });+ const signedUrl = await this.getSignedUrl(fileName, expiresInSeconds);+ return { signedUrl };+ } catch (err) {+ logger.error(+ `Failed to upload file to Google Cloud Storage ${fileName}`,+ err,+ );+ throw new Error("Failed to upload to Google Cloud Storage");+ }+ }+public async uploadJson(path: string,body: Record<string, unknown>[],diff --git a/web/src/features/blobstorage-integration/blobstorage-integration-router.ts b/web/src/features/blobstorage-integration/blobstorage-integration-router.tsindex cc4b180470..7bd95482e1 100644--- a/web/src/features/blobstorage-integration/blobstorage-integration-router.ts+++ b/web/src/features/blobstorage-integration/blobstorage-integration-router.ts@@ -356,7 +356,7 @@ Configuration: ${type} storageThis file can be safely deleted.`;// Upload the test file- const result = await storageService.uploadFile({+ const result = await storageService.uploadWithSignedUrl({fileName: testFileName,fileType: "text/plain",data: testContent,diff --git a/worker/src/__tests__/dataRetentionProcessing.test.ts b/worker/src/__tests__/dataRetentionProcessing.test.tsindex 0c75c9c9bc..6f30eff3f9 100644--- a/worker/src/__tests__/dataRetentionProcessing.test.ts+++ b/worker/src/__tests__/dataRetentionProcessing.test.ts@@ -41,12 +41,11 @@ describe("DataRetentionProcessingJob", () => {const fileName = `${baseId}.json`;const fileType = "application/json";const data = JSON.stringify({ hello: "world" });- const expiresInSeconds = 3600;+await storageService.uploadFile({fileName,fileType,data,- expiresInSeconds,});await clickhouseClient().insert({@@ -90,12 +89,11 @@ describe("DataRetentionProcessingJob", () => {const fileName = `${baseId}.json`;const fileType = "application/json";const data = JSON.stringify({ hello: "world" });- const expiresInSeconds = 3600;+await storageService.uploadFile({fileName,fileType,data,- expiresInSeconds,});await clickhouseClient().insert({@@ -138,12 +136,11 @@ describe("DataRetentionProcessingJob", () => {const fileName = `${randomUUID()}.txt`;const fileType = "text/plain";const data = "Hello, world!";- const expiresInSeconds = 3600;+await storageService.uploadFile({fileName,fileType,data,- expiresInSeconds,});const mediaId = randomUUID();@@ -196,12 +193,11 @@ describe("DataRetentionProcessingJob", () => {const fileName = `${randomUUID()}.txt`;const fileType = "text/plain";const data = "Hello, world!";- const expiresInSeconds = 3600;+await storageService.uploadFile({fileName,fileType,data,- expiresInSeconds,});const mediaId = randomUUID();diff --git a/worker/src/__tests__/projectDeletionProcessing.test.ts b/worker/src/__tests__/projectDeletionProcessing.test.tsindex 0acfd81f40..ee6760dbab 100644--- a/worker/src/__tests__/projectDeletionProcessing.test.ts+++ b/worker/src/__tests__/projectDeletionProcessing.test.ts@@ -204,12 +204,11 @@ describe("ProjectDeletionProcessingJob", () => {const fileName = `${randomUUID()}.txt`;const fileType = "text/plain";const data = "Hello, world!";- const expiresInSeconds = 3600;+await storageService.uploadFile({fileName,fileType,data,- expiresInSeconds,});const mediaId = randomUUID();diff --git a/worker/src/__tests__/scoreDeletion.test.ts b/worker/src/__tests__/scoreDeletion.test.tsindex 063b4e1df8..f2313f3d1f 100644--- a/worker/src/__tests__/scoreDeletion.test.ts+++ b/worker/src/__tests__/scoreDeletion.test.ts@@ -53,13 +53,12 @@ describe("score deletion", () => {const fileType = "application/json";const data = JSON.stringify({ hello: "world" });- const expiresInSeconds = 3600;+await Promise.all([eventStorageService.uploadFile({fileName: `${projectId}/score/${scoreId}-score.json`,fileType,data,- expiresInSeconds,}),]);diff --git a/worker/src/__tests__/storageservice.test.ts b/worker/src/__tests__/storageservice.test.tsindex 8c798b7de0..0b4a3a6bd7 100644--- a/worker/src/__tests__/storageservice.test.ts+++ b/worker/src/__tests__/storageservice.test.ts@@ -33,7 +33,7 @@ describe("StorageService", () => {});});- test("uploadFile should upload a file and return a signed URL", async () => {+ test("uploadWithSignedUrl should upload a file and return a signed URL", async () => {// Setupconst fileName = `${randomUUID()}.txt`;const fileType = "text/plain";@@ -41,7 +41,7 @@ describe("StorageService", () => {const expiresInSeconds = 3600;// When- const result = await storageService.uploadFile({+ const result = await storageService.uploadWithSignedUrl({fileName,fileType,data,@@ -112,7 +112,6 @@ describe("StorageService", () => {fileName,fileType,data,- expiresInSeconds,});// When@@ -138,7 +137,6 @@ describe("StorageService", () => {fileName,fileType,data,- expiresInSeconds,});// When@@ -197,7 +195,7 @@ describe("StorageService", () => {expect(signedUrl).not.toContain(env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT);});- test("uploadFile should return signed URL with external endpoint when configured", async () => {+ test("uploadWithSignedUrl should return signed URL with external endpoint when configured", async () => {// Setupconst fileName = `${randomUUID()}.txt`;const fileType = "text/plain";@@ -205,12 +203,14 @@ describe("StorageService", () => {const expiresInSeconds = 3600;// When- const result = await storageServiceWithExternalEndpoint.uploadFile({- fileName,- fileType,- data,- expiresInSeconds,- });+ const result = await storageServiceWithExternalEndpoint.uploadWithSignedUrl(+ {+ fileName,+ fileType,+ data,+ expiresInSeconds,+ },+ );// Thenexpect(result.signedUrl).toContain("external-endpoint.example.com");diff --git a/worker/src/__tests__/traceDeletion.test.ts b/worker/src/__tests__/traceDeletion.test.tsindex c45d96b416..d263ab7046 100644--- a/worker/src/__tests__/traceDeletion.test.ts+++ b/worker/src/__tests__/traceDeletion.test.ts@@ -92,19 +92,17 @@ describe("trace deletion", () => {const fileType = "text/plain";const data = "Hello, world!";- const expiresInSeconds = 3600;+await Promise.all([mediaStorageService.uploadFile({fileName: `${projectId}/trace-${traceId}.txt`,fileType,data,- expiresInSeconds,}),mediaStorageService.uploadFile({fileName: `${projectId}/observation-${observationId}.txt`,fileType,data,- expiresInSeconds,}),]);@@ -186,12 +184,11 @@ describe("trace deletion", () => {const fileType = "text/plain";const data = "Hello, world!";- const expiresInSeconds = 3600;+await mediaStorageService.uploadFile({fileName: `${projectId}/trace-${traceId1}.txt`,fileType,data,- expiresInSeconds,});const traceMediaId = randomUUID();@@ -275,25 +272,22 @@ describe("trace deletion", () => {const fileType = "application/json";const data = JSON.stringify({ hello: "world" });- const expiresInSeconds = 3600;+await Promise.all([eventStorageService.uploadFile({fileName: `${projectId}/traces/${traceId}-trace.json`,fileType,data,- expiresInSeconds,}),eventStorageService.uploadFile({fileName: `${projectId}/observation/${traceId}-observation.json`,fileType,data,- expiresInSeconds,}),eventStorageService.uploadFile({fileName: `${projectId}/score/${traceId}-score.json`,fileType,data,- expiresInSeconds,}),]);diff --git a/worker/src/features/batchExport/handleBatchExportJob.ts b/worker/src/features/batchExport/handleBatchExportJob.tsindex 6f19af5bad..6c235685cb 100644--- a/worker/src/features/batchExport/handleBatchExportJob.ts+++ b/worker/src/features/batchExport/handleBatchExportJob.ts@@ -118,7 +118,7 @@ export const handleBatchExportJob = async (forcePathStyle: env.LANGFUSE_S3_BATCH_EXPORT_FORCE_PATH_STYLE === "true",awsSse: env.LANGFUSE_S3_BATCH_EXPORT_SSE,awsSseKmsKeyId: env.LANGFUSE_S3_BATCH_EXPORT_SSE_KMS_KEY_ID,- }).uploadFile({+ }).uploadWithSignedUrl({fileName,fileType:exportOptions[jobDetails.format as BatchExportFileFormat].fileType,diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.tsindex 56050be0f9..e7c751f1fa 100644--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts@@ -157,7 +157,6 @@ const processBlobStorageExport = async (config: {fileName: filePath,fileType: blobStorageProps.contentType,data: fileStream,- expiresInSeconds: 3600, // 1 hour expiry for the signed URL - is ignored});logger.info(diff --git a/worker/src/queues/coreDataS3ExportQueue.ts b/worker/src/queues/coreDataS3ExportQueue.tsindex cf1f412244..8b5a349ad8 100644--- a/worker/src/queues/coreDataS3ExportQueue.ts+++ b/worker/src/queues/coreDataS3ExportQueue.ts@@ -138,7 +138,6 @@ export const coreDataS3ExportProcessor: Processor = async (): Promise<void> => {fileName: `${env.LANGFUSE_S3_CORE_DATA_UPLOAD_PREFIX}${key}.jsonl`,fileType: "application/x-ndjson",data: value.map((item) => JSON.stringify(item)).join("\n"),- expiresInSeconds: 1, // not used as we only upload}),),);