Skip to content

feat(android): Connect traces with web content in a WebView - #6395

Open
antonis wants to merge 6 commits into
mainfrom
feat/webview-trace-propagation
Open

antonis wants to merge 6 commits into
mainfrom
feat/webview-trace-propagation

Conversation

@antonis

@antonis antonis commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

📜 Description

Adds SentryWebViewTracing.addTracePropagation, which connects the app's trace with web content in an android.webkit.WebView.

SentryWebViewTracing.addTracePropagation(webView, "app.example.com")
webView.loadUrl("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 WebViewCompat.addDocumentStartJavaScript. A @sentry/browser running in the page then continues the trace on pageload.

  • The app must add androidx.webkit:webkit; it is compileOnly in sentry-android-core. Without it, on a WebView without document-start scripts, or when called off the WebView's thread, the call logs a warning and returns false.
  • allowedHosts is a trust boundary, separate from tracePropagationTargets. A page matches an entry's host or its subdomains, over https. A non-default port in an entry is kept.
  • 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.
  • Also adds a sample screen and an instrumented test.

⚠️ Note for reviewing: I understand that this is a big PR since it also includes tests and the sample update. I'll be happy to break this down in 2-3 chained PRs 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 Android WebView guidance for connected traces #6161
  • 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.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

antonis and others added 2 commits October 27, 2026 17:01
Add SentryWebViewTracing.addTracePropagation, which writes the current
trace into pages from allowed hosts as sentry-trace and baggage meta
tags before any page script runs, so @sentry/browser in the page
continues the trace on pageload.

It uses androidx.webkit's document-start script, a compileOnly
dependency checked at runtime, and adds a sample screen and an
instrumented test.

Refs JAVA-716

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 27, 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 bbf1517

@sentry

sentry Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.62.0 (1) release

⚙️ sentry-android Build Distribution Settings

@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 and others added 2 commits October 28, 2026 09:12
A WebView origin rule without a port only matches port 443, so a
non-default port in an allowedHosts entry is now kept in the rule.

androidx.webkit is compileOnly, so an app may ship an older version
than the SDK was compiled against. A LinkageError is now caught, like
for androidx.activity in the feedback form, and the call returns false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…call

A call off the WebView's thread returns false without touching the
WebView, which left Sentry's previous script installed. The removal is
now posted to the WebView's thread, and skipped if a call on that
thread replaced the script in the meantime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

The off-thread cleanup compared against one counter shared by all
WebViews, bumped before the replacement finished. A concurrent
off-thread call could then remove a script that was just installed,
and a call on another WebView cancelled this WebView's cleanup. The
count is now kept per WebView, with weak keys, and bumped once the
replacement is done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 8db8222. Configure here.

The sample no longer shows an iframe; the instrumented test in
sentry-uitest-android still checks that iframes don't get the trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@antonis
antonis marked this pull request as ready for review October 28, 2026 10:33
@antonis
antonis requested review from a team and adinauer as code owners October 28, 2026 10:33

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Track WebView trace-connection feature adoption Add support for WebViews

1 participant