-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.dart
100 lines (86 loc) · 2.74 KB
/
main.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
import 'dart:convert';
import 'dart:io';
import 'package:cbl/cbl.dart';
import 'package:cbl_dart/cbl_dart.dart';
import 'package:cbl_sentry/cbl_sentry.dart';
import 'package:sentry/sentry.dart';
Future<void> main() async {
await Sentry.init(
(options) => options
..dsn = File.fromUri(Uri.parse('sentry_dsn.txt')).readAsStringSync()
..tracesSampleRate = 1
..addIntegration(CouchbaseLiteIntegration()),
appRunner: runApp,
);
await Sentry.close();
}
late final Database db;
late final Collection users;
Future<void> runApp() async {
await initApp();
try {
await doStuff();
} finally {
await shutDownApp();
}
}
Future<void> initApp() => runAppTransaction('initApp', () async {
await CouchbaseLiteDart.init(edition: Edition.community);
await Database.remove('example');
db = await Database.openAsync('example');
users = await db.createCollection('users');
});
Future<void> shutDownApp() => runAppTransaction('shutDownApp', () async {
await db.close();
});
Future<void> doStuff() => runAppTransaction('doStuff', () async {
await fillDatabase();
await queryDatabase();
// throw Exception('Triggering exception event...');
});
Future<void> fillDatabase() => runAppOperation('fillDatabase', () async {
await users.saveDocument(MutableDocument({
'name': 'Alice',
'age': 25,
}));
await users.saveDocument(MutableDocument({
'name': 'Bob',
'age': 57,
}));
await users.saveDocument(MutableDocument({
'name': 'Sohla',
'age': 36,
}));
});
Future<void> queryDatabase() => runAppOperation('queryDatabase', () async {
final query = await db.createQuery(
'SELECT * FROM users WHERE age >= 28 OR name LIKE "A%"',
);
final resultSet = await query.execute();
final results = await resultSet
.asStream()
.map((result) => result.toPlainMap())
.toList();
prettyPrintJson(results);
});
Future<T> runAppTransaction<T>(String name, Future<T> Function() fn) =>
_runAppSpan(Sentry.startTransaction(name, 'task'), fn);
Future<T> runAppOperation<T>(String name, Future<T> Function() fn) =>
_runAppSpan(cblSentrySpan!.startChild(name), fn);
Future<T> _runAppSpan<T>(ISentrySpan span, Future<T> Function() fn) async {
try {
return await runWithCblSentrySpan(span, fn);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
span
..throwable = e
..status = const SpanStatus.internalError();
rethrow;
} finally {
span.status ??= const SpanStatus.ok();
await span.finish();
}
}
void prettyPrintJson(Object? value) =>
// ignore: avoid_print
print(const JsonEncoder.withIndent(' ').convert(value));