Skip to content

Commit

Permalink
introduce base box to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Oct 16, 2023
1 parent 5881d84 commit 296e25e
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 178 deletions.
3 changes: 2 additions & 1 deletion dart/lib/src/sentry_trace_origins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ class SentryTraceOrigins {
'auto.db.sqflite.database_factory';

static const autoDbHive = 'auto.db.hive';
static const autoDbHiveBox = 'auto.db.hive.box';
static const autoDbHiveOpenDatabase = 'auto.db.hive.open_database';
static const autoDbHiveBaseBox = 'auto.db.hive.base_box';
}
49 changes: 49 additions & 0 deletions hive/example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:hive/hive.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_hive/sentry_hive.dart';

part 'main.g.dart';

Future<void> main() async {
// ATTENTION: Change the DSN below with your own to see the events in Sentry. Get one at sentry.io
const dsn =
'https://[email protected]/5428562';

await Sentry.init(
(options) {
options.dsn = dsn;
options.tracesSampleRate = 1.0;
options.debug = true;
},
appRunner: runApp, // Init your App.
);
}

Future<void> runApp() async {
Hive
..init(Directory.current.path)
..registerAdapter(PersonAdapter());

var box = await Hive.openBox('testBox');

var sentryBox = SentryBox

var person = Person(name: 'Dave', age: 23);

}

@HiveType(typeId: 1)
class Person {
Person({required this.name, required this.age});

@HiveField(0)
String name;

@HiveField(1)
int age;

@override
String toString() {
return '$name: $age';
}
}
6 changes: 0 additions & 6 deletions hive/example/hive_example.dart

This file was deleted.

172 changes: 5 additions & 167 deletions hive/lib/src/sentry_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,16 @@ import 'package:meta/meta.dart';
import 'package:hive/hive.dart';
import 'package:sentry/sentry.dart';

import 'sentry_hive_impl.dart';
import 'sentry_box_base.dart';

///
@experimental
class SentryBox<E> implements Box<E> {
class SentryBox<E> extends SentryBoxBase<E> implements Box<E> {

final Box<E> _box;
final Hub _hub;

// ignore: public_member_api_docs
SentryBox(this._box, @internal Hub? hub) : _hub = hub ?? HubAdapter();

@override
Future<int> add(E value) async {
return _asyncWrapInSpan('add', () async {
return await _box.add(value);
});
}

@override
Future<Iterable<int>> addAll(Iterable<E> values) async {
return _asyncWrapInSpan('addAll', () async {
return await _box.addAll(values);
});
}

@override
Future<int> clear() async {
return _asyncWrapInSpan('clear', () async {
return await _box.clear();
});
}

@override
Future<void> close() async {
return _asyncWrapInSpan('close', () async {
return await _box.close();
});
}

@override
Future<void> compact() async {
return _asyncWrapInSpan('compact', () async {
return await _box.compact();
});
}

@override
bool containsKey(key) {
return _box.containsKey(key);
}

@override
Future<void> delete(key) async {
return _asyncWrapInSpan('delete', () async {
return await _box.delete(key);
});
}

@override
// ignore: strict_raw_type
Future<void> deleteAll(Iterable keys) async {
return _asyncWrapInSpan('deleteAll', () async {
return await _box.deleteAll(keys);
});
}

@override
Future<void> deleteAt(int index) async {
return _asyncWrapInSpan('deleteAt', () async {
return await _box.deleteAt(index);
});
}

@override
Future<void> deleteFromDisk() async {
return _asyncWrapInSpan('deleteFromDisk', () async {
return await _box.deleteFromDisk();
});
}

@override
Future<void> flush() async {
return _asyncWrapInSpan('flush', () async {
return await _box.flush();
});
}
///
SentryBox(this._box, @internal Hub hub) : super(_box, hub);

@override
E? get(key, {E? defaultValue}) {
Expand All @@ -100,102 +23,17 @@ class SentryBox<E> implements Box<E> {
return _box.getAt(index);
}

@override
bool get isEmpty => _box.isEmpty;

@override
bool get isNotEmpty => _box.isNotEmpty;

@override
bool get isOpen => _box.isOpen;

@override
dynamic keyAt(int index) {
return _box.keyAt(index);
}

@override
// ignore: strict_raw_type
Iterable get keys => _box.keys;

@override
bool get lazy => _box.lazy;

@override
int get length => _box.length;

@override
String get name => _box.name;

@override
String? get path => _box.path;

@override
Future<void> put(key, E value) async {
return _asyncWrapInSpan('put', () async {
return await _box.put(key, value);
});
}

@override
Future<void> putAll(Map<dynamic, E> entries) async {
return _asyncWrapInSpan('putAll', () async {
return await _box.putAll(entries);
});
}

@override
Future<void> putAt(int index, E value) async {
return _asyncWrapInSpan('putAt', () async {
return await _box.putAt(index, value);
});
}

@override
Map<dynamic, E> toMap() {
return _box.toMap();
}

@override
// TODO: implement values
Iterable<E> get values => _box.values;

@override
Iterable<E> valuesBetween({startKey, endKey}) {
return _box.valuesBetween(startKey: startKey, endKey: endKey);
}

@override
Stream<BoxEvent> watch({key}) {
return _box.watch(key: key);
}

// Helper

Future<T> _asyncWrapInSpan<T>(String description, Future<T> Function() execute) async {
final currentSpan = _hub.getSpan();
final span = currentSpan?.startChild(
SentryHiveImpl.dbOp,
description: description,
);

// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbHiveBox;

span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem);
span?.setData(SentryHiveImpl.dbNameKey, name);

try {
final result = await execute();
span?.status = SpanStatus.ok();

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

rethrow;
} finally {
await span?.finish();
}
}
}
Loading

0 comments on commit 296e25e

Please sign in to comment.