Skip to content

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

fix(worker): emit blob export stage timing on failures (LFE-10441) (#14624)

Niklas Semmlera081d431be50
diff --git a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
index 6c74db32eb..75c0c016b1 100644
--- a/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
+++ b/worker/src/__tests__/blobStorageIntegrationProcessing.test.ts
@@ -15,12 +15,18 @@ const originalCloudRegion = vi.hoisted(() => {
return cloudRegion;
});
-// Override only recordIncrement so the attempt counter is assertable.
+// Override recordIncrement + recordHistogram so the attempt counter and
+// per-stage timing metrics are assertable.
const mockRecordIncrement = vi.hoisted(() => vi.fn());
+const mockRecordHistogram = vi.hoisted(() => vi.fn());
vi.mock("@langfuse/shared/src/server", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@langfuse/shared/src/server")>();
- return { ...actual, recordIncrement: mockRecordIncrement };
+ return {
+ ...actual,
+ recordIncrement: mockRecordIncrement,
+ recordHistogram: mockRecordHistogram,
+ };
});
import { env } from "../env";
@@ -203,6 +209,89 @@ describe("BlobStorageIntegrationProcessingJob", () => {
expect(files.filter((f) => f.file.includes(projectId))).toHaveLength(0);
});
+ // LFE-10441: per-stage timing histograms must also be emitted when an export
+ // fails (originally gated on upload success), tagged outcome="failure" so the
+ // happy-path percentiles stay clean.
+ it("emits per-stage timing histograms tagged outcome=failure when the upload fails", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ s3Prefix = projectId;
+
+ await prisma.blobStorageIntegration.create({
+ data: {
+ projectId,
+ type: BlobStorageIntegrationType.S3,
+ bucketName,
+ prefix: s3Prefix,
+ accessKeyId: minioAccessKeyId,
+ secretAccessKey: encrypt(minioAccessKeySecret),
+ region: region ? region : "auto",
+ // endpoint null -> skip the persisted-endpoint preflight; the storage
+ // service is mocked anyway, so no real connection is made.
+ endpoint: null,
+ forcePathStyle:
+ env.LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE === "true",
+ enabled: true,
+ exportFrequency: "daily",
+ // Non-enriched source: sidesteps the enriched-export guard regardless of
+ // V4 preview state, so this runs outside maybeDescribe on every CI leg.
+ exportSource: "TRACES_OBSERVATIONS",
+ // A past lastSyncAt yields a non-empty export window without requiring
+ // the ClickHouse min-timestamp probe.
+ lastSyncAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
+ },
+ });
+
+ // Force every upload to reject so the export takes the failure path.
+ const getInstanceSpy = vi
+ .spyOn(StorageServiceFactory, "getInstance")
+ .mockReturnValue({
+ uploadFileBuffered: vi
+ .fn()
+ .mockRejectedValue(new Error("simulated upload failure")),
+ } as unknown as StorageService);
+
+ mockRecordIncrement.mockClear();
+ mockRecordHistogram.mockClear();
+
+ try {
+ await expect(
+ handleBlobStorageIntegrationProjectJob({
+ data: { payload: { projectId } },
+ } as Job),
+ ).rejects.toThrow(/simulated upload failure/i);
+ } finally {
+ getInstanceSpy.mockRestore();
+ }
+
+ // Failure attempt counter fired.
+ expect(mockRecordIncrement).toHaveBeenCalledWith(
+ "langfuse.blobstorage.table_export.count",
+ 1,
+ expect.objectContaining({ outcome: "failure", projectId }),
+ );
+
+ // All four stage timers emitted with outcome=failure...
+ for (const metric of [
+ "langfuse.blob_export.ch_read_ms",
+ "langfuse.blob_export.enrich_ms",
+ "langfuse.blob_export.gzip_cpu_ms",
+ "langfuse.blob_export.upload_wait_ms",
+ ]) {
+ expect(mockRecordHistogram).toHaveBeenCalledWith(
+ metric,
+ expect.any(Number),
+ expect.objectContaining({ outcome: "failure" }),
+ );
+ }
+
+ // ...and never with outcome=success, since no upload succeeded.
+ expect(mockRecordHistogram).not.toHaveBeenCalledWith(
+ "langfuse.blob_export.ch_read_ms",
+ expect.any(Number),
+ expect.objectContaining({ outcome: "success" }),
+ );
+ });
+
maybeDescribe("events table export tests", () => {
it("should export traces, generations, and scores to S3", async () => {
// Setup
diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
index 71f6243680..16f42aa2c2 100644
--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
@@ -853,49 +853,54 @@ const processBlobStorageExport = async (config: {
uploadDurationMsFinal ??
Math.round(performance.now() - uploadStartMs);
span.setAttribute("blob.uploadDurationMs", totalUploadMs);
- if (uploadSucceeded) {
- const finalGzipCpuMs = gzipStats
- ? Math.max(
- 0,
- Math.round(gzipStats.activeMs - gzipStats.backpressureMs),
- )
- : 0;
- const finalUploadWaitMs = gzipStats
- ? Math.round(gzipStats.backpressureMs)
- : parquetEligible
- ? Math.round(sourceStats.backpressureMs)
- : Math.max(0, totalUploadMs - finalChReadMs - finalEnrichMs);
- span.setAttribute("blob.gzipCpuMs", finalGzipCpuMs);
- span.setAttribute("blob.uploadWaitMs", finalUploadWaitMs);
- const finalExportFormat = parquetEligible
- ? BlobExportFormat.PARQUET
- : resolveBlobExportFormat(config.fileType, config.compressed);
- const stageTags = {
- table: config.table,
- path: exportPath,
- source: finalExportFormat,
- };
- recordHistogram(
- "langfuse.blob_export.ch_read_ms",
- finalChReadMs,
- stageTags,
- );
- recordHistogram(
- "langfuse.blob_export.enrich_ms",
- finalEnrichMs,
- stageTags,
- );
- recordHistogram(
- "langfuse.blob_export.gzip_cpu_ms",
- finalGzipCpuMs,
- stageTags,
- );
- recordHistogram(
- "langfuse.blob_export.upload_wait_ms",
- finalUploadWaitMs,
- stageTags,
- );
- }
+ // Emit stage timings on both success and failure. On failure the
+ // values are partial (the upload aborted mid-stream), so an
+ // `outcome` tag keeps them out of the happy-path percentiles while
+ // still capturing where a failed export spent its time.
+ const finalGzipCpuMs = gzipStats
+ ? Math.max(
+ 0,
+ Math.round(gzipStats.activeMs - gzipStats.backpressureMs),
+ )
+ : 0;
+ const finalUploadWaitMs = gzipStats
+ ? Math.round(gzipStats.backpressureMs)
+ : parquetEligible
+ ? Math.round(sourceStats.backpressureMs)
+ : Math.max(0, totalUploadMs - finalChReadMs - finalEnrichMs);
+ span.setAttribute("blob.gzipCpuMs", finalGzipCpuMs);
+ span.setAttribute("blob.uploadWaitMs", finalUploadWaitMs);
+ const finalExportFormat = parquetEligible
+ ? BlobExportFormat.PARQUET
+ : resolveBlobExportFormat(config.fileType, config.compressed);
+ const stageTags = {
+ table: config.table,
+ path: exportPath,
+ source: finalExportFormat,
+ outcome: (uploadSucceeded
+ ? "success"
+ : "failure") satisfies BlobTableExportOutcome,
+ };
+ recordHistogram(
+ "langfuse.blob_export.ch_read_ms",
+ finalChReadMs,
+ stageTags,
+ );
+ recordHistogram(
+ "langfuse.blob_export.enrich_ms",
+ finalEnrichMs,
+ stageTags,
+ );
+ recordHistogram(
+ "langfuse.blob_export.gzip_cpu_ms",
+ finalGzipCpuMs,
+ stageTags,
+ );
+ recordHistogram(
+ "langfuse.blob_export.upload_wait_ms",
+ finalUploadWaitMs,
+ stageTags,
+ );
}
if (producesUploadStats) {
span.setAttribute("blob.upload.parts", uploadStats.partsUploaded);