Skip to content

Commit df27999

Browse files
committed
Improvements
1 parent d0a18eb commit df27999

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

drift/lib/src/sentry_query_executor.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ class SentryQueryExecutor extends QueryExecutor {
6767
@override
6868
TransactionExecutor beginTransaction() {
6969
final transactionExecutor = _queryExecutor.beginTransaction();
70-
return SentryTransactionExecutor(
70+
final sentryTransactionExecutor = SentryTransactionExecutor(
7171
transactionExecutor,
7272
_hub,
7373
dbName: _dbName,
7474
);
75+
sentryTransactionExecutor.beginTransaction();
76+
return sentryTransactionExecutor;
7577
}
7678

7779
@override

drift/lib/src/sentry_span_helper.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ class SentrySpanHelper {
2727
String description,
2828
Future<T> Function() execute, {
2929
String? dbName,
30+
bool useTransactionSpan = false,
3031
}) async {
31-
final currentSpan = _hub.getSpan();
32+
ISentrySpan? currentSpan = _hub.getSpan();
33+
if (useTransactionSpan) {
34+
currentSpan = transactionSpan;
35+
}
3236
final span = currentSpan?.startChild(
3337
SentryQueryExecutor.dbOp,
3438
description: description,

drift/lib/src/sentry_transaction_executor.dart

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class SentryTransactionExecutor extends TransactionExecutor {
2222
: _hub = hub,
2323
_dbName = dbName {
2424
_spanHelper.setHub(_hub);
25-
beginTransaction();
2625
}
2726

2827
@override
@@ -81,7 +80,14 @@ class SentryTransactionExecutor extends TransactionExecutor {
8180

8281
@override
8382
Future<int> runInsert(String statement, List<Object?> args) {
84-
return _executor.runInsert(statement, args);
83+
return _spanHelper.asyncWrapInSpan(
84+
'within transaction: $statement',
85+
() async {
86+
return _executor.runInsert(statement, args);
87+
},
88+
dbName: _dbName,
89+
useTransactionSpan: true,
90+
);
8591
}
8692

8793
@override

drift/test/sentry_database_test.dart

+20-12
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,16 @@ void main() {
175175
});
176176

177177
verifySpan(
178-
'transaction',
178+
'within transaction: $expectedInsertStatement',
179179
fixture.getCreatedSpan(),
180180
origin: SentryTraceOrigins.autoDbDriftTransactionExecutor,
181181
);
182+
183+
verifySpan(
184+
'transaction',
185+
fixture.getCreatedSpanByDescription('transaction'),
186+
origin: SentryTraceOrigins.autoDbDriftTransactionExecutor,
187+
);
182188
});
183189

184190
test('transaction rollback adds span', () async {
@@ -379,19 +385,21 @@ void main() {
379385
when(mockTransactionExecutor.beginTransaction())
380386
.thenThrow(fixture.exception);
381387

382-
try {
383-
// We need to move it inside the try/catch becaue SentryTransactionExecutor
384-
// starts beginTransaction() directly after init
385-
final SentryTransactionExecutor transactionExecutor =
386-
SentryTransactionExecutor(
387-
mockTransactionExecutor,
388-
fixture.hub,
389-
dbName: Fixture.dbName,
390-
);
388+
// We need to move it inside the try/catch becaue SentryTransactionExecutor
389+
// starts beginTransaction() directly after init
390+
final SentryTransactionExecutor transactionExecutor =
391+
SentryTransactionExecutor(
392+
mockTransactionExecutor,
393+
fixture.hub,
394+
dbName: Fixture.dbName,
395+
);
391396

392-
when(fixture.mockLazyDatabase.beginTransaction())
393-
.thenReturn(transactionExecutor);
397+
when(fixture.mockLazyDatabase.beginTransaction())
398+
.thenReturn(transactionExecutor);
394399

400+
when(fixture.mockLazyDatabase.runInsert(any, any)).thenAnswer((realInvocation) => Future.value(1));
401+
402+
try {
395403
await fixture.sut.batch((batch) async {
396404
await insertRow(fixture.sut);
397405
});

0 commit comments

Comments
 (0)