Skip to content

feat: Connect WebView traces with app traces - #9474

Open
antonis wants to merge 8 commits into
ref/outgoing-tracefrom
feat/webview-trace-propagation
Open

antonis wants to merge 8 commits into
ref/outgoing-tracefrom
feat/webview-trace-propagation

Conversation

@antonis

@antonis antonis commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

Stacked on #9487, which extracts the outgoing trace lookup from SentryNetworkTracker. #9488 adds the SentryObjC API on top.

Adds SentryWebViewTracing.addTracePropagation(to:options:), which connects the app's trace with web content in a WKWebView.

SentryWebViewTracing.addTracePropagation(
    to: webView,
    options: SentryWebViewTracingOptions(allowedHosts: ["app.example.com"])
)
webView.load(URLRequest(url: URL(string: "https://app.example.com")!))

Before any page script runs, the current trace is written into pages from the allowed hosts as sentry-trace and baggage meta tags, using a WKUserScript at document start. A @sentry/browser running in the page then continues the trace on pageload.

  • Accepts a WKWebView, WKWebViewConfiguration, WKUserContentController, or SwiftUI WebPage.Configuration (iOS 26+). The SentryObjC API follows in feat: Add SentryObjC WebView trace propagation #9488.
  • allowedHosts is a trust boundary, separate from tracePropagationTargets. A page matches an entry's host or its subdomains.
  • Only the main frame receives the trace, and sentry-transaction is removed from baggage.
  • Calling it again replaces Sentry's script with a fresh snapshot of the trace.
  • Each call records a WebViewTracePropagation integration for feature adoption.
  • The script and host normalization match sentry-react-native#6794 and sentry-java#6395. Unlike Android, any scheme is allowed, so pages served through custom scheme handlers (for example Capacitor's capacitor://localhost) work; App Transport Security already blocks plain http by default.

⚠️ Note for reviewing: I understand that this is a big PR since it also includes tests and the sample update I used for verification. I'll be happy to split those in a separate PR if it makes more sense 🙏

💡 Motivation and Context

💚 How did you test it?

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed. (docs: Update Apple WebView guidance for connected traces #9136)
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

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.
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 973db93

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@antonis antonis added the run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request label Oct 28, 2026
@sentry

sentry Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Size Analysis

1 component analyzed

iOS Builds

Name Configuration Version Download Size Install Size
SDK-Size (iOS)
io.sentry.sample.SDK-Size
Release 9.32.0 (1) 1.2 MB (-580 B) 3.2 MB (0 B)

Configure sentry-cocoa status check rules

@sentry

sentry Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

📲 Install Builds

iOS

🔗 App Name App ID Version Configuration
SDK-Size io.sentry.sample.SDK-Size 9.32.0 (1) Release

⚙️ sentry-cocoa Build Distribution Settings

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.
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🚨 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:

  • Sources/Swift/Networking/SentryNetworkTracker.swift

@antonis antonis mentioned this pull request Oct 29, 2026
4 tasks done
@antonis
antonis changed the base branch from main to ref/outgoing-trace October 29, 2026 10:03
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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@antonis
antonis marked this pull request as ready for review October 29, 2026 12:33
@antonis
antonis requested review from a team and noahsmartin as code owners October 29, 2026 12:33
@antonis
antonis requested review from 0xadam-brown, itaybre, philprime and runningcode and removed request for a team October 29, 2026 12:33
@antonis
antonis requested review from alwx and buenaflor and removed request for a team October 29, 2026 12:33

@itaybre itaybre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost LGTM, one small question


/// For testing purposes, we want to be able to change the DSN and store it to disk. In a real app, you shouldn't need this behavior.
var dsn: String? {
public var dsn: String? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: any reason to make this public?

Comment on lines +66 to +70
SentryWebViewTracePropagation.recordUsage(in: SentrySDK.isEnabled ? SentrySDKInternal.currentHub() : nil)
return SentryWebViewTracePropagation.install(
allowedHosts: options.allowedHosts,
on: userContentController,
hub: SentryWebViewTracePropagation.currentHub

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Both SentrySDKInternal.currentHub() and SentryWebViewTracePropagation.currentHub should be the same Hub, right?
Would it be possible for the hub be passed as an argument?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved run-full-ci Allows gated GitHub Action workflows to run for a labelled pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Track WebView trace-connection feature adoption Add support for WebViews

2 participants