-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add basic lifecycle instrumentation. #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+199
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...nchdarkly/launchdarkly_flutter_observability/lib/src/instrumentation/instrumentation.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /// Interfaces which instrumentations should implement. | ||
| interface class Instrumentation {} |
54 changes: 54 additions & 0 deletions
54
...darkly_flutter_observability/lib/src/instrumentation/lifecycle/lifecycle_conventions.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import 'dart:ui'; | ||
| import '../../api/attribute.dart'; | ||
|
|
||
| const _lifecycleSpanName = "device.app.lifecycle"; | ||
| const _flutterAppState = "flutter.app.state"; | ||
|
|
||
| enum LifecycleState { | ||
| detached('detached'), | ||
| resumed('resumed'), | ||
| inactive('inactive'), | ||
| hidden('hidden'), | ||
| paused('paused'); | ||
|
|
||
| final String stringValue; | ||
|
|
||
| const LifecycleState(String value) : stringValue = value; | ||
|
|
||
| @override | ||
| String toString() { | ||
| return stringValue; | ||
| } | ||
|
|
||
| static LifecycleState fromAppLifecycleState(AppLifecycleState state) { | ||
| switch (state) { | ||
| case AppLifecycleState.detached: | ||
| return LifecycleState.detached; | ||
| case AppLifecycleState.resumed: | ||
| return LifecycleState.resumed; | ||
| case AppLifecycleState.inactive: | ||
| return LifecycleState.inactive; | ||
| case AppLifecycleState.hidden: | ||
| return LifecycleState.hidden; | ||
| case AppLifecycleState.paused: | ||
| return LifecycleState.paused; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// LaunchDarkly specific lifecycle convention inspired by the otel mobile | ||
| /// events semantic convention. | ||
| final class LifecycleConventions { | ||
| static Map<String, Attribute> getAttributes({ | ||
| required AppLifecycleState state, | ||
| }) { | ||
| return { | ||
| _flutterAppState: StringAttribute( | ||
| LifecycleState.fromAppLifecycleState(state).toString(), | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| /// The name to use for the span. | ||
| static const spanName = _lifecycleSpanName; | ||
| } |
57 changes: 57 additions & 0 deletions
57
...ly_flutter_observability/lib/src/instrumentation/lifecycle/lifecycle_instrumentation.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import 'package:flutter/scheduler.dart'; | ||
| import 'package:launchdarkly_flutter_observability/launchdarkly_flutter_observability.dart'; | ||
| import 'package:launchdarkly_flutter_observability/src/instrumentation/instrumentation.dart'; | ||
| import 'package:launchdarkly_flutter_observability/src/instrumentation/lifecycle/lifecycle_conventions.dart'; | ||
|
|
||
| import '../../observe.dart'; | ||
| import 'platform/stub_lifecycle_listener.dart' | ||
| if (dart.library.io) 'platform/io_lifecycle_listener.dart' | ||
| if (dart.library.js_interop) 'platform/js_lifecycle_listener.dart'; | ||
|
|
||
| final class LifecycleInstrumentation implements Instrumentation { | ||
| late final LDAppLifecycleListener _lifecycleListener; | ||
|
|
||
| LifecycleInstrumentation() { | ||
| final initialState = SchedulerBinding.instance.lifecycleState; | ||
| if (initialState != null) { | ||
| _handleApplicationLifecycle(initialState); | ||
| } | ||
|
|
||
| _lifecycleListener = LDAppLifecycleListener(); | ||
| _lifecycleListener.stream.listen(_handleApplicationLifecycle); | ||
| } | ||
|
|
||
| /// The application lifecycle is as follows. | ||
| /// Diagram based on: https://api.flutter.dev/flutter/widgets/AppLifecycleListener-class.html | ||
| /// +-----------+ onStart +-----------+ | ||
| /// | +---------------------------> | | ||
| /// | Detached | | Resumed | | ||
| /// | | | | | ||
| /// +--------^--+ +-^-------+-+ | ||
| /// | | | | ||
| /// |onDetach onInactive| |onResume | ||
| /// | | | | ||
| /// | onPause | | | ||
| /// +--------+--+ +-----------+onHide +-+-------v-+ | ||
| /// | <-------+ <-------+ | | ||
| /// | Paused | | Hidden | | Inactive | | ||
| /// | +-------> +-------> | | ||
| /// +-----------+ +-----------+onShow +-----------+ | ||
| /// onRestart | ||
| /// | ||
| /// On iOS/Android the hidden state is synthesized in the process of pausing, | ||
| /// so it will always hide before being paused. On desktop/web platforms | ||
| /// hidden may happen when the app is covered. | ||
| void _handleApplicationLifecycle(AppLifecycleState state) { | ||
| Observe.startSpan( | ||
| LifecycleConventions.spanName, | ||
| attributes: LifecycleConventions.getAttributes(state: state), | ||
| ) | ||
| ..setStatus(SpanStatusCode.ok) | ||
| ..end(); | ||
| } | ||
|
|
||
| void dispose() { | ||
| _lifecycleListener.close(); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...utter_observability/lib/src/instrumentation/lifecycle/platform/io_lifecycle_listener.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| /// Lifecycle listener that uses the Flutter [AppLifecycleListener]. | ||
| /// Unfortunately, the [AppLifecycleListener] does not support web very well at | ||
| /// the moment, so the [LDAppLifecycleListener] was created. | ||
| class LDAppLifecycleListener { | ||
| late final StreamController<AppLifecycleState> _streamController; | ||
| AppLifecycleListener? _underlyingListener; | ||
|
|
||
| LDAppLifecycleListener() { | ||
| _streamController = StreamController.broadcast( | ||
| onListen: () { | ||
| _underlyingListener = AppLifecycleListener( | ||
| onStateChange: (state) => _streamController.add(state), | ||
| ); | ||
| }, | ||
| onCancel: () { | ||
| _underlyingListener?.dispose(); | ||
| _underlyingListener = null; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| Stream<AppLifecycleState> get stream => _streamController.stream; | ||
|
|
||
| void close() { | ||
| _streamController.close(); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...utter_observability/lib/src/instrumentation/lifecycle/platform/js_lifecycle_listener.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import 'dart:async'; | ||
| import 'dart:js_interop'; | ||
| import 'package:web/web.dart' as web; | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| /// Lifecycle listener that uses the underlying visibility of the html web | ||
| /// document to emit events. | ||
| class LDAppLifecycleListener { | ||
| late final StreamController<AppLifecycleState> _streamController; | ||
|
|
||
| LDAppLifecycleListener() { | ||
| _streamController = StreamController.broadcast(); | ||
|
|
||
| void listenerFunc(web.Event event) => _streamController.add( | ||
| web.document.hidden == true | ||
| ? AppLifecycleState.hidden | ||
| : AppLifecycleState.resumed, | ||
| ); | ||
|
|
||
| /// Use a stable reference for the JS listener. | ||
| final listenerJS = listenerFunc.toJS; | ||
|
|
||
| _streamController.onListen = () { | ||
| web.document.addEventListener('visibilitychange', listenerJS); | ||
| }; | ||
|
|
||
| _streamController.onCancel = () { | ||
| web.document.removeEventListener('visibilitychange', listenerJS); | ||
| }; | ||
| } | ||
|
|
||
| Stream<AppLifecycleState> get stream => _streamController.stream; | ||
|
|
||
| void close() { | ||
| _streamController.close(); | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
...ter_observability/lib/src/instrumentation/lifecycle/platform/stub_lifecycle_listener.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| class LDAppLifecycleListener { | ||
| Stream<AppLifecycleState> get stream => | ||
| throw Exception('Stub implementation'); | ||
|
|
||
| void close() { | ||
| throw Exception('Stub implementation'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.