feat(span-first): Add startSpan and startSpanSync#3574
feat(span-first): Add startSpan and startSpanSync#3574buenaflor merged 18 commits intofeat/span-firstfrom
startSpan and startSpanSync#3574Conversation
…nd include startTimestamp parameter Modified the startSpan method across Hub, NoOpHub, and Sentry classes to return type T instead of FutureOr<T>, enhancing type safety. Added an optional startTimestamp parameter to support backdated spans. Updated example usage in the Flutter demo to demonstrate synchronous span creation.
Added a new startSpanSync method to the Hub, NoOpHub, and Sentry classes, allowing for synchronous span creation with a callback. Updated the startSpan method to return Future<T> for asynchronous operations. Enhanced documentation to clarify usage and added tests to ensure correct behavior of the new synchronous method.
Add tests covering the NoOpSentrySpanV2 code path in both startSpan and startSpanSync for two scenarios: hub is closed and traceLifecycle is static. These paths skip zone forking and call the callback directly, which was previously untested. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🚨 Detected changes in high risk code 🚨High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:
|
startSpan and startSpanSync
|
The Hub.startSpan return type changed from FutureOr<T> to Future<T>, causing MockHub.startSpan to be out of sync. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…n creation Replaced the call to Sentry.startSpan with Sentry.startSpanSync in the Flutter example to demonstrate the synchronous span creation functionality. This aligns with recent changes to the span API and enhances the example's clarity.
Removed the _endActiveSpan method and integrated its functionality directly into the error handling of startSpan. Now, spans are ended and their status is set to error in the catch block, improving code clarity and reducing redundancy.
Removed unnecessary variable assignments in the runZoned calls within the Hub class, enhancing code readability and reducing redundancy. The changes simplify the asynchronous handling of spans while maintaining functionality.
There was a problem hiding this comment.
Pull request overview
This PR splits the Span V2 callback API into two explicit variants—startSpan for async work and startSpanSync for synchronous work—removing the previous FutureOr<T> ambiguity and updating call sites, mocks, and tests accordingly.
Changes:
- Change
startSpanto require an async callback (Future<T>) and returnFuture<T>. - Add
startSpanSyncfor synchronous callbacks returningT. - Propagate
startTimestampthrough the new APIs and update tests/examples/mocks.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/dart/lib/src/hub.dart | Implements async startSpan and new startSpanSync, sharing zone-based active-span scoping. |
| packages/dart/lib/src/sentry.dart | Updates public Sentry API to Future<T> startSpan + adds startSpanSync, plus doc tweaks. |
| packages/dart/lib/src/noop_hub.dart | Updates NoOpHub to match the new Hub interface (async + sync variants, startTimestamp). |
| packages/dart/lib/src/hub_adapter.dart | Forwards new startSpan/startSpanSync signatures and startTimestamp to Sentry.currentHub. |
| packages/dart/test/hub_span_test.dart | Updates existing tests and adds coverage for the new sync/async split and startTimestamp. |
| packages/flutter/test/user_interaction/sentry_user_interaction_widget_test.dart | Migrates a test call site from async startSpan to startSpanSync. |
| packages/flutter/test/mocks.mocks.dart | Regenerates Mockito mocks to reflect the new Hub API (startSpanSync, startTimestamp, async-only startSpan). |
| packages/flutter/example/lib/main.dart | Demonstrates new Sentry.startSpanSync usage in the example app. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/flutter/test/user_interaction/sentry_user_interaction_widget_test.dart
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…feat/span/change-futureor
Removed outdated comments regarding active spans in zone-forked scopes.
Updated comments in the Hub class to clarify the behavior of the _zoneScope and getActiveSpan methods. The changes improve the understanding of how zone-forked scopes and active spans are managed, including the resolution order for active spans.
Updated the startSpan method to be asynchronous, improving the handling of span creation and execution within the Hub class. This change enhances the overall flow of asynchronous operations while maintaining existing functionality.
…anSync Updated the documentation for startSpan and startSpanSync methods to clarify their behavior, including nesting capabilities and handling of NoOpSentrySpanV2. Added comprehensive tests to verify the correct parent-child relationships between synchronous and asynchronous spans, ensuring accurate span management in various scenarios.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
denrase
left a comment
There was a problem hiding this comment.
Just one comment for the non-wrapped comments.
…an method Updated the startSpan method in the NoOpHub class to ensure the startTimestamp parameter is optional, enhancing flexibility in span creation. This change aligns with the asynchronous nature of span handling introduced in previous commits.
Updated the startSpanSync method in the NoOpHub class to remove the async keyword, ensuring it operates synchronously. This change aligns with the method's intended functionality and improves clarity in its usage.

📜 Description
Updates from a single
FutureOr<T> startSpanto two distinct ones.💡 Motivation and Context
FutureOrgets dropped because it pushes ambiguity onto every caller.It's ergonomically also not well to work with and we want to maximize dev experience as much as possible
💚 How did you test it?
hub_span_test📝 Checklist
sendDefaultPiiis enabled🔮 Next steps
#skip-changelog