Skip to content
Prev Previous commit
Next Next commit
couple dbName to db instance
  • Loading branch information
buenaflor committed Sep 7, 2023
commit 39b4c2584a8b466252c3c0e329c8939e3050dec9
9 changes: 6 additions & 3 deletions sqflite/lib/src/sentry_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'utils/sentry_database_span_attributes.dart';
class SentryBatch implements Batch {
final Batch _batch;
final Hub _hub;
final String? _dbName;

// we don't clear the buffer because SqfliteBatch don't either
final _buffer = StringBuffer();
Expand All @@ -37,7 +38,9 @@ class SentryBatch implements Batch {
SentryBatch(
this._batch, {
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter();
@internal String? dbName,
}) : _hub = hub ?? HubAdapter(),
_dbName = dbName;

@override
Future<List<Object?>> apply({bool? noResult, bool? continueOnError}) {
Expand All @@ -51,7 +54,7 @@ class SentryBatch implements Batch {

// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _batch.apply(
Expand Down Expand Up @@ -89,7 +92,7 @@ class SentryBatch implements Batch {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _batch.commit(
Expand Down
18 changes: 8 additions & 10 deletions sqflite/lib/src/sentry_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
static const dbNameKey = 'db.name';
@internal
// ignore: public_member_api_docs
static String? dbName;

String? dbName;

/// ```dart
/// import 'package:sqflite/sqflite.dart';
Expand All @@ -57,16 +56,16 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
this._database, {
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter(),
super(_database, hub: hub) {
dbName = _basenameWithoutExtension(_database.path),
super(_database, hub: hub, dbName: _basenameWithoutExtension(_database.path)) {
// ignore: invalid_use_of_internal_member
final options = _hub.options;
options.sdk.addIntegration('SentrySqfliteTracing');
options.sdk.addPackage(packageName, sdkVersion);
dbName = _basenameWithoutExtension(_database.path);
}

/// Gets the part of path after the last separator, and without any trailing file extension.
String _basenameWithoutExtension(String filePath) {
static String _basenameWithoutExtension(String filePath) {
int lastIndex = filePath.lastIndexOf('/');
int dotIndex = filePath.lastIndexOf('.');

Expand All @@ -90,11 +89,10 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;

dbName = null;

try {
await _database.close();

dbName = null;
span?.status = SpanStatus.ok();
} catch (exception) {
span?.throwable = exception;
Expand Down Expand Up @@ -142,13 +140,13 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, dbName);

Future<T> newAction(Transaction txn) async {
final executor =
SentryDatabaseExecutor(txn, parentSpan: span, hub: _hub);
SentryDatabaseExecutor(txn, parentSpan: span, hub: _hub, dbName: dbName);
final sentrySqfliteTransaction =
SentrySqfliteTransaction(executor, hub: _hub);
SentrySqfliteTransaction(executor, hub: _hub, dbName: dbName);

return await action(sentrySqfliteTransaction);
}
Expand Down
29 changes: 16 additions & 13 deletions sqflite/lib/src/sentry_database_executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import 'utils/sentry_database_span_attributes.dart';
class SentryDatabaseExecutor implements DatabaseExecutor {
final DatabaseExecutor _executor;
final ISentrySpan? _parentSpan;
final String? _dbName;

// ignore: public_member_api_docs
SentryDatabaseExecutor(
this._executor, {
ISentrySpan? parentSpan,
@internal Hub? hub,
@internal String? dbName,
}) : _parentSpan = parentSpan,
_hub = hub ?? HubAdapter();
_hub = hub ?? HubAdapter(),
_dbName = dbName;
final Hub _hub;

@override
Batch batch() => SentryBatch(_executor.batch(), hub: _hub);
Batch batch() => SentryBatch(_executor.batch(), hub: _hub, dbName: _dbName);

@override
Database get database => _executor.database;
Expand All @@ -42,7 +45,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result =
Expand Down Expand Up @@ -73,7 +76,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
span?.setData(SentryDatabase.dbSystemKey, SentryDatabase.dbSystem);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
await _executor.execute(sql, arguments);
Expand Down Expand Up @@ -111,7 +114,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.insert(
Expand Down Expand Up @@ -168,7 +171,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.query(
Expand Down Expand Up @@ -232,7 +235,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.queryCursor(
Expand Down Expand Up @@ -273,7 +276,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.rawDelete(sql, arguments);
Expand Down Expand Up @@ -302,7 +305,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.rawInsert(sql, arguments);
Expand Down Expand Up @@ -334,7 +337,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.rawQuery(sql, arguments);
Expand Down Expand Up @@ -367,7 +370,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.rawQueryCursor(
Expand Down Expand Up @@ -400,7 +403,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.rawUpdate(sql, arguments);
Expand Down Expand Up @@ -442,7 +445,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
setDatabaseAttributeData(span);
setDatabaseAttributeData(span, _dbName);

try {
final result = await _executor.update(
Expand Down
7 changes: 5 additions & 2 deletions sqflite/lib/src/sentry_sqflite_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import 'sentry_batch.dart';
class SentrySqfliteTransaction extends Transaction implements DatabaseExecutor {
final DatabaseExecutor _executor;
final Hub _hub;
final String? _dbName;

// ignore: public_member_api_docs
@internal
SentrySqfliteTransaction(
this._executor, {
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter();
@internal String? dbName,
}) : _hub = hub ?? HubAdapter(),
_dbName = dbName;

@override
Batch batch() => SentryBatch(_executor.batch(), hub: _hub);
Batch batch() => SentryBatch(_executor.batch(), hub: _hub, dbName: _dbName);

@override
Database get database => _executor.database;
Expand Down
8 changes: 4 additions & 4 deletions sqflite/lib/src/utils/sentry_database_span_attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import '../../sentry_sqflite.dart';

/// Sets the database attributes on the [span].
/// It contains the database system and the database name.
void setDatabaseAttributeData(ISentrySpan? span) {
void setDatabaseAttributeData(ISentrySpan? span, String? dbName) {
span?.setData(SentryDatabase.dbSystemKey, SentryDatabase.dbSystem);
if (SentryDatabase.dbName != null) {
span?.setData(SentryDatabase.dbNameKey, SentryDatabase.dbName);
if (dbName != null) {
span?.setData(SentryDatabase.dbNameKey, dbName);
}
}
}
20 changes: 2 additions & 18 deletions sqflite/test/sentry_batch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ SELECT * FROM Product''';

final span = fixture.tracer.children.last;
expect(span.data[SentryDatabase.dbSystemKey], SentryDatabase.dbSystem);
expect(span.data[SentryDatabase.dbNameKey], SentryDatabase.dbName);
expect(span.data[SentryDatabase.dbNameKey], (db as SentryDatabase).dbName);

await db.close();
});
Expand All @@ -311,23 +311,7 @@ SELECT * FROM Product''';

final span = fixture.tracer.children.last;
expect(span.data[SentryDatabase.dbSystemKey], SentryDatabase.dbSystem);
expect(span.data[SentryDatabase.dbNameKey], SentryDatabase.dbName);

await db.close();
});

test('dbName attribute is null if currentDbName is null', () async {
final db = await fixture.getDatabase();
final batch = db.batch();
SentryDatabase.dbName = null;

batch.insert('Product', <String, Object?>{'title': 'Product 1'});

await batch.commit();

final span = fixture.tracer.children.last;
expect(span.data[SentryDatabase.dbSystemKey], SentryDatabase.dbSystem);
expect(span.data[SentryDatabase.dbNameKey], isNull);
expect(span.data[SentryDatabase.dbNameKey], (db as SentryDatabase).dbName);

await db.close();
});
Expand Down
10 changes: 5 additions & 5 deletions sqflite/test/sentry_database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void main() {

await db.close();

expect(SentryDatabase.dbName, null);
expect(db.dbName, null);

final span = fixture.tracer.children.last;
expect(span.context.operation, 'db');
Expand Down Expand Up @@ -139,7 +139,7 @@ void main() {
test('opening db sets currentDbName with :memory:', () async {
final db = await fixture.getSut();

expect(SentryDatabase.dbName, ':memory:');
expect(db.dbName, ':memory:');

await db.close();
});
Expand All @@ -150,19 +150,19 @@ void main() {
database: await openDatabase('path/database/mydatabase.db'),
execute: false);

expect(SentryDatabase.dbName, 'mydatabase');
expect(db.dbName, 'mydatabase');

await db.close();
});

test('closing db sets currentDbName to null', () async {
final db = await fixture.getSut();

expect(SentryDatabase.dbName, inMemoryDatabasePath);
expect(db.dbName, inMemoryDatabasePath);

await db.close();

expect(SentryDatabase.dbName, null);
expect(db.dbName, null);
});

tearDown(() {
Expand Down
5 changes: 2 additions & 3 deletions sqflite/test/sentry_sqflite_database_factory_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void main() {
test('returns wrapped data base if performance enabled', () async {
final db = await openDatabase(inMemoryDatabasePath);

expect(SentryDatabase.dbName, inMemoryDatabasePath);
expect(db is SentryDatabase, true);
expect((db as SentryDatabase).dbName, inMemoryDatabasePath);

await db.close();
});
Expand All @@ -48,7 +48,6 @@ void main() {

final db = await openDatabase(inMemoryDatabasePath);

expect(SentryDatabase.dbName, null);
expect(db is! SentryDatabase, true);

await db.close();
Expand All @@ -58,7 +57,7 @@ void main() {
() async {
final db = await openDatabase(inMemoryDatabasePath);

expect(SentryDatabase.dbName, inMemoryDatabasePath);
expect((db as SentryDatabase).dbName, inMemoryDatabasePath);

final span = fixture.tracer.children.last;
expect(span.context.operation, 'db');
Expand Down
1 change: 1 addition & 0 deletions sqflite/test/sentry_sqflite_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void main() {
expect(span.status, SpanStatus.ok());
// ignore: invalid_use_of_internal_member
expect(span.origin, SentryTraceOrigins.autoDbSqfliteOpenDatabase);
expect((db as SentryDatabase).dbName, inMemoryDatabasePath);

await db.close();
});
Expand Down