Skip to content

Commit 2fa0411

Browse files
committed
Add SentryBox with first test
1 parent 7a9ea7d commit 2fa0411

10 files changed

+750
-20
lines changed

dart/lib/src/sentry_trace_origins.dart

+3
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ class SentryTraceOrigins {
1818
'auto.db.sqflite.database_executor';
1919
static const autoDbSqfliteDatabaseFactory =
2020
'auto.db.sqflite.database_factory';
21+
22+
static const autoDbHive = 'auto.db.hive';
23+
static const autoDbHiveBox = 'auto.db.hive.box';
2124
}

hive/lib/sentry_hive.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
library;
1+
library sentry_hive;
22

33
export 'src/sentry_hive.dart';
44
export 'src/sentry_box.dart';

hive/lib/src/sentry_box.dart

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:hive/hive.dart';
3+
import 'package:sentry/sentry.dart';
4+
5+
import '../sentry_hive.dart';
6+
7+
// ignore: public_member_api_docs
8+
class SentryBox<E> implements Box<E> {
9+
10+
final Box<E> _box;
11+
final Hub _hub;
12+
13+
// ignore: public_member_api_docs
14+
SentryBox(this._box, @internal Hub? hub) : _hub = hub ?? HubAdapter();
15+
16+
@override
17+
Future<int> add(E value) async {
18+
return _asyncWrapInSpan('add', () async {
19+
return await _box.add(value);
20+
});
21+
}
22+
23+
@override
24+
Future<Iterable<int>> addAll(Iterable<E> values) async {
25+
return _asyncWrapInSpan('addAll', () async {
26+
return await _box.addAll(values);
27+
});
28+
}
29+
30+
@override
31+
Future<int> clear() async {
32+
return _asyncWrapInSpan('clear', () async {
33+
return await _box.clear();
34+
});
35+
}
36+
37+
@override
38+
Future<void> close() async {
39+
return _asyncWrapInSpan('close', () async {
40+
return await _box.close();
41+
});
42+
}
43+
44+
@override
45+
Future<void> compact() async {
46+
return _asyncWrapInSpan('compact', () async {
47+
return await _box.compact();
48+
});
49+
}
50+
51+
@override
52+
bool containsKey(key) {
53+
return _box.containsKey(key);
54+
}
55+
56+
@override
57+
Future<void> delete(key) async {
58+
return _asyncWrapInSpan('delete', () async {
59+
return await _box.delete(key);
60+
});
61+
}
62+
63+
@override
64+
// ignore: strict_raw_type
65+
Future<void> deleteAll(Iterable keys) async {
66+
return _asyncWrapInSpan('delete', () async {
67+
return await _box.deleteAll(keys);
68+
});
69+
}
70+
71+
@override
72+
Future<void> deleteAt(int index) async {
73+
return _asyncWrapInSpan('deleteAt', () async {
74+
return await _box.deleteAt(index);
75+
});
76+
}
77+
78+
@override
79+
Future<void> deleteFromDisk() async {
80+
return _asyncWrapInSpan('deleteFromDisk', () async {
81+
return await _box.deleteFromDisk();
82+
});
83+
}
84+
85+
@override
86+
Future<void> flush() async {
87+
return _asyncWrapInSpan('flush', () async {
88+
return await _box.flush();
89+
});
90+
}
91+
92+
@override
93+
E? get(key, {E? defaultValue}) {
94+
return _box.get(key, defaultValue: defaultValue);
95+
}
96+
97+
@override
98+
E? getAt(int index) {
99+
return _box.getAt(index);
100+
}
101+
102+
@override
103+
bool get isEmpty => _box.isEmpty;
104+
105+
@override
106+
bool get isNotEmpty => _box.isNotEmpty;
107+
108+
@override
109+
bool get isOpen => _box.isOpen;
110+
111+
@override
112+
dynamic keyAt(int index) {
113+
return _box.keyAt(index);
114+
}
115+
116+
@override
117+
// ignore: strict_raw_type
118+
Iterable get keys => _box.keys;
119+
120+
@override
121+
bool get lazy => _box.lazy;
122+
123+
@override
124+
int get length => _box.length;
125+
126+
@override
127+
String get name => _box.name;
128+
129+
@override
130+
String? get path => _box.path;
131+
132+
@override
133+
Future<void> put(key, E value) async {
134+
return _asyncWrapInSpan('put', () async {
135+
return await _box.put(key, value);
136+
});
137+
}
138+
139+
@override
140+
Future<void> putAll(Map<dynamic, E> entries) async {
141+
return _asyncWrapInSpan('putAll', () async {
142+
return await _box.putAll(entries);
143+
});
144+
}
145+
146+
@override
147+
Future<void> putAt(int index, E value) async {
148+
return _asyncWrapInSpan('putAt', () async {
149+
return await _box.putAt(index, value);
150+
});
151+
}
152+
153+
@override
154+
Map<dynamic, E> toMap() {
155+
return _box.toMap();
156+
}
157+
158+
@override
159+
Iterable<E> get values => _box.values;
160+
161+
@override
162+
Iterable<E> valuesBetween({startKey, endKey}) {
163+
return _box.valuesBetween(startKey: startKey, endKey: endKey);
164+
}
165+
166+
@override
167+
Stream<BoxEvent> watch({key}) {
168+
return _box.watch(key: key);
169+
}
170+
171+
// Helper
172+
173+
Future<T> _asyncWrapInSpan<T>(String description, Future<T> Function() execute) async {
174+
final currentSpan = _hub.getSpan();
175+
final span = currentSpan?.startChild(
176+
SentryHive.dbOp,
177+
description: description,
178+
);
179+
180+
// ignore: invalid_use_of_internal_member
181+
span?.origin = SentryTraceOrigins.autoDbHiveBox;
182+
183+
span?.setData(SentryHive.dbSystemKey, SentryHive.dbSystem);
184+
span?.setData(SentryHive.dbNameKey, name);
185+
186+
try {
187+
final result = await execute();
188+
span?.status = SpanStatus.ok();
189+
190+
return result;
191+
} catch (exception) {
192+
span?.throwable = exception;
193+
span?.status = SpanStatus.internalError();
194+
195+
rethrow;
196+
} finally {
197+
await span?.finish();
198+
}
199+
}
200+
}

hive/lib/src/sentry_hive.dart

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
// TODO: Put public facing types in this file.
1+
import 'package:meta/meta.dart';
22

33
/// Checks if you are awesome. Spoiler: you are.
4-
class Awesome {
5-
bool get isAwesome => true;
4+
class SentryHive {
5+
@internal
6+
// ignore: public_member_api_docs
7+
static const dbOp = 'db';
8+
9+
@internal
10+
// ignore: public_member_api_docs
11+
static const dbSystemKey = 'db.system';
12+
@internal
13+
// ignore: public_member_api_docs
14+
static const dbSystem = 'sqlite';
15+
16+
@internal
17+
// ignore: public_member_api_docs
18+
static const dbNameKey = 'db.name';
619
}

hive/pubspec.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ environment:
1111
dependencies:
1212
sentry: 7.10.1
1313
hive: ^2.2.3
14+
meta: ^1.3.0
1415

1516
dev_dependencies:
1617
lints: ^2.0.0
1718
test: ^1.21.0
1819
yaml: ^3.1.0 # needed for version match (code and pubspec)
20+
mockito: ^5.1.0
21+
build_runner: ^2.4.2
22+
path_provider: ^2.1.1

hive/test/common.dart

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'dart:ffi';
2+
import 'dart:io';
3+
import 'dart:math';

hive/test/mocks/mocks.dart

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:mockito/annotations.dart';
2+
import 'package:sentry/sentry.dart';
3+
4+
@GenerateMocks(
5+
[
6+
Hub,
7+
]
8+
)
9+
void main() {}

0 commit comments

Comments
 (0)