Skip to content

Commit 614913e

Browse files
committed
feat: batch span planning
1 parent 9852801 commit 614913e

File tree

1 file changed

+70
-12
lines changed

1 file changed

+70
-12
lines changed

src/index.ts

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Client,
33
InStatement,
4+
ResultSet,
45
Transaction,
56
TransactionMode,
67
} from "@libsql/client";
@@ -93,35 +94,92 @@ export function libsqlIntegration(
9394

9495
const originalBatch = client.batch;
9596
client.batch = function (stmts: InStatement[]) {
96-
const batchOperation = async (span?: Span) => {
97+
const batchOperation = async (parentSpan?: Span) => {
9798
if (options.breadcrumbs) {
9899
Sentry.addBreadcrumb({
99100
category: "libsql",
100-
message: "Executing batch SQL statements",
101+
message: "Starting batch SQL operation",
101102
data: { stmtCount: stmts.length },
102103
});
103104
}
104105

105106
try {
106-
const result = await originalBatch.call(this, stmts);
107+
const results = await Sentry.startSpan(
108+
{
109+
op: "db.batch",
110+
name: "libsql.batch",
111+
parentSpan: parentSpan,
112+
},
113+
async (batchSpan: Span) => {
114+
const statementResults: ResultSet[] = [];
115+
116+
for (let i = 0; i < stmts.length; i++) {
117+
const stmt = stmts[i];
118+
const sql = typeof stmt === "string" ? stmt : stmt.sql;
119+
const statementType = getStatementType(sql);
120+
121+
await Sentry.startSpan(
122+
{
123+
op: `db.${statementType}`,
124+
name: `libsql.batch.statement.${i}`,
125+
parentSpan: batchSpan,
126+
},
127+
async (statementSpan: Span) => {
128+
try {
129+
const result = await originalExecute.call(this, stmt);
130+
statementResults.push(result);
131+
132+
if (statementSpan) {
133+
statementSpan.setAttribute(
134+
"rows_affected",
135+
result.rowsAffected
136+
);
137+
statementSpan.setStatus({ code: 1 }); // Set success status
138+
}
139+
140+
if (options.breadcrumbs) {
141+
Sentry.addBreadcrumb({
142+
category: "libsql",
143+
message: `Batch statement ${i} executed successfully`,
144+
data: { sql, rowsAffected: result.rowsAffected },
145+
});
146+
}
147+
} catch (error) {
148+
if (statementSpan) {
149+
statementSpan.setStatus({ code: 2 });
150+
}
151+
152+
if (options.errors) {
153+
Sentry.captureException(error);
154+
}
155+
156+
throw error;
157+
}
158+
}
159+
);
160+
}
107161

108-
if (span) {
109-
span.setAttribute("stmtCount", stmts.length);
110-
span.setStatus({ code: 1 });
162+
return statementResults;
163+
}
164+
);
165+
166+
if (parentSpan) {
167+
parentSpan.setAttribute("statements_count", stmts.length);
168+
parentSpan.setStatus({ code: 1 }); // Set success status
111169
}
112170

113171
if (options.breadcrumbs) {
114172
Sentry.addBreadcrumb({
115173
category: "libsql",
116-
message: "Batch SQL statements executed successfully",
174+
message: "Batch SQL operation completed successfully",
117175
data: { stmtCount: stmts.length },
118176
});
119177
}
120178

121-
return result;
179+
return results;
122180
} catch (error) {
123-
if (span) {
124-
span.setStatus({ code: 2 });
181+
if (parentSpan) {
182+
parentSpan.setStatus({ code: 2 });
125183
}
126184

127185
if (options.errors) {
@@ -223,7 +281,7 @@ export function libsqlIntegration(
223281
{
224282
op: "db.commit",
225283
name: "libsql.transaction.commit",
226-
parentSpan: parentSpan,
284+
parentSpan,
227285
},
228286
commitOperation
229287
);
@@ -283,7 +341,7 @@ export function libsqlIntegration(
283341
{
284342
op: "db.rollback",
285343
name: "libsql.transaction.rollback",
286-
parentSpan: parentSpan,
344+
parentSpan,
287345
},
288346
rollbackOperation
289347
);

0 commit comments

Comments
 (0)