Repository navigation
feat: Connect WebView traces with app traces - #9474
Conversation
Add SentryWebViewTracing.addTracePropagation(to:options:), which injects the current trace as sentry-trace and baggage meta tags into pages on allowed hosts before page scripts run, so @sentry/browser continues the trace on pageload. It accepts a WKUserContentController, WKWebViewConfiguration, WKWebView, or SwiftUI WebPage.Configuration (iOS 26+). SentryObjC exposes it as SentryObjCWebViewTracing. The injected script and host normalization match sentry-react-native and sentry-java. Using the API records the WebViewTracePropagation integration for adoption. Extract the outgoing trace lookup from the network tracker into SentryOutgoingTrace so both paths share it.
|
Size Analysis1 component analyzed iOS Builds
|
📲 Install BuildsiOS
|
Run @sentry/browser in the iOS-Swift WebView screen so its pageload continues the app's trace, and add a SwiftUI WebPage screen on iOS 26. UI tests check that both pages see the injected trace before their own scripts run. Add a test that loads a page in a real WebPage created from a configuration with Sentry's script.
🚨 Detected changes in high risk code 🚨High-risk code can easily blow up and is hard to test. We had severe bugs in the past. Be extra careful when changing these files, and have an extra careful look at these:
|
Move the lookup of the trace propagated to outgoing requests from SentryNetworkTracker into SentryOutgoingTrace: the span bound to the scope if it belongs to a tracer, otherwise the scope's propagation context. Behavior is unchanged. This lets WebView trace propagation reuse the same lookup.
Move SentryObjCWebViewTracing, its tests, and the SentryObjC API snapshots to a separate pull request stacked on this one, to keep this one focused on the Swift API.
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 973db93. Configure here.
itaybre
left a comment
There was a problem hiding this comment.
Almost LGTM, one small question
Resolve the current hub once in addTracePropagation and pass it to both the adoption marker and the script install, and replace the test Hub double with real hubs. Keep SentrySDKWrapper.dsn internal: the sample pages read the DSN from SentrySDK.startOption instead. Merge the changelog entry into the existing Features section of Unreleased.
The SwiftPM package tests run on the iOS Simulator without a host app. There, WebKit's web content process intermittently becomes unresponsive, so the two WebView page-load tests time out. The Xcode unit test plans and the iOS-Swift UI tests keep covering real page loads.
|
Thank you for the feedback @itaybre 🙇 I've updated the PR. If there are no blockers I'll proceed with merging. Note that I had to disable a couple of tests 2e5bbee since they started failing consistently without obvious reason. I've tested moving them to Bitrise in #9525 and they are 🟢 again but I think this is a bigger SDK/CI decision. |
<!-- Keep the urgency section so automation can prioritize this PR. You may delete other sections you don't need. --> ## DESCRIBE YOUR PR Updates the Apple WebView page for the new trace connection in the Cocoa SDK. It replaces the statement that the SDK doesn't interact with web views with setup for `SentryWebViewTracing.addTracePropagation`, which writes the app's trace into `WKWebView` pages as `sentry-trace` and `baggage` meta tags before page scripts run, so `@sentry/browser` continues it on pageload. - Swift and SentryObjC examples, plus a SwiftUI `WebPage` section (iOS, macOS, visionOS 26+) - `allowedHosts` matching and the trust warning, aligned with the [React Native WebView page](https://docs.sentry.io/platforms/react-native/configuration/webview/) - The snapshot behavior, supported platforms, and limitations Resolves getsentry/sentry-cocoa#9136 >⚠️ SDK changes: getsentry/sentry-cocoa#9474 (Swift API) and getsentry/sentry-cocoa#9488 (SentryObjC API). The version alert says `9.33.0`; it will be updated if the release differs. Keep this as a draft until those PRs ship. ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the due date. You can update this information later by editing the PR description. - [ ] Urgent deadline (GA date, etc.): YYYY-MM-DD - [ ] Other deadline: YYYY-MM-DD - [x] No deadline: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've supplied a deadline. Thanks in advance for your help! ## PRE-MERGE CHECKLIST _Make sure you've checked the following before merging your changes:_ - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/)
📜 Description
Adds
SentryWebViewTracing.addTracePropagation(to:options:), which connects the app's trace with web content in aWKWebView.Before any page script runs, the current trace is written into pages from the allowed hosts as
sentry-traceandbaggagemeta tags, using aWKUserScriptat document start. A@sentry/browserrunning in the page then continues the trace on pageload.WKWebView,WKWebViewConfiguration,WKUserContentController, or SwiftUIWebPage.Configuration(iOS 26+). The SentryObjC API follows in feat: Add SentryObjC WebView trace propagation #9488.allowedHostsis a trust boundary, separate fromtracePropagationTargets. A page matches an entry's host or its subdomains.sentry-transactionis removed frombaggage.WebViewTracePropagationintegration for feature adoption.capacitor://localhost) work; App Transport Security already blocks plain http by default.💡 Motivation and Context
💚 How did you test it?
WKWebViewand SwiftUIWebPage@sentry/browseron the iOS 26.5 simulatorWKWebView:WebView:📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps