Skip to content

Commit 0a7552c

Browse files
buenaflordenrase
andauthored
enh: use lifecycle hook for before send event (#3017)
* Add os and device attributes to Flutter logs * cleanup + changelog * dont return context * update cl * extract reading of os and device * fix cl * extract contexts enrichment * cleanup * update cl * fix test * change to hooks api approach * update client tests * update tests * update cl * test for LogsEnricherIntegration * fix error * fix analyzer issues * update integration name * use generic api to register lifecycle events * use function type * move to sep file * check if logs are enabled * Remove `LogsEnricherIntegration` in load contexts integration * update test with real expected values * add comment * fix analyze issue * fix type casts * fix cl * Update * Update * Update * Update * Update * Update * Fix analyze * Update * Update * Update * Updaet * Update * Use empty sentry options * Update tests --------- Co-authored-by: Denis Andrasec <denrase@gmail.com>
1 parent 0fb45d0 commit 0a7552c

22 files changed

+658
-432
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#8170)
99
- [diff](https://github.com/getsentry/sentry-java/compare/8.13.2...8.17.0)
1010

11+
### Internal
12+
13+
- Use lifecycle hook for before send event ([#3017](https://github.com/getsentry/sentry-dart/pull/3017))
14+
1115
## 9.5.0
1216

1317
### Features

dart/lib/sentry.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export 'src/integration.dart';
2121
export 'src/noop_isolate_error_integration.dart'
2222
if (dart.library.io) 'src/isolate_error_integration.dart';
2323
// ignore: invalid_export_of_internal_element
24-
export 'src/observers.dart';
2524
export 'src/performance_collector.dart';
2625
export 'src/protocol.dart';
2726
export 'src/protocol/sentry_feature_flag.dart';
@@ -38,7 +37,7 @@ export 'src/sentry_baggage.dart';
3837
// ignore: invalid_export_of_internal_element
3938
export 'src/sentry_client.dart';
4039
// ignore: invalid_export_of_internal_element
41-
export 'src/lifecycle/on_before_capture_log.dart';
40+
export 'src/sdk_lifecycle_hooks.dart';
4241
export 'src/sentry_envelope.dart';
4342
export 'src/sentry_envelope_item.dart';
4443
export 'src/sentry_options.dart';

dart/lib/src/hub.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,19 @@ class Hub {
651651
SentryProfilerFactory? _profilerFactory;
652652

653653
@internal
654-
void registerCallback<T extends SdkLifecycleEvent>(
654+
Map<Type, List<Function>> get lifecycleCallbacks =>
655+
_peek().client.lifeCycleRegistry.lifecycleCallbacks;
656+
657+
@internal
658+
void registerSdkLifecycleCallback<T extends SdkLifecycleEvent>(
659+
SdkLifecycleCallback<T> callback) {
660+
_peek().client.lifeCycleRegistry.registerCallback<T>(callback);
661+
}
662+
663+
@internal
664+
void removeSdkLifecycleCallback<T extends SdkLifecycleEvent>(
655665
SdkLifecycleCallback<T> callback) {
656-
final item = _peek();
657-
item.client.registerCallback<T>(callback);
666+
_peek().client.lifeCycleRegistry.removeCallback<T>(callback);
658667
}
659668

660669
SentryEvent _assignTraceContext(SentryEvent event) {

dart/lib/src/hub_adapter.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'profiling.dart';
88
import 'protocol.dart';
99
import 'protocol/sentry_feedback.dart';
1010
import 'scope.dart';
11+
import 'sdk_lifecycle_hooks.dart';
1112
import 'sentry.dart';
1213
import 'sentry_client.dart';
1314
import 'sentry_options.dart';
@@ -201,8 +202,18 @@ class HubAdapter implements Hub {
201202
FutureOr<void> captureLog(SentryLog log) => Sentry.currentHub.captureLog(log);
202203

203204
@override
204-
void registerCallback<T extends SdkLifecycleEvent>(
205+
Map<Type, List<Function>> get lifecycleCallbacks =>
206+
Sentry.currentHub.lifecycleCallbacks;
207+
208+
@override
209+
void registerSdkLifecycleCallback<T extends SdkLifecycleEvent>(
210+
SdkLifecycleCallback<T> callback) {
211+
Sentry.currentHub.registerSdkLifecycleCallback(callback);
212+
}
213+
214+
@override
215+
void removeSdkLifecycleCallback<T extends SdkLifecycleEvent>(
205216
SdkLifecycleCallback<T> callback) {
206-
Sentry.currentHub.registerCallback(callback);
217+
Sentry.currentHub.removeSdkLifecycleCallback(callback);
207218
}
208219
}

dart/lib/src/lifecycle/on_before_capture_log.dart

Lines changed: 0 additions & 9 deletions
This file was deleted.

dart/lib/src/logs_enricher_integration.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import 'dart:async';
22
import 'package:meta/meta.dart';
33

4+
import 'sdk_lifecycle_hooks.dart';
45
import 'utils/os_utils.dart';
56
import 'integration.dart';
67
import 'hub.dart';
78
import 'protocol/sentry_log_attribute.dart';
89
import 'sentry_options.dart';
9-
import 'lifecycle/on_before_capture_log.dart';
1010

1111
@internal
1212
class LogsEnricherIntegration extends Integration<SentryOptions> {
@@ -15,7 +15,7 @@ class LogsEnricherIntegration extends Integration<SentryOptions> {
1515
@override
1616
FutureOr<void> call(Hub hub, SentryOptions options) {
1717
if (options.enableLogs) {
18-
hub.registerCallback<OnBeforeCaptureLog>(
18+
hub.registerSdkLifecycleCallback<OnBeforeCaptureLog>(
1919
(event) async {
2020
final os = getSentryOperatingSystem();
2121

dart/lib/src/noop_hub.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'profiling.dart';
88
import 'protocol.dart';
99
import 'protocol/sentry_feedback.dart';
1010
import 'scope.dart';
11+
import 'sdk_lifecycle_hooks.dart';
1112
import 'sentry_client.dart';
1213
import 'sentry_options.dart';
1314
import 'tracing.dart';
@@ -145,8 +146,14 @@ class NoOpHub implements Hub {
145146
@override
146147
Scope get scope => Scope(_options);
147148

148-
@internal
149149
@override
150-
void registerCallback<T extends SdkLifecycleEvent>(
150+
Map<Type, List<Function>> get lifecycleCallbacks => {};
151+
152+
@override
153+
void registerSdkLifecycleCallback<T extends SdkLifecycleEvent>(
154+
SdkLifecycleCallback<T> callback) {}
155+
156+
@override
157+
void removeSdkLifecycleCallback<T extends SdkLifecycleEvent>(
151158
SdkLifecycleCallback<T> callback) {}
152159
}

dart/lib/src/noop_sentry_client.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import 'hint.dart';
44
import 'protocol.dart';
55
import 'protocol/sentry_feedback.dart';
66
import 'scope.dart';
7+
import 'sdk_lifecycle_hooks.dart';
78
import 'sentry_client.dart';
89
import 'sentry_envelope.dart';
10+
import 'sentry_options.dart';
911
import 'sentry_trace_context_header.dart';
1012

1113
class NoOpSentryClient implements SentryClient {
@@ -70,7 +72,8 @@ class NoOpSentryClient implements SentryClient {
7072
@override
7173
FutureOr<void> captureLog(SentryLog log, {Scope? scope}) async {}
7274

75+
final _lifeCycleRegistry = SdkLifecycleRegistry(SentryOptions.empty());
76+
7377
@override
74-
void registerCallback<T extends SdkLifecycleEvent>(
75-
SdkLifecycleCallback<T> callback) {}
78+
SdkLifecycleRegistry get lifeCycleRegistry => _lifeCycleRegistry;
7679
}

dart/lib/src/observers.dart

Lines changed: 0 additions & 18 deletions
This file was deleted.

dart/lib/src/sdk_lifecycle_hooks.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'dart:async';
2+
3+
import 'package:meta/meta.dart';
4+
5+
import '../sentry.dart';
6+
7+
@internal
8+
typedef SdkLifecycleCallback<T extends SdkLifecycleEvent> = FutureOr<void>
9+
Function(T event);
10+
11+
@internal
12+
abstract class SdkLifecycleEvent {}
13+
14+
/// Holds and dispatches SDK lifecycle events in a type-safe way.
15+
/// These are meant to be used internally and are not part of public api.
16+
@internal
17+
class SdkLifecycleRegistry {
18+
SdkLifecycleRegistry(this._options);
19+
20+
final SentryOptions _options;
21+
final _lifecycleCallbacks = <Type, List<Function>>{};
22+
23+
Map<Type, List<Function>> get lifecycleCallbacks => _lifecycleCallbacks;
24+
25+
void registerCallback<T extends SdkLifecycleEvent>(
26+
SdkLifecycleCallback<T> callback) {
27+
_lifecycleCallbacks[T] ??= [];
28+
_lifecycleCallbacks[T]?.add(callback);
29+
}
30+
31+
void removeCallback<T extends SdkLifecycleEvent>(
32+
SdkLifecycleCallback<T> callback) {
33+
final callbacks = _lifecycleCallbacks[T];
34+
callbacks?.remove(callback);
35+
}
36+
37+
FutureOr<void> dispatchCallback<T extends SdkLifecycleEvent>(T event) async {
38+
final callbacks = _lifecycleCallbacks[event.runtimeType] ?? [];
39+
for (final cb in callbacks) {
40+
try {
41+
final result = (cb as SdkLifecycleCallback<T>)(event);
42+
if (result is Future) {
43+
await result;
44+
}
45+
} catch (exception, stackTrace) {
46+
_options.log(
47+
SentryLevel.error,
48+
'The SDK lifecycle callback threw an exception',
49+
exception: exception,
50+
stackTrace: stackTrace,
51+
);
52+
if (_options.automatedTestMode) {
53+
rethrow;
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
@internal
61+
class OnBeforeCaptureLog extends SdkLifecycleEvent {
62+
OnBeforeCaptureLog(this.log);
63+
64+
final SentryLog log;
65+
}
66+
67+
@internal
68+
class OnBeforeSendEvent extends SdkLifecycleEvent {
69+
OnBeforeSendEvent(this.event, this.hint);
70+
71+
final SentryEvent event;
72+
final Hint hint;
73+
}

0 commit comments

Comments
 (0)