Proprely handle errors with drizzle-otel

This commit is contained in:
2025-11-17 22:06:53 +01:00
parent 595b6eea7c
commit 1323e5a62a
3 changed files with 21 additions and 45 deletions

View File

@@ -41,7 +41,7 @@
"@opentelemetry/sdk-trace-base": "^2.1.0",
"@types/node": "18.15.11",
"@types/pg": "^8.11.10",
"drizzle-orm": "^0.36.4",
"drizzle-orm": "^0.44.7",
"postgres": "^3.4.7",
"rimraf": "3.0.2",
"typescript": "^5",

View File

@@ -5,6 +5,7 @@ import {
trace,
type Span,
} from "@opentelemetry/api";
import { DrizzleQueryError } from "drizzle-orm";
const DEFAULT_TRACER_NAME = "@kubiks/otel-drizzle";
const DEFAULT_DB_SYSTEM = "postgresql";
@@ -130,7 +131,12 @@ function extractOperation(queryText: string): string | undefined {
*/
function finalizeSpan(span: Span, error?: unknown): void {
if (error) {
if (error instanceof Error) {
if (error instanceof DrizzleQueryError && error.cause) {
// special handling for node-postgres
if ("detail" in error.cause && typeof error.cause.detail === "string")
error.cause.stack = error.cause.detail;
span.recordException(error.cause)
} else if (error instanceof Error) {
span.recordException(error);
} else {
span.recordException(new Error(String(error)));