Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
eff8bd0
draft impl
buenaflor Nov 2, 2023
210f306
improve code
buenaflor Nov 2, 2023
3caa4cd
Add open span
buenaflor Nov 2, 2023
a035435
Refactor to extend lazy database
buenaflor Nov 2, 2023
ea9e2d6
Update
buenaflor Nov 2, 2023
64652a4
SentryTransactionExecutor
buenaflor Nov 3, 2023
dcabe06
Implement tracing batch and transactions
buenaflor Nov 3, 2023
a3a5fb9
Update tests
buenaflor Nov 6, 2023
d3a65b4
update changelog
buenaflor Nov 6, 2023
9e54500
Formatting
buenaflor Nov 6, 2023
92c56f4
Merge branch 'main' into feat/drift-apm
buenaflor Nov 6, 2023
51f8041
Update workflows
buenaflor Nov 6, 2023
f089dde
Update deps
buenaflor Nov 6, 2023
4c41212
Fix analyze issues
buenaflor Nov 6, 2023
8d24a36
Format
buenaflor Nov 6, 2023
eeea299
Add example
buenaflor Nov 6, 2023
2a108fe
Update descriptions
buenaflor Nov 7, 2023
3ae85fe
Fix deps
buenaflor Nov 7, 2023
f077e06
Update run commands
buenaflor Nov 7, 2023
084503a
Update workflow
buenaflor Nov 7, 2023
36bc1fa
Update example
buenaflor Nov 7, 2023
a598fdb
Update workflow
buenaflor Nov 7, 2023
9f93206
Format
buenaflor Nov 7, 2023
f92816a
Fix dart analyze
buenaflor Nov 7, 2023
b6b8650
Try changing deps
buenaflor Nov 7, 2023
3e7f989
Add to craft
buenaflor Nov 7, 2023
8443c42
Change to flutter test
buenaflor Nov 7, 2023
a2fa7bc
Revert flutter test mock
buenaflor Nov 7, 2023
c4da2d3
change dir manually
buenaflor Nov 7, 2023
46dfa08
download and extract sqlite.dll for windows
buenaflor Nov 7, 2023
63439aa
Try downloading sqlite3
buenaflor Nov 7, 2023
5e513f9
set up dart
buenaflor Nov 7, 2023
87ad6dd
fix path to file
buenaflor Nov 7, 2023
e7308e0
use pub get
buenaflor Nov 7, 2023
38cae39
Use flutter pub get
buenaflor Nov 7, 2023
cbc8b43
fix
buenaflor Nov 7, 2023
4ffeb6c
skip windows test
buenaflor Nov 7, 2023
a1e638e
fix analyze
buenaflor Nov 7, 2023
ebd8dd5
fix drift.yml
buenaflor Nov 7, 2023
ae7dd70
format
buenaflor Nov 7, 2023
ddc6bad
Remove redundant test
buenaflor Nov 7, 2023
3df9151
Remove debug print
buenaflor Nov 7, 2023
d0a18eb
update trace origin
buenaflor Nov 7, 2023
df27999
Improvements
buenaflor Nov 7, 2023
d83844f
Improvements
buenaflor Nov 8, 2023
8d68a84
Add to flutter example
buenaflor Nov 13, 2023
40082e0
Skip web in examples
buenaflor Nov 13, 2023
76065c7
Update comment
buenaflor Nov 14, 2023
908d619
Fix web build
buenaflor Nov 14, 2023
bf5c20e
Try and set sqlite on windows
buenaflor Nov 14, 2023
8b02069
trigger
buenaflor Nov 14, 2023
9577c11
maybe fix workflow
buenaflor Nov 14, 2023
984f88a
fix
buenaflor Nov 14, 2023
9132516
trigger
buenaflor Nov 14, 2023
0a58ba9
trigger
buenaflor Nov 14, 2023
8b3928f
maybe trigger
buenaflor Nov 14, 2023
42e88d3
maybe trigger
buenaflor Nov 14, 2023
8b54152
maybe trigger
buenaflor Nov 14, 2023
4f0af71
fix
buenaflor Nov 14, 2023
78e56fc
try trigger
buenaflor Nov 14, 2023
13e67e7
Merge branch 'main' into feat/drift-apm
buenaflor Nov 14, 2023
6fb54e5
Merge branch 'main' into feat/drift-apm
buenaflor Nov 14, 2023
9b0244b
update workflow
buenaflor Nov 14, 2023
70910b3
try github_env
buenaflor Nov 14, 2023
55a5f4e
try
buenaflor Nov 14, 2023
0ffb3fa
try
buenaflor Nov 14, 2023
ee97635
try
buenaflor Nov 14, 2023
467d970
try
buenaflor Nov 14, 2023
26da22f
add to path
buenaflor Nov 14, 2023
bb3840a
skip windows
buenaflor Nov 14, 2023
4b98cc8
formatting
buenaflor Nov 14, 2023
d9e12a6
try with embedded dll
buenaflor Nov 14, 2023
921bf08
improvements
buenaflor Nov 14, 2023
52cec3b
Formatting
buenaflor Nov 14, 2023
8bb5fae
gitignore
buenaflor Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
  • Loading branch information
buenaflor committed Nov 2, 2023
commit ea9e2d6e3c7aef5afdc3875813ca0ced4fdc60b8
66 changes: 42 additions & 24 deletions drift/lib/src/sentry_drift_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import 'sentry_span_helper.dart';
/// Signature of a function that opens a database connection when instructed to.
typedef DatabaseOpener = FutureOr<QueryExecutor> Function();

/// Sentry Drift Database Executor based on [LazyDatabase].
/// The Sentry Drift Database Executor.
///
/// A special database executor that delegates work to another [QueryExecutor].
/// The other executor is lazily opened by a [DatabaseOpener].
class SentryDriftDatabase extends LazyDatabase {
/// If queryExecutor is not null, it will be used instead of the default one.
/// The default is [LazyDatabase].
class SentryDriftDatabase extends QueryExecutor {
final Hub _hub;

final _spanHelper = SentrySpanHelper(
// ignore: invalid_use_of_internal_member
SentryTraceOrigins.autoDbDriftDatabaseExecutor,
);

String? dbName = 'people-drift-impl';
final QueryExecutor _queryExecutor;

final String? _dbName;

@internal
// ignore: public_member_api_docs
Expand All @@ -41,61 +44,76 @@ class SentryDriftDatabase extends LazyDatabase {
/// Declares a [SentryDriftDatabase] that will run [opener] when the database is
/// first requested to be opened. You must specify the same [dialect] as the
/// underlying database has
SentryDriftDatabase(
super.opener, {
super.dialect,
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter() {
SentryDriftDatabase(DatabaseOpener opener,
{@internal Hub? hub,
@internal QueryExecutor? queryExecutor,
@internal String? dbName})
: _hub = hub ?? HubAdapter(),
_dbName = dbName,
_queryExecutor = queryExecutor ?? LazyDatabase(opener) {
_spanHelper.setHub(_hub);
}

@override
TransactionExecutor beginTransaction() {
return _queryExecutor.beginTransaction();
}

@override
Future<void> runBatched(BatchedStatements statements) {
return _queryExecutor.runBatched(statements);
}

@override
Future<bool> ensureOpen(QueryExecutorUser user) {
return _spanHelper.asyncWrapInSpan('open', () async {
return await super.ensureOpen(user);
});
return await _queryExecutor.ensureOpen(user);
}, dbName: _dbName);
}

@override
Future<void> runCustom(String statement, [List<Object?>? args]) {
return _spanHelper.asyncWrapInSpan('custom', () async {
return await super.runCustom(statement, args);
}, dbName: dbName);
return await _queryExecutor.runCustom(statement, args);
}, dbName: _dbName);
}

@override
Future<int> runDelete(String statement, List<Object?> args) {
return _spanHelper.asyncWrapInSpan('delete', () async {
return await super.runDelete(statement, args);
}, dbName: dbName);
return await _queryExecutor.runDelete(statement, args);
}, dbName: _dbName);
}

@override
Future<int> runInsert(String statement, List<Object?> args) {
return _spanHelper.asyncWrapInSpan('insert', () async {
return await super.runInsert(statement, args);
}, dbName: dbName);
return await _queryExecutor.runInsert(statement, args);
}, dbName: _dbName);
}

@override
Future<List<Map<String, Object?>>> runSelect(
String statement, List<Object?> args) {
return _spanHelper.asyncWrapInSpan('select', () async {
return await super.runSelect(statement, args);
}, dbName: dbName);
return await _queryExecutor.runSelect(statement, args);
}, dbName: _dbName);
}

@override
Future<int> runUpdate(String statement, List<Object?> args) {
return _spanHelper.asyncWrapInSpan('update', () async {
return await super.runUpdate(statement, args);
}, dbName: dbName);
return await _queryExecutor.runUpdate(statement, args);
}, dbName: _dbName);
}

@override
Future<void> close() {
return _spanHelper.asyncWrapInSpan('close', () async {
return await super.close();
}, dbName: dbName);
return await _queryExecutor.close();
}, dbName: _dbName);
}

@override
SqlDialect get dialect => _queryExecutor.dialect;
}
1 change: 0 additions & 1 deletion drift/lib/src/sentry_span_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class SentrySpanHelper {

try {
final result = await execute();

span?.status = SpanStatus.ok();

return result;
Expand Down
100 changes: 33 additions & 67 deletions drift/test/sentry_database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ void main() {
expect(span?.data[SentryDriftDatabase.dbNameKey], Fixture.dbName);
}

void verifyErrorSpan(
String description,
Exception exception,
SentrySpan? span,
) {
void verifyErrorSpan(String description,
Exception exception,
SentrySpan? span,) {
expect(span?.context.operation, SentryDriftDatabase.dbOp);
expect(span?.context.description, description);
expect(span?.status, SpanStatus.internalError());
Expand Down Expand Up @@ -56,26 +54,26 @@ void main() {
});

test('insert adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

await sut.into(sut.todoItems).insert(TodoItemsCompanion.insert(
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));

verifySpan('insert', fixture.getCreatedSpan());
});

test('update adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

await sut.into(sut.todoItems).insert(TodoItemsCompanion.insert(
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));

await (sut.update(sut.todoItems)
..where((tbl) => tbl.title.equals('todo: finish drift setup')))
..where((tbl) => tbl.title.equals('todo: finish drift setup')))
.write(TodoItemsCompanion(
title: Value('after update'),
content: Value('We can now write queries and define our own tables.'),
Expand All @@ -85,93 +83,64 @@ void main() {
});

test('custom adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

await sut.customStatement('SELECT * FROM todo_items');

verifySpan('custom', fixture.getCreatedSpan());
});

test('batch adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

await sut.transaction(() async {
await sut.into(sut.todoItems).insert(TodoItemsCompanion.insert(
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
});

await sut.batch((batch) async {
await sut.into(sut.todoItems).insert(TodoItemsCompanion.insert(
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
});

// TODO
});

test('close adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

await sut.close();

verifySpan('close', fixture.getCreatedSpan());
});

test('open adds span', () async {
final sut = fixture.getSut();
final sut = fixture.sut;

// SentryDriftDatabase is lazily opened by default so it won't create a span
// until it is actually used.
// SentryDriftDatabase is by default lazily opened by default so it won't
// create a span until it is actually used.
await sut.select(sut.todoItems).get();

verifySpan('open', fixture.getSpanByDescription('open'));
});

test('will not add open span if db is not used', () async {
fixture.getSut();
fixture.sut;

expect(fixture.tracer.children.isEmpty, true);
});
});

group('adds error span', () {
late Fixture fixture;

setUp(() async {
fixture = Fixture();
await fixture.setUp();

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
});

tearDown(() async {
await fixture.tearDown();
});

// test('throwing insert adds error span', () async {
// final sut = fixture.getSut();
// Exception? exception;
//
// try {
// await sut.into(fixture.sut.todoItems).insert(TodoItemsCompanion.insert(
// title: 'AAAaaaaaa',
// content: 'aaaaaaaaaa',
// ));
// } catch (error) {
// expect(error is InvalidDataException, true);
// exception = error as InvalidDataException;
// }
//
//
//
// //verifyErrorSpan('insert', exception!, fixture.getCreatedSpan());
// });
});
}

class Fixture {
final options = SentryOptions();
final hub = MockHub();
static final dbName = 'people-drift-impl';
final exception = Exception('fixture-exception');

final _context = SentryTransactionContext('name', 'operation');
late final tracer = SentryTracer(_context, hub);
late AppDatabase sut;
Expand All @@ -184,21 +153,18 @@ class Fixture {
await sut.close();
}

AppDatabase getSut() {
return sut;
}

SentrySpan? getCreatedSpan() {
return tracer.children.last;
}

SentrySpan? getSpanByDescription(String description) {
return tracer.children.firstWhere((element) => element.context.description == description);
return tracer.children
.firstWhere((element) => element.context.description == description);
}

SentryDriftDatabase openConnection() {
return SentryDriftDatabase(() {
return NativeDatabase.memory();
}, hub: hub);
}, hub: hub, dbName: dbName);
}
}