-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathsentry_database.dart
199 lines (175 loc) · 5.39 KB
/
sentry_database.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'package:sqflite/sqflite.dart';
import 'sentry_database_executor.dart';
import 'sentry_sqflite_transaction.dart';
import 'version.dart';
import 'utils/sentry_database_span_attributes.dart';
import 'package:path/path.dart' as p;
/// A [Database] wrapper that adds Sentry support.
///
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// ```
@experimental
class SentryDatabase extends SentryDatabaseExecutor implements Database {
final Database _database;
final Hub _hub;
@internal
// ignore: public_member_api_docs
static const dbOp = 'db';
@internal
// ignore: public_member_api_docs
static const dbSqlExecuteOp = 'db.sql.execute';
@internal
// ignore: public_member_api_docs
static const dbSqlQueryOp = 'db.sql.query';
static const _dbSqlOp = 'db.sql.transaction';
@internal
// ignore: public_member_api_docs
static const dbSystemKey = 'db.system';
@internal
// ignore: public_member_api_docs
static const dbSystem = 'sqlite';
@internal
// ignore: public_member_api_docs
static const dbNameKey = 'db.name';
@internal
// ignore: public_member_api_docs
String dbName;
/// ```dart
/// import 'package:sqflite/sqflite.dart';
/// import 'package:sentry_sqflite/sentry_sqflite.dart';
///
/// final database = await openDatabase('path/to/db');
/// final sentryDatabase = SentryDatabase(database);
/// ```
SentryDatabase(
this._database, {
@internal Hub? hub,
}) : _hub = hub ?? HubAdapter(),
dbName = p.basenameWithoutExtension(_database.path),
super(
_database,
hub: hub,
dbName: p.basenameWithoutExtension(_database.path),
) {
// ignore: invalid_use_of_internal_member
final options = _hub.options;
options.sdk.addIntegration('SentrySqfliteTracing');
options.sdk.addPackage(packageName, sdkVersion);
}
// TODO: check if perf is enabled
@override
Future<void> close() {
return Future<void>(() async {
final currentSpan = _hub.getSpan();
final description = 'Close DB: ${_database.path}';
final span = currentSpan?.startChild(
dbOp,
description: description,
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;
var breadcrumb = Breadcrumb(
message: description,
category: dbOp,
data: {},
type: 'query',
);
try {
await _database.close();
span?.status = SpanStatus.ok();
breadcrumb.data?['status'] = 'ok';
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();
breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);
rethrow;
} finally {
await span?.finish();
// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
@override
Future<T> devInvokeMethod<T>(String method, [Object? arguments]) {
// ignore: deprecated_member_use
return _database.devInvokeMethod(method, arguments);
}
@override
Future<T> devInvokeSqlMethod<T>(
String method,
String sql, [
List<Object?>? arguments,
]) {
// ignore: deprecated_member_use
return _database.devInvokeSqlMethod(method, sql);
}
@override
bool get isOpen => _database.isOpen;
@override
String get path => _database.path;
@override
Future<T> transaction<T>(
Future<T> Function(Transaction txn) action, {
bool? exclusive,
}) {
return Future<T>(() async {
final currentSpan = _hub.getSpan();
final description = 'Transaction DB: ${_database.path}';
final span = currentSpan?.startChild(
_dbSqlOp,
description: description,
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;
setDatabaseAttributeData(span, dbName);
var breadcrumb = Breadcrumb(
message: description,
category: _dbSqlOp,
data: {},
type: 'query',
);
setDatabaseAttributeOnBreadcrumb(breadcrumb, dbName);
Future<T> newAction(Transaction txn) async {
final executor = SentryDatabaseExecutor(
txn,
parentSpan: span,
hub: _hub,
dbName: dbName,
);
final sentrySqfliteTransaction =
SentrySqfliteTransaction(executor, hub: _hub, dbName: dbName);
return await action(sentrySqfliteTransaction);
}
try {
final result =
await _database.transaction(newAction, exclusive: exclusive);
span?.status = SpanStatus.ok();
breadcrumb.data?['status'] = 'ok';
return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();
breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);
rethrow;
} finally {
await span?.finish();
// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
}