Skip to content

Commit 9442665

Browse files
authored
[starfish] add attributes to db spans data (#1629)
* add db attibutes to spans * change variable names * update changelog * update changelog * update tests * update imports * update tests * remove unused import * couple dbName to db instance * format * use path pub * don't set dbName to null when db close() is called
1 parent 280ab9f commit 9442665

12 files changed

+163
-11
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Add db.system and db.name attributes to db spans data ([#1629](https://github.com/getsentry/sentry-dart/pull/1629))
8+
59
### Features
610

711
- Tracing without performance ([#1621](https://github.com/getsentry/sentry-dart/pull/1621))

sqflite/lib/src/sentry_batch.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:sqflite/sqflite.dart';
66
import 'package:sqflite_common/src/sql_builder.dart';
77

88
import 'sentry_database.dart';
9+
import 'utils/sentry_database_span_attributes.dart';
910

1011
/// A [Batch] wrapper that adds Sentry support.
1112
///
@@ -21,6 +22,7 @@ import 'sentry_database.dart';
2122
class SentryBatch implements Batch {
2223
final Batch _batch;
2324
final Hub _hub;
25+
final String? _dbName;
2426

2527
// we don't clear the buffer because SqfliteBatch don't either
2628
final _buffer = StringBuffer();
@@ -36,7 +38,9 @@ class SentryBatch implements Batch {
3638
SentryBatch(
3739
this._batch, {
3840
@internal Hub? hub,
39-
}) : _hub = hub ?? HubAdapter();
41+
@internal String? dbName,
42+
}) : _hub = hub ?? HubAdapter(),
43+
_dbName = dbName;
4044

4145
@override
4246
Future<List<Object?>> apply({bool? noResult, bool? continueOnError}) {
@@ -47,8 +51,10 @@ class SentryBatch implements Batch {
4751
SentryDatabase.dbOp,
4852
description: _buffer.toString().trim(),
4953
);
54+
5055
// ignore: invalid_use_of_internal_member
5156
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
57+
setDatabaseAttributeData(span, _dbName);
5258

5359
try {
5460
final result = await _batch.apply(
@@ -86,6 +92,7 @@ class SentryBatch implements Batch {
8692
);
8793
// ignore: invalid_use_of_internal_member
8894
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
95+
setDatabaseAttributeData(span, _dbName);
8996

9097
try {
9198
final result = await _batch.commit(

sqflite/lib/src/sentry_database.dart

+21-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'package:sqflite/sqflite.dart';
55
import 'sentry_database_executor.dart';
66
import 'sentry_sqflite_transaction.dart';
77
import 'version.dart';
8+
import 'utils/sentry_database_span_attributes.dart';
9+
import 'package:path/path.dart' as p;
810

911
/// A [Database] wrapper that adds Sentry support.
1012
///
@@ -31,6 +33,18 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
3133
static const dbSqlQueryOp = 'db.sql.query';
3234

3335
static const _dbSqlOp = 'db.sql.transaction';
36+
@internal
37+
// ignore: public_member_api_docs
38+
static const dbSystemKey = 'db.system';
39+
@internal
40+
// ignore: public_member_api_docs
41+
static const dbSystem = 'sqlite';
42+
@internal
43+
// ignore: public_member_api_docs
44+
static const dbNameKey = 'db.name';
45+
@internal
46+
// ignore: public_member_api_docs
47+
String dbName;
3448

3549
/// ```dart
3650
/// import 'package:sqflite/sqflite.dart';
@@ -43,7 +57,9 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
4357
this._database, {
4458
@internal Hub? hub,
4559
}) : _hub = hub ?? HubAdapter(),
46-
super(_database, hub: hub) {
60+
dbName = p.basenameWithoutExtension(_database.path),
61+
super(_database,
62+
hub: hub, dbName: p.basenameWithoutExtension(_database.path)) {
4763
// ignore: invalid_use_of_internal_member
4864
final options = _hub.options;
4965
options.sdk.addIntegration('SentrySqfliteTracing');
@@ -113,12 +129,13 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
113129
);
114130
// ignore: invalid_use_of_internal_member
115131
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;
132+
setDatabaseAttributeData(span, dbName);
116133

117134
Future<T> newAction(Transaction txn) async {
118-
final executor =
119-
SentryDatabaseExecutor(txn, parentSpan: span, hub: _hub);
135+
final executor = SentryDatabaseExecutor(txn,
136+
parentSpan: span, hub: _hub, dbName: dbName);
120137
final sentrySqfliteTransaction =
121-
SentrySqfliteTransaction(executor, hub: _hub);
138+
SentrySqfliteTransaction(executor, hub: _hub, dbName: dbName);
122139

123140
return await action(sentrySqfliteTransaction);
124141
}

sqflite/lib/src/sentry_database_executor.dart

+18-2
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@ import 'package:sqflite_common/src/sql_builder.dart';
77

88
import 'sentry_batch.dart';
99
import 'sentry_database.dart';
10+
import 'utils/sentry_database_span_attributes.dart';
1011

1112
@internal
1213
// ignore: public_member_api_docs
1314
class SentryDatabaseExecutor implements DatabaseExecutor {
1415
final DatabaseExecutor _executor;
1516
final ISentrySpan? _parentSpan;
17+
final String? _dbName;
1618

1719
// ignore: public_member_api_docs
1820
SentryDatabaseExecutor(
1921
this._executor, {
2022
ISentrySpan? parentSpan,
2123
@internal Hub? hub,
24+
@internal String? dbName,
2225
}) : _parentSpan = parentSpan,
23-
_hub = hub ?? HubAdapter();
26+
_hub = hub ?? HubAdapter(),
27+
_dbName = dbName;
2428
final Hub _hub;
2529

2630
@override
27-
Batch batch() => SentryBatch(_executor.batch(), hub: _hub);
31+
Batch batch() => SentryBatch(_executor.batch(), hub: _hub, dbName: _dbName);
2832

2933
@override
3034
Database get database => _executor.database;
@@ -41,6 +45,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
4145
);
4246
// ignore: invalid_use_of_internal_member
4347
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
48+
setDatabaseAttributeData(span, _dbName);
4449

4550
try {
4651
final result =
@@ -68,8 +73,10 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
6873
SentryDatabase.dbSqlExecuteOp,
6974
description: sql,
7075
);
76+
span?.setData(SentryDatabase.dbSystemKey, SentryDatabase.dbSystem);
7177
// ignore: invalid_use_of_internal_member
7278
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
79+
setDatabaseAttributeData(span, _dbName);
7380

7481
try {
7582
await _executor.execute(sql, arguments);
@@ -107,6 +114,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
107114
);
108115
// ignore: invalid_use_of_internal_member
109116
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
117+
setDatabaseAttributeData(span, _dbName);
110118

111119
try {
112120
final result = await _executor.insert(
@@ -163,6 +171,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
163171
);
164172
// ignore: invalid_use_of_internal_member
165173
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
174+
setDatabaseAttributeData(span, _dbName);
166175

167176
try {
168177
final result = await _executor.query(
@@ -226,6 +235,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
226235
);
227236
// ignore: invalid_use_of_internal_member
228237
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
238+
setDatabaseAttributeData(span, _dbName);
229239

230240
try {
231241
final result = await _executor.queryCursor(
@@ -266,6 +276,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
266276
);
267277
// ignore: invalid_use_of_internal_member
268278
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
279+
setDatabaseAttributeData(span, _dbName);
269280

270281
try {
271282
final result = await _executor.rawDelete(sql, arguments);
@@ -294,6 +305,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
294305
);
295306
// ignore: invalid_use_of_internal_member
296307
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
308+
setDatabaseAttributeData(span, _dbName);
297309

298310
try {
299311
final result = await _executor.rawInsert(sql, arguments);
@@ -325,6 +337,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
325337
);
326338
// ignore: invalid_use_of_internal_member
327339
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
340+
setDatabaseAttributeData(span, _dbName);
328341

329342
try {
330343
final result = await _executor.rawQuery(sql, arguments);
@@ -357,6 +370,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
357370
);
358371
// ignore: invalid_use_of_internal_member
359372
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
373+
setDatabaseAttributeData(span, _dbName);
360374

361375
try {
362376
final result = await _executor.rawQueryCursor(
@@ -389,6 +403,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
389403
);
390404
// ignore: invalid_use_of_internal_member
391405
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
406+
setDatabaseAttributeData(span, _dbName);
392407

393408
try {
394409
final result = await _executor.rawUpdate(sql, arguments);
@@ -430,6 +445,7 @@ class SentryDatabaseExecutor implements DatabaseExecutor {
430445
);
431446
// ignore: invalid_use_of_internal_member
432447
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabaseExecutor;
448+
setDatabaseAttributeData(span, _dbName);
433449

434450
try {
435451
final result = await _executor.update(

sqflite/lib/src/sentry_sqflite_transaction.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ import 'sentry_batch.dart';
2121
class SentrySqfliteTransaction extends Transaction implements DatabaseExecutor {
2222
final DatabaseExecutor _executor;
2323
final Hub _hub;
24+
final String? _dbName;
2425

2526
// ignore: public_member_api_docs
2627
@internal
2728
SentrySqfliteTransaction(
2829
this._executor, {
2930
@internal Hub? hub,
30-
}) : _hub = hub ?? HubAdapter();
31+
@internal String? dbName,
32+
}) : _hub = hub ?? HubAdapter(),
33+
_dbName = dbName;
3134

3235
@override
33-
Batch batch() => SentryBatch(_executor.batch(), hub: _hub);
36+
Batch batch() => SentryBatch(_executor.batch(), hub: _hub, dbName: _dbName);
3437

3538
@override
3639
Database get database => _executor.database;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:sentry/sentry.dart';
2+
3+
import '../../sentry_sqflite.dart';
4+
5+
/// Sets the database attributes on the [span].
6+
/// It contains the database system and the database name.
7+
void setDatabaseAttributeData(ISentrySpan? span, String? dbName) {
8+
span?.setData(SentryDatabase.dbSystemKey, SentryDatabase.dbSystem);
9+
if (dbName != null) {
10+
span?.setData(SentryDatabase.dbNameKey, dbName);
11+
}
12+
}

sqflite/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
sqflite: ^2.0.0
1515
sqflite_common: ^2.0.0
1616
meta: ^1.3.0
17+
path: ^1.8.3
1718

1819
dev_dependencies:
1920
lints: ^2.0.0

sqflite/test/mocks/mocks.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ ISentrySpan startTransactionShim(
3333
DatabaseExecutor,
3434
],
3535
customMocks: [
36-
MockSpec<Hub>(fallbackGenerators: {#startTransaction: startTransactionShim})
36+
MockSpec<Hub>(
37+
fallbackGenerators: {#startTransaction: startTransactionShim}),
3738
],
3839
)
3940
void main() {}

sqflite/test/sentry_batch_test.dart

+33
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,39 @@ SELECT * FROM Product''';
285285
await db.close();
286286
});
287287

288+
test('apply creates db span with dbSystem and dbName attributes', () async {
289+
final db = await fixture.getDatabase();
290+
final batch = db.batch();
291+
292+
batch.insert('Product', <String, Object?>{'title': 'Product 1'});
293+
294+
await batch.apply();
295+
296+
final span = fixture.tracer.children.last;
297+
expect(span.data[SentryDatabase.dbSystemKey], SentryDatabase.dbSystem);
298+
expect(
299+
span.data[SentryDatabase.dbNameKey], (db as SentryDatabase).dbName);
300+
301+
await db.close();
302+
});
303+
304+
test('commit creates db span with dbSystem and dbName attributes',
305+
() async {
306+
final db = await fixture.getDatabase();
307+
final batch = db.batch();
308+
309+
batch.insert('Product', <String, Object?>{'title': 'Product 1'});
310+
311+
await batch.commit();
312+
313+
final span = fixture.tracer.children.last;
314+
expect(span.data[SentryDatabase.dbSystemKey], SentryDatabase.dbSystem);
315+
expect(
316+
span.data[SentryDatabase.dbNameKey], (db as SentryDatabase).dbName);
317+
318+
await db.close();
319+
});
320+
288321
tearDown(() {
289322
databaseFactory = sqfliteDatabaseFactoryDefault;
290323
});

0 commit comments

Comments
 (0)