Skip to content

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

fix(blob-export): surface root cause and query_id in blob storage error logs (#13854)

Niklas Semmler62c5c775c1fb
diff --git a/packages/shared/src/server/repositories/clickhouse.ts b/packages/shared/src/server/repositories/clickhouse.ts
index fdcab1b5bf..b348f96795 100644
--- a/packages/shared/src/server/repositories/clickhouse.ts
+++ b/packages/shared/src/server/repositories/clickhouse.ts
@@ -240,6 +240,8 @@ export async function* queryClickhouseStream<T>(
kind: SpanKind.CLIENT,
});
+ let queryId: string | undefined;
+
try {
setSpanQueryAttributes(span, opts.query);
@@ -254,15 +256,23 @@ export async function* queryClickhouseStream<T>(
);
});
+ queryId = res.query_id;
+ span.setAttribute("ch.queryId", queryId);
+
for await (const rows of res.stream<T>()) {
for (const row of rows) {
yield handleExceptionRow(row.json());
}
}
} catch (error) {
- if (error instanceof ClickHouseResourceError) throw error;
+ if (error instanceof ClickHouseResourceError) {
+ const enriched = enrichWithQueryId(error, queryId);
+ throw enriched === error
+ ? error
+ : new ClickHouseResourceError(error.errorType, enriched, error.tags);
+ }
throw ClickHouseResourceError.wrapIfResourceError(
- error as Error,
+ enrichWithQueryId(error as Error, queryId),
opts.tags,
);
} finally {
@@ -270,6 +280,15 @@ export async function* queryClickhouseStream<T>(
}
}
+function enrichWithQueryId(error: Error, queryId: string | undefined): Error {
+ if (!queryId) return error;
+ const enriched = new Error(`${error.message} [query_id: ${queryId}]`, {
+ cause: error,
+ });
+ enriched.stack = error.stack;
+ return enriched;
+}
+
/**
* ClickHouse has a quirk when it comes to handling exceptions mid response.
* It will simply output a row with "exception" key inside, which is indistinguishable from
diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
index 4588b022e8..02d4e337f5 100644
--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
@@ -588,11 +588,18 @@ export const handleBlobStorageIntegrationProjectJob = async (
notifyBlobStorageExportFailedInBackground(projectId);
+ const chain = formatErrorChain(error);
logger.error(
- `[BLOB INTEGRATION] Error processing blob storage integration for project ${projectId}`,
- error,
+ `[BLOB INTEGRATION] Error processing blob storage integration for project ${projectId}: ${chain}`,
+ error instanceof Error ? { stack: error.stack } : {},
);
- throw error; // Rethrow to trigger retries
+ const rethrown = new Error(chain, { cause: error });
+ // Copy the original stack so BullMQ and the queue processor see the real
+ // failure site rather than this rethrow line. rethrown.stack starts with
+ // the original error's message, which won't match rethrown.message (the
+ // full chain), but structured loggers record them as separate fields.
+ if (error instanceof Error) rethrown.stack = error.stack;
+ throw rethrown;
}
};
@@ -688,3 +695,14 @@ function extractStorageErrorMessage(error: unknown): string {
// Fallback: ClickHouse errors or other non-wrapped errors
return error.message.slice(0, 1000);
}
+
+function formatErrorChain(error: unknown): string {
+ if (!(error instanceof Error)) return String(error);
+ const parts: string[] = [];
+ let current: unknown = error;
+ while (current instanceof Error) {
+ parts.push(current.message);
+ current = current.cause;
+ }
+ return parts.join(" caused by ");
+}