-
-
Notifications
You must be signed in to change notification settings - Fork 278
fix: TTID/TTFD transaction for the root page #3099
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
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
20612cd
Fix TTID/TTFD not being created for root
buenaflor 4654477
Update
buenaflor 26ad5f9
Update test
buenaflor e585f0e
Fix analyze
buenaflor 9759b94
Merge branch 'main' into fix/web-ttid-for-root
buenaflor 524bf72
Update
buenaflor 5908292
Update
buenaflor 3e8e8a1
Update test
buenaflor 1249055
Update test
buenaflor e0febd6
Update
buenaflor 920cd13
Update
buenaflor b44fc1a
Update
buenaflor f5cf0f9
Update test
buenaflor 6cb5a00
Update assert
buenaflor 9588357
Set origin to native app start as well
buenaflor 847ba72
Update
buenaflor f046892
Update
buenaflor c3a2586
Update
buenaflor 030cff0
Update
buenaflor ccf8649
Update
buenaflor 4cdba90
Merge branch 'main' into fix/web-ttid-for-root
buenaflor 5fcfc50
Add comment
buenaflor 27648fb
Update
buenaflor 1b80841
Update
buenaflor d183fd2
Update
buenaflor d4595eb
Review
buenaflor 6c4b5b3
Review
buenaflor 5b072d3
Review
buenaflor 38b3b80
Review
buenaflor 57722c4
Update
buenaflor fda1bd1
Update
buenaflor 0740f6f
Update
buenaflor d684ddc
Update
buenaflor def5eaf
Analyze
buenaflor 52c537c
Update
buenaflor 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
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,195 @@ | ||
| import 'package:sentry/sentry.dart'; | ||
| import 'package:sentry/src/platform/mock_platform.dart'; | ||
| import 'package:sentry/src/sentry_tracer.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import 'mocks/mock_client_report_recorder.dart'; | ||
| import 'mocks/mock_log_batcher.dart'; | ||
| import 'mocks/mock_transport.dart'; | ||
| import 'sentry_client_test.dart'; | ||
| import 'test_utils.dart'; | ||
| import 'utils/url_details_test.dart'; | ||
|
|
||
| void main() { | ||
| group('SDK lifecycle callbacks', () { | ||
| late Fixture fixture; | ||
|
|
||
| setUp(() => fixture = Fixture()); | ||
|
|
||
| group('Logs', () { | ||
| SentryLog givenLog() { | ||
| return SentryLog( | ||
| timestamp: DateTime.now(), | ||
| traceId: SentryId.newId(), | ||
| level: SentryLogLevel.info, | ||
| body: 'test', | ||
| attributes: { | ||
| 'attribute': SentryLogAttribute.string('value'), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| test('captureLog triggers OnBeforeCaptureLog', () async { | ||
| fixture.options.enableLogs = true; | ||
| fixture.options.environment = 'test-environment'; | ||
| fixture.options.release = 'test-release'; | ||
|
|
||
| final log = givenLog(); | ||
|
|
||
| final scope = Scope(fixture.options); | ||
| final span = MockSpan(); | ||
| scope.span = span; | ||
|
|
||
| final client = fixture.getSut(); | ||
| fixture.options.logBatcher = MockLogBatcher(); | ||
|
|
||
| client.lifeCycleRegistry.registerCallback<OnBeforeCaptureLog>((event) { | ||
| event.log.attributes['test'] = | ||
| SentryLogAttribute.string('test-value'); | ||
| }); | ||
|
|
||
| await client.captureLog(log, scope: scope); | ||
|
|
||
| final mockLogBatcher = fixture.options.logBatcher as MockLogBatcher; | ||
| expect(mockLogBatcher.addLogCalls.length, 1); | ||
| final capturedLog = mockLogBatcher.addLogCalls.first; | ||
|
|
||
| expect(capturedLog.attributes['test']?.value, "test-value"); | ||
| expect(capturedLog.attributes['test']?.type, 'string'); | ||
| }); | ||
| }); | ||
|
|
||
| group('SentryEvent', () { | ||
| test('captureEvent triggers OnBeforeSendEvent', () async { | ||
| fixture.options.enableLogs = true; | ||
| fixture.options.environment = 'test-environment'; | ||
| fixture.options.release = 'test-release'; | ||
|
|
||
| final event = SentryEvent(); | ||
|
|
||
| final scope = Scope(fixture.options); | ||
| final span = MockSpan(); | ||
| scope.span = span; | ||
|
|
||
| final client = fixture.getSut(); | ||
| fixture.options.logBatcher = MockLogBatcher(); | ||
|
|
||
| client.lifeCycleRegistry.registerCallback<OnBeforeSendEvent>((event) { | ||
| event.event.release = '999'; | ||
| }); | ||
|
|
||
| await client.captureEvent(event, scope: scope); | ||
|
|
||
| final capturedEnvelope = (fixture.transport).envelopes.first; | ||
| final capturedEvent = await eventFromEnvelope(capturedEnvelope); | ||
|
|
||
| expect(capturedEvent.release, '999'); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class Fixture { | ||
| final recorder = MockClientReportRecorder(); | ||
| final transport = MockTransport(); | ||
|
|
||
| final options = defaultTestOptions() | ||
| ..platform = MockPlatform.iOS() | ||
| ..groupExceptions = true; | ||
|
|
||
| late SentryTransactionContext _context; | ||
| late SentryTracer tracer; | ||
|
|
||
| SentryLevel? loggedLevel; | ||
| Object? loggedException; | ||
|
|
||
| SentryClient getSut({ | ||
| bool sendDefaultPii = false, | ||
| bool attachStacktrace = true, | ||
| bool attachThreads = false, | ||
| double? sampleRate, | ||
| BeforeSendCallback? beforeSend, | ||
| BeforeSendTransactionCallback? beforeSendTransaction, | ||
| BeforeSendCallback? beforeSendFeedback, | ||
| EventProcessor? eventProcessor, | ||
| bool provideMockRecorder = true, | ||
| bool debug = false, | ||
| Transport? transport, | ||
| }) { | ||
| options.tracesSampleRate = 1.0; | ||
| options.sendDefaultPii = sendDefaultPii; | ||
| options.attachStacktrace = attachStacktrace; | ||
| options.attachThreads = attachThreads; | ||
| options.sampleRate = sampleRate; | ||
| options.beforeSend = beforeSend; | ||
| options.beforeSendTransaction = beforeSendTransaction; | ||
| options.beforeSendFeedback = beforeSendFeedback; | ||
| options.debug = debug; | ||
| options.log = mockLogger; | ||
|
|
||
| if (eventProcessor != null) { | ||
| options.addEventProcessor(eventProcessor); | ||
| } | ||
|
|
||
| // Internally also creates a SentryClient instance | ||
| final hub = Hub(options); | ||
| _context = SentryTransactionContext( | ||
| 'name', | ||
| 'op', | ||
| ); | ||
| tracer = SentryTracer(_context, hub); | ||
|
|
||
| // Reset transport | ||
| options.transport = transport ?? this.transport; | ||
|
|
||
| // Again create SentryClient instance | ||
| final client = SentryClient(options); | ||
|
|
||
| if (provideMockRecorder) { | ||
| options.recorder = recorder; | ||
| } | ||
| return client; | ||
| } | ||
|
|
||
| Future<SentryEvent?> droppingBeforeSend(SentryEvent event, Hint hint) async { | ||
| return null; | ||
| } | ||
|
|
||
| SentryTransaction fakeTransaction() { | ||
| return SentryTransaction( | ||
| tracer, | ||
| sdk: SdkVersion(name: 'sdk1', version: '1.0.0'), | ||
| breadcrumbs: [], | ||
| ); | ||
| } | ||
|
|
||
| SentryEvent fakeFeedbackEvent() { | ||
| return SentryEvent( | ||
| type: 'feedback', | ||
| contexts: Contexts(feedback: fakeFeedback()), | ||
| level: SentryLevel.info, | ||
| ); | ||
| } | ||
|
|
||
| SentryFeedback fakeFeedback() { | ||
| return SentryFeedback( | ||
| message: 'fixture-message', | ||
| contactEmail: 'fixture-contactEmail', | ||
| name: 'fixture-name', | ||
| replayId: 'fixture-replayId', | ||
| url: "https://fixture-url.com", | ||
| associatedEventId: SentryId.fromId('1d49af08b6e2c437f9052b1ecfd83dca'), | ||
| ); | ||
| } | ||
|
|
||
| void mockLogger( | ||
| SentryLevel level, | ||
| String message, { | ||
| String? logger, | ||
| Object? exception, | ||
| StackTrace? stackTrace, | ||
| }) { | ||
| loggedLevel = level; | ||
| loggedException = exception; | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
flutter/lib/src/integrations/generic_app_start_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,74 @@ | ||
| // ignore_for_file: invalid_use_of_internal_member | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| import '../../sentry_flutter.dart'; | ||
| import '../frame_callback_handler.dart'; | ||
|
|
||
| // TODO(buenaflor): marking this internal until we can find a robust way to unify the TTID/TTFD implementation as currently it is very fragmented. | ||
|
|
||
| /// A fallback app–start integration for platforms without built-in app-start timing. | ||
| /// | ||
| /// The Sentry Cocoa and Android SDKs include calls to capture the | ||
| /// exact application start timestamp. Other platforms—such as web, desktop, | ||
| /// or any SDK that doesn’t (yet) expose app-start instrumentation can use this | ||
| /// integration as a reasonable alternative. It measures the duration from | ||
| /// integration call to the first completed frame. | ||
| @internal | ||
| class GenericAppStartIntegration extends Integration<SentryFlutterOptions> { | ||
| GenericAppStartIntegration([FrameCallbackHandler? frameHandler]) | ||
| : _framesHandler = frameHandler ?? DefaultFrameCallbackHandler(); | ||
|
|
||
| final FrameCallbackHandler _framesHandler; | ||
|
|
||
| static const String integrationName = 'GenericAppStart'; | ||
|
|
||
| @override | ||
| void call(Hub hub, SentryFlutterOptions options) { | ||
| if (!options.isTracingEnabled()) return; | ||
|
|
||
| final transactionContext = SentryTransactionContext( | ||
| 'root /', | ||
| SentrySpanOperations.uiLoad, | ||
| origin: SentryTraceOrigins.autoUiTimeToDisplay, | ||
| ); | ||
|
|
||
| final startTimeStamp = options.clock(); | ||
| final transaction = hub.startTransactionWithContext( | ||
| transactionContext, | ||
| startTimestamp: startTimeStamp, | ||
| waitForChildren: true, | ||
| autoFinishAfter: Duration(seconds: 3), | ||
| bindToScope: true, | ||
| trimEnd: true, | ||
| ); | ||
|
|
||
| options.timeToDisplayTracker.transactionId = transactionContext.spanId; | ||
|
|
||
| _framesHandler.addPostFrameCallback((_) async { | ||
| try { | ||
| final endTimestamp = options.clock(); | ||
| await options.timeToDisplayTracker.track( | ||
| transaction, | ||
| ttidEndTimestamp: endTimestamp, | ||
| ); | ||
|
|
||
| // Note: we do not set app start transaction measurements (yet) on purpose | ||
| // This integration is used for TTID/TTFD mainly | ||
| // However this may change in the future. | ||
| } catch (exception, stackTrace) { | ||
| options.log( | ||
| SentryLevel.error, | ||
| 'An exception occurred while executing the $GenericAppStartIntegration', | ||
| exception: exception, | ||
| stackTrace: stackTrace, | ||
| ); | ||
| if (options.automatedTestMode) { | ||
| rethrow; | ||
| } | ||
| } | ||
buenaflor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| options.sdk.addIntegration(integrationName); | ||
| } | ||
| } | ||
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
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.
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.