Skip to content

Commit 9852801

Browse files
committed
feat: batch tracing
1 parent 6556e8b commit 9852801

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/index.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ export function libsqlIntegration(
5353

5454
if (span) {
5555
span.setAttribute("rows_affected", result.rowsAffected);
56+
span.setStatus({ code: 1 });
57+
}
58+
59+
if (options.breadcrumbs) {
60+
Sentry.addBreadcrumb({
61+
category: "libsql",
62+
message: "SQL statement executed successfully",
63+
data: { sql, rowsAffected: result.rowsAffected },
64+
});
5665
}
5766

5867
return result;
@@ -82,6 +91,60 @@ export function libsqlIntegration(
8291
}
8392
};
8493

94+
const originalBatch = client.batch;
95+
client.batch = function (stmts: InStatement[]) {
96+
const batchOperation = async (span?: Span) => {
97+
if (options.breadcrumbs) {
98+
Sentry.addBreadcrumb({
99+
category: "libsql",
100+
message: "Executing batch SQL statements",
101+
data: { stmtCount: stmts.length },
102+
});
103+
}
104+
105+
try {
106+
const result = await originalBatch.call(this, stmts);
107+
108+
if (span) {
109+
span.setAttribute("stmtCount", stmts.length);
110+
span.setStatus({ code: 1 });
111+
}
112+
113+
if (options.breadcrumbs) {
114+
Sentry.addBreadcrumb({
115+
category: "libsql",
116+
message: "Batch SQL statements executed successfully",
117+
data: { stmtCount: stmts.length },
118+
});
119+
}
120+
121+
return result;
122+
} catch (error) {
123+
if (span) {
124+
span.setStatus({ code: 2 });
125+
}
126+
127+
if (options.errors) {
128+
Sentry.captureException(error);
129+
}
130+
131+
throw error;
132+
}
133+
};
134+
135+
if (options.tracing) {
136+
return Sentry.startSpan(
137+
{
138+
op: "db.batch",
139+
name: "libsql.batch",
140+
},
141+
batchOperation
142+
);
143+
} else {
144+
return batchOperation();
145+
}
146+
};
147+
85148
const originalTransaction = client.transaction;
86149
function wrappedTransaction(): Promise<Transaction>;
87150
function wrappedTransaction(mode: TransactionMode): Promise<Transaction>;
@@ -125,15 +188,24 @@ export function libsqlIntegration(
125188
if (span) {
126189
span.setStatus({ code: 1 });
127190
}
191+
128192
if (parentSpan) {
129193
parentSpan.setStatus({ code: 1 });
130194
}
131195

196+
if (options.breadcrumbs) {
197+
Sentry.addBreadcrumb({
198+
category: "libsql",
199+
message: "Transaction committed successfully",
200+
});
201+
}
202+
132203
return result;
133204
} catch (error) {
134205
if (span) {
135206
span.setStatus({ code: 2 });
136207
}
208+
137209
if (parentSpan) {
138210
parentSpan.setStatus({ code: 2 });
139211
}
@@ -176,15 +248,24 @@ export function libsqlIntegration(
176248
if (span) {
177249
span.setStatus({ code: 1 });
178250
}
251+
179252
if (parentSpan) {
180253
parentSpan.setStatus({ code: 1 });
181254
}
182255

256+
if (options.breadcrumbs) {
257+
Sentry.addBreadcrumb({
258+
category: "libsql",
259+
message: "Transaction rolled back successfully",
260+
});
261+
}
262+
183263
return result;
184264
} catch (error) {
185265
if (span) {
186266
span.setStatus({ code: 2 });
187267
}
268+
188269
if (parentSpan) {
189270
parentSpan.setStatus({ code: 2 });
190271
}

0 commit comments

Comments
 (0)