-
-
Notifications
You must be signed in to change notification settings - Fork 275
Add sentry.replay_id
to flutter logs
#3257
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
Open
denrase
wants to merge
22
commits into
main
Choose a base branch
from
enha/logs-replay-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+477
−16
Open
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
234bb86
Add `sentry.replay_id` to flutter logs
denrase c365316
add cl entry
denrase 006a60d
Merge branch 'main' into enha/logs-replay-id
denrase f0030f0
Add `sentry._internal.replay_is_buffering` attribute
denrase 91562e2
remove in client
denrase 1fafd0c
remove old test
denrase 0c6d307
update logic
denrase 02f86c3
format
denrase 8488d63
Merge branch 'main' into enha/logs-replay-id
denrase 9e888b6
update replay id handling from ios/android native
denrase 1e63021
Merge branch 'main' into enha/logs-replay-id
denrase ccd6a45
Merge branch 'main' into enha/logs-replay-id
denrase b892b55
remove newline
denrase 8b1e81e
fix cl
denrase 506f5cb
additionally check options
denrase f98c32f
Merge branch 'enha/logs-replay-id' of github.com:getsentry/sentry-dar…
denrase 744d429
check args
denrase 56579e7
Merge branch 'main' into enha/logs-replay-id
denrase a279e8d
Change how native side reports the replay id
denrase 34436c7
curreclty check if in buffering mode
denrase 2b5369f
add override annotation
denrase 7980011
fix cl
denrase 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
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
51 changes: 51 additions & 0 deletions
51
packages/flutter/lib/src/integrations/replay_log_integration.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,51 @@ | ||
// ignore_for_file: invalid_use_of_internal_member | ||
|
||
import 'package:sentry/sentry.dart'; | ||
import '../sentry_flutter_options.dart'; | ||
|
||
/// Integration that adds replay-related information to logs using lifecycle callbacks | ||
class ReplayLogIntegration implements Integration<SentryFlutterOptions> { | ||
static const String integrationName = 'ReplayLog'; | ||
|
||
SentryFlutterOptions? _options; | ||
SdkLifecycleCallback<OnBeforeCaptureLog>? _addReplayInformation; | ||
|
||
@override | ||
Future<void> call(Hub hub, SentryFlutterOptions options) async { | ||
_options = options; | ||
_addReplayInformation = (OnBeforeCaptureLog event) { | ||
final hasActiveReplay = hub.scope.replayId != null; | ||
|
||
if (hasActiveReplay) { | ||
event.log.attributes['sentry.replay_id'] = SentryLogAttribute.string( | ||
hub.scope.replayId.toString(), | ||
); | ||
} | ||
|
||
final isReplayEnabled = (options.replay.onErrorSampleRate ?? 0) > 0; | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
buenaflor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (isReplayEnabled) { | ||
event.log.attributes['sentry._internal.replay_is_buffering'] = | ||
SentryLogAttribute.bool( | ||
!hasActiveReplay, | ||
); | ||
} | ||
denrase marked this conversation as resolved.
Show resolved
Hide resolved
denrase marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
options.lifecycleRegistry | ||
.registerCallback<OnBeforeCaptureLog>(_addReplayInformation!); | ||
options.sdk.addIntegration(integrationName); | ||
} | ||
|
||
@override | ||
Future<void> close() async { | ||
final options = _options; | ||
final addReplayInformation = _addReplayInformation; | ||
|
||
if (options != null && addReplayInformation != null) { | ||
options.lifecycleRegistry | ||
.removeCallback<OnBeforeCaptureLog>(addReplayInformation); | ||
} | ||
|
||
_options = null; | ||
_addReplayInformation = null; | ||
} | ||
} |
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
192 changes: 192 additions & 0 deletions
192
packages/flutter/test/integrations/replay_log_integration_test.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,192 @@ | ||
// ignore_for_file: invalid_use_of_internal_member | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:sentry_flutter/sentry_flutter.dart'; | ||
import 'package:sentry_flutter/src/integrations/replay_log_integration.dart'; | ||
|
||
import '../mocks.mocks.dart'; | ||
|
||
void main() { | ||
late Fixture fixture; | ||
|
||
setUp(() { | ||
fixture = Fixture(); | ||
}); | ||
|
||
group('ReplayLogIntegration', () { | ||
test('adds replay_id attribute when replay is active', () async { | ||
final integration = fixture.getSut(); | ||
|
||
fixture.options.replay.onErrorSampleRate = 0.5; | ||
fixture.hub.scope.replayId = SentryId.fromId('test-replay-id'); | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes['sentry.replay_id']?.value, 'testreplayid'); | ||
expect(log.attributes['sentry.replay_id']?.type, 'string'); | ||
|
||
expect( | ||
log.attributes['sentry._internal.replay_is_buffering']?.value, false); | ||
expect(log.attributes['sentry._internal.replay_is_buffering']?.type, | ||
'boolean'); | ||
}); | ||
|
||
test('adds replay buffering flag when replay is enabled but not active', | ||
() async { | ||
final integration = fixture.getSut(); | ||
fixture.options.replay.onErrorSampleRate = 0.5; | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes.containsKey('sentry.replay_id'), false); | ||
|
||
expect( | ||
log.attributes['sentry._internal.replay_is_buffering']?.value, true); | ||
expect(log.attributes['sentry._internal.replay_is_buffering']?.type, | ||
'boolean'); | ||
}); | ||
|
||
test('does not add buffering flag when onErrorSampleRate is disabled', | ||
() async { | ||
final integration = fixture.getSut(); | ||
fixture.options.replay.onErrorSampleRate = 0.0; | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes.containsKey('sentry.replay_id'), false); | ||
expect(log.attributes.containsKey('sentry._internal.replay_is_buffering'), | ||
false); | ||
}); | ||
|
||
test( | ||
'adds replay_id but not buffering flag when onErrorSampleRate is disabled', | ||
() async { | ||
final integration = fixture.getSut(); | ||
fixture.options.replay.onErrorSampleRate = 0.0; | ||
fixture.hub.scope.replayId = SentryId.fromId('test-replay-id'); | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes['sentry.replay_id']?.value, 'testreplayid'); | ||
expect(log.attributes['sentry.replay_id']?.type, 'string'); | ||
|
||
expect(log.attributes.containsKey('sentry._internal.replay_is_buffering'), | ||
false); | ||
}); | ||
|
||
test('does not add buffering flag when onErrorSampleRate is null', | ||
() async { | ||
final integration = fixture.getSut(); | ||
|
||
fixture.options.replay.onErrorSampleRate = null; | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes.containsKey('sentry.replay_id'), false); | ||
expect(log.attributes.containsKey('sentry._internal.replay_is_buffering'), | ||
false); | ||
}); | ||
|
||
test('adds replay_id but not buffering flag when onErrorSampleRate is null', | ||
() async { | ||
final integration = fixture.getSut(); | ||
fixture.options.replay.onErrorSampleRate = null; | ||
fixture.hub.scope.replayId = SentryId.fromId('test-replay-id'); | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes['sentry.replay_id']?.value, 'testreplayid'); | ||
expect(log.attributes['sentry.replay_id']?.type, 'string'); | ||
|
||
expect(log.attributes.containsKey('sentry._internal.replay_is_buffering'), | ||
false); | ||
}); | ||
|
||
test('registers integration name in SDK', () async { | ||
final integration = fixture.getSut(); | ||
|
||
fixture.options.replay.onErrorSampleRate = 0.5; | ||
fixture.hub.scope.replayId = SentryId.fromId('test-replay-id'); | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
|
||
// Integration name is registered in SDK | ||
expect(fixture.options.sdk.integrations.contains('ReplayLog'), true); | ||
}); | ||
|
||
test('removes callback on close', () async { | ||
final integration = fixture.getSut(); | ||
|
||
fixture.options.replay.onErrorSampleRate = 0.5; | ||
fixture.hub.scope.replayId = SentryId.fromId('test-replay-id'); | ||
|
||
await integration.call(fixture.hub, fixture.options); | ||
await integration.close(); | ||
|
||
final log = fixture.createTestLog(); | ||
await fixture.hub.captureLog(log); | ||
|
||
expect(log.attributes.containsKey('sentry.replay_id'), false); | ||
expect(log.attributes.containsKey('sentry._internal.replay_is_buffering'), | ||
false); | ||
}); | ||
|
||
test('integration name is correct', () { | ||
expect(ReplayLogIntegration.integrationName, 'ReplayLog'); | ||
}); | ||
}); | ||
} | ||
|
||
class Fixture { | ||
final options = | ||
SentryFlutterOptions(dsn: 'https://[email protected]/1234567'); | ||
final hub = MockHub(); | ||
|
||
Fixture() { | ||
options.enableLogs = true; | ||
options.environment = 'test'; | ||
options.release = 'test-release'; | ||
|
||
final scope = Scope(options); | ||
when(hub.options).thenReturn(options); | ||
when(hub.scope).thenReturn(scope); | ||
when(hub.captureLog(any)).thenAnswer((invocation) async { | ||
final log = invocation.positionalArguments.first as SentryLog; | ||
// Trigger the lifecycle callback | ||
await options.lifecycleRegistry.dispatchCallback(OnBeforeCaptureLog(log)); | ||
}); | ||
} | ||
|
||
SentryLog createTestLog() { | ||
return SentryLog( | ||
timestamp: DateTime.now(), | ||
traceId: SentryId.newId(), | ||
level: SentryLogLevel.info, | ||
body: 'test log message', | ||
attributes: <String, SentryLogAttribute>{}, | ||
); | ||
} | ||
|
||
ReplayLogIntegration getSut() { | ||
return ReplayLogIntegration(); | ||
} | ||
} |
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
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.