Skip to content

Commit ed4ebff

Browse files
authored
feat(sentry-native): add options_set_native_database_path option (#3236)
* Update * Update * Update * Update * Update * Update CHANGELOG.md * Update sentry_flutter_options.dart
1 parent e04b24b commit ed4ebff

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Features
6+
7+
- Add `nativeDatabasePath` option to `SentryFlutterOptions` to set the database path for Sentry Native ([#3236](https://github.com/getsentry/sentry-dart/pull/3236))
8+
39
## 9.7.0-beta.3
410

511
### Fixes

packages/flutter/ffi-native.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ functions:
1919
- sentry_options_set_dist
2020
- sentry_options_set_max_breadcrumbs
2121
- sentry_options_set_handler_path
22+
- sentry_options_set_database_path
2223
- sentry_set_user
2324
- sentry_remove_user
2425
- sentry_add_breadcrumb

packages/flutter/lib/src/native/c/binding.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,52 @@ class SentryNative {
640640
void Function(
641641
ffi.Pointer<sentry_options_s>, ffi.Pointer<ffi.Char>)>();
642642

643+
/// Sets the path to the Sentry Database Directory.
644+
///
645+
/// Sentry will use this path to persist user consent, sessions, and other
646+
/// artifacts in case of a crash. This will also be used by the crashpad backend
647+
/// if it is configured.
648+
///
649+
/// The directory is used for "cached" data, which needs to persist across
650+
/// application restarts to ensure proper flagging of release-health sessions,
651+
/// but might otherwise be safely purged regularly.
652+
///
653+
/// It is roughly equivalent to the type of `AppData/Local` on Windows and
654+
/// `XDG_CACHE_HOME` on Linux, and equivalent runtime directories on other
655+
/// platforms.
656+
///
657+
/// It is recommended that users set an explicit absolute path, depending
658+
/// on their apps runtime directory. The path will be created if it does not
659+
/// exist, and will be resolved to an absolute path inside of `sentry_init`. The
660+
/// directory should not be shared with other application data/configuration, as
661+
/// sentry-native will enumerate and possibly delete files in that directory. An
662+
/// example might be `$XDG_CACHE_HOME/your-app/sentry`
663+
///
664+
/// If no explicit path it set, sentry-native will default to `.sentry-native` in
665+
/// the current working directory, with no specific platform-specific handling.
666+
///
667+
/// `path` is assumed to be in platform-specific filesystem path encoding.
668+
/// API Users on windows are encouraged to use
669+
/// `sentry_options_set_database_pathw` instead.
670+
void options_set_database_path(
671+
ffi.Pointer<sentry_options_s> opts,
672+
ffi.Pointer<ffi.Char> path,
673+
) {
674+
return _options_set_database_path(
675+
opts,
676+
path,
677+
);
678+
}
679+
680+
late final _options_set_database_pathPtr = _lookup<
681+
ffi.NativeFunction<
682+
ffi.Void Function(ffi.Pointer<sentry_options_s>,
683+
ffi.Pointer<ffi.Char>)>>('sentry_options_set_database_path');
684+
late final _options_set_database_path =
685+
_options_set_database_pathPtr.asFunction<
686+
void Function(
687+
ffi.Pointer<sentry_options_s>, ffi.Pointer<ffi.Char>)>();
688+
643689
/// Initializes the Sentry SDK with the specified options.
644690
///
645691
/// This takes ownership of the options. After the options have been set

packages/flutter/lib/src/native/c/sentry_native.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class SentryNative with SentryNativeSafeInvoker implements SentryNativeBinding {
6969
cOptions, options.enableAutoSessionTracking ? 1 : 0);
7070
native.options_set_dist(cOptions, c.str(options.dist));
7171
native.options_set_max_breadcrumbs(cOptions, options.maxBreadcrumbs);
72+
if (options.nativeDatabasePath != null) {
73+
native.options_set_database_path(
74+
cOptions, c.str(options.nativeDatabasePath));
75+
}
7276
if (options.proxy != null) {
7377
// sentry-native expects a single string and it doesn't support different types or authentication
7478
options.log(SentryLevel.warning,

packages/flutter/lib/src/sentry_flutter_options.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ class SentryFlutterOptions extends SentryOptions {
291291
/// Screen content masking is enabled by default.
292292
final privacy = SentryPrivacyOptions();
293293

294+
/// Specifies the file system path to the Sentry database directory
295+
/// used by the Sentry Native SDK.
296+
///
297+
/// ### Default
298+
/// If `null` (the default), the database directory is created at
299+
/// `.sentry-native` in the current working directory (CWD).
300+
///
301+
/// ### Recommendation
302+
/// While relying on the default path may be sufficient during development,
303+
/// **it is strongly recommended** to provide an explicit path in production
304+
/// environments. Doing so ensures consistent and predictable behavior across
305+
/// deployments.
306+
///
307+
/// ### Platform Support
308+
/// This option only applies to platforms that integrate the Sentry Native SDK
309+
/// directly:
310+
/// - **Linux (Desktop)**
311+
/// - **Windows (Desktop)**
312+
///
313+
/// ### References
314+
/// For additional details, see:
315+
/// https://docs.sentry.io/platforms/native/configuration/options/#database-path
316+
String? nativeDatabasePath;
317+
294318
/// By using this, you are disabling native [Breadcrumb] tracking and instead
295319
/// you are just tracking [Breadcrumb]s which result from events available
296320
/// in the current Flutter environment.

packages/flutter/test/sentry_native/sentry_native_test_ffi.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ void main() {
139139
await sut.init(MockHub());
140140
});
141141

142+
test('init creates native database path directory when configured',
143+
() async {
144+
final dbDir = Directory(
145+
'${helper.nativeTestRoot}/db-${backend.actualValue.name}');
146+
if (dbDir.existsSync()) {
147+
dbDir.deleteSync(recursive: true);
148+
}
149+
150+
options.nativeDatabasePath = dbDir.path;
151+
152+
addTearDown(() {
153+
if (dbDir.existsSync()) {
154+
dbDir.deleteSync(recursive: true);
155+
}
156+
});
157+
addTearDown(sut.close);
158+
159+
await sut.init(MockHub());
160+
161+
expect(dbDir.existsSync(), isTrue);
162+
});
163+
142164
test('app start', () {
143165
expect(sut.fetchNativeAppStart(), null);
144166
});

0 commit comments

Comments
 (0)