|
1 | 1 | import { |
2 | 2 | Client, |
3 | 3 | InStatement, |
| 4 | + ResultSet, |
4 | 5 | Transaction, |
5 | 6 | TransactionMode, |
6 | 7 | } from "@libsql/client"; |
@@ -93,35 +94,92 @@ export function libsqlIntegration( |
93 | 94 |
|
94 | 95 | const originalBatch = client.batch; |
95 | 96 | client.batch = function (stmts: InStatement[]) { |
96 | | - const batchOperation = async (span?: Span) => { |
| 97 | + const batchOperation = async (parentSpan?: Span) => { |
97 | 98 | if (options.breadcrumbs) { |
98 | 99 | Sentry.addBreadcrumb({ |
99 | 100 | category: "libsql", |
100 | | - message: "Executing batch SQL statements", |
| 101 | + message: "Starting batch SQL operation", |
101 | 102 | data: { stmtCount: stmts.length }, |
102 | 103 | }); |
103 | 104 | } |
104 | 105 |
|
105 | 106 | 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 | + } |
107 | 161 |
|
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 |
111 | 169 | } |
112 | 170 |
|
113 | 171 | if (options.breadcrumbs) { |
114 | 172 | Sentry.addBreadcrumb({ |
115 | 173 | category: "libsql", |
116 | | - message: "Batch SQL statements executed successfully", |
| 174 | + message: "Batch SQL operation completed successfully", |
117 | 175 | data: { stmtCount: stmts.length }, |
118 | 176 | }); |
119 | 177 | } |
120 | 178 |
|
121 | | - return result; |
| 179 | + return results; |
122 | 180 | } catch (error) { |
123 | | - if (span) { |
124 | | - span.setStatus({ code: 2 }); |
| 181 | + if (parentSpan) { |
| 182 | + parentSpan.setStatus({ code: 2 }); |
125 | 183 | } |
126 | 184 |
|
127 | 185 | if (options.errors) { |
@@ -223,7 +281,7 @@ export function libsqlIntegration( |
223 | 281 | { |
224 | 282 | op: "db.commit", |
225 | 283 | name: "libsql.transaction.commit", |
226 | | - parentSpan: parentSpan, |
| 284 | + parentSpan, |
227 | 285 | }, |
228 | 286 | commitOperation |
229 | 287 | ); |
@@ -283,7 +341,7 @@ export function libsqlIntegration( |
283 | 341 | { |
284 | 342 | op: "db.rollback", |
285 | 343 | name: "libsql.transaction.rollback", |
286 | | - parentSpan: parentSpan, |
| 344 | + parentSpan, |
287 | 345 | }, |
288 | 346 | rollbackOperation |
289 | 347 | ); |
|
0 commit comments