Skip to content

feat(network-details): New swizzling to capture response bodies for session replay#7584

Merged
43jay merged 6 commits intomainfrom
mobile-935/new-swizzling
Apr 28, 2026
Merged

feat(network-details): New swizzling to capture response bodies for session replay#7584
43jay merged 6 commits intomainfrom
mobile-935/new-swizzling

Conversation

@43jay
Copy link
Copy Markdown
Collaborator

@43jay 43jay commented Mar 3, 2026

📜 Description

  • Newly swizzled methods:
    [NSURLSession dataTaskWithURL:completionHandler:]
    [NSURLSession dataTaskWithRequest:completionHandler:]

  • This swizzling is skipped when network detail capture is not enabled (SentryReplayOptions#networkDetailAllowUrls is empty)

  • Added testing to validate for future iOS SDKs that the IMPs we are swizzling are implemented directly on NSURLSession: SentryNSURLSessionTaskSearchTests

    • test_URLSessionDataTaskWithRequest_ByIosVersion
    • test_URLSessionDataTaskWithURL_ByIosVersion

💡 Motivation and Context

With this change sentry-cocoa can inspect response bodies of NSURLSession dataTask's that use completionHandlers.

Swizzling is added to inspect the NSURLResponse before delegating to the original completionHandler.
The request body will be captured via existing swizzling into SentryNetworkTracker (setState, resume).

See swizzling discussion on #7582 for more context.

💚 How did you test it?

Unit tests

xcodebuild test -workspace Sentry.xcworkspace -scheme Sentry -destination 'platform=iOS Simulator,name=iPhone 16 Pro' -only-testing:Tests/SentryTests/Integrations/Performance/Network/SentryNSURLSessionTaskSearchTests.swift | xcbeautify

test_URLSessionTask_ByIosVersion ✅
test_URLSessionDataTaskWithRequest_ByIosVersion ✅
test_URLSessionDataTaskWithURL_ByIosVersion ✅

SKIP_SWIFTLINT=1 xcodebuild test -workspace Sentry.xcworkspace -scheme Sentry -destination 'platform=iOS Simulator,name=iPhone 16 Pro' -only-testing:SentryTests/SentrySwizzleWrapperHelperTests

make test-ios ONLY_TESTING=SentryNetworkDetailSwizzlingTests 2>&1 | xcbeautify
...
Test Suite 'SentryNetworkDetailSwizzlingTests' started at 2026-04-13 13:55:14.423.
Executed 2 tests, with 0 failures (0 unexpected) in 2.100 (2.100) seconds

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled. gated by networkDetailAllowUrls
  • I updated the docs if needed. future pr
  • I updated the wizard if needed. N/A
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog. #skip-changelog future PR
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs. 🤔

@linear
Copy link
Copy Markdown

linear Bot commented Mar 3, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


This PR will not appear in the changelog.


🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

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

Generated by 🚫 dangerJS against dc934e5

@43jay 43jay marked this pull request as ready for review March 3, 2026 20:10
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 1.96078% with 50 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.172%. Comparing base (1c5ecda) to head (dc934e5).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
Sources/Sentry/SentrySwizzleWrapperHelper.m 0.000% 49 Missing ⚠️
...nce/Network/SentryNetworkTrackingIntegration.swift 50.000% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #7584       +/-   ##
=============================================
- Coverage   85.330%   85.172%   -0.158%     
=============================================
  Files          490       490               
  Lines        29448     29500       +52     
  Branches     12738     12755       +17     
=============================================
- Hits         25128     25126        -2     
- Misses        4269      4322       +53     
- Partials        51        52        +1     
Files with missing lines Coverage Δ
Sources/Sentry/SentryNetworkTracker.m 99.651% <ø> (ø)
...nce/Network/SentryNetworkTrackingIntegration.swift 97.142% <50.000%> (-2.858%) ⬇️
Sources/Sentry/SentrySwizzleWrapperHelper.m 52.427% <0.000%> (-47.573%) ⬇️

... and 7 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c5ecda...dc934e5. Read the comment docs.

Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
Comment thread Sources/Sentry/SentryNSURLSessionSwizzleTargetDiscovery.m Outdated
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
Comment thread Sources/Sentry/SentryNSURLSessionSwizzleTargetDiscovery.m Outdated
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
Copy link
Copy Markdown
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this. I have one major high-level question before reviewing this more closely.

Comment thread Sources/Sentry/SentryNSURLSessionSwizzleTargetDiscovery.m Outdated
Comment thread Sources/Sentry/SentryNSURLSessionSwizzleTargetDiscovery.m Outdated
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
@43jay 43jay force-pushed the mobile-935/data-classes branch from 71fd40b to 78d1d60 Compare March 4, 2026 21:03
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 9473efb to 5bc5830 Compare March 4, 2026 21:03
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m
@43jay 43jay force-pushed the mobile-935/data-classes branch from 78d1d60 to 9a4f8b3 Compare March 4, 2026 22:00
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 5bc5830 to 7faf4ef Compare March 4, 2026 22:00
@43jay 43jay force-pushed the mobile-935/data-classes branch from 9a4f8b3 to c62d15c Compare March 6, 2026 16:33
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 7faf4ef to 6e19c0a Compare March 6, 2026 16:33
@43jay 43jay force-pushed the mobile-935/data-classes branch from c62d15c to e717c1a Compare March 6, 2026 19:25
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 6e19c0a to 9a8d48d Compare March 6, 2026 19:25
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
Comment thread Sentry.xcodeproj/project.pbxproj Outdated
Comment thread Sources/Sentry/SentrySwizzleWrapperHelper.m Outdated
@43jay 43jay marked this pull request as draft March 9, 2026 15:12
@43jay 43jay force-pushed the mobile-935/new-swizzling branch 2 times, most recently from 8584657 to c3afd6d Compare March 9, 2026 19:02
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from bc47f47 to 7167c11 Compare April 27, 2026 19:24
@43jay 43jay force-pushed the mobile-935/data-classes branch 2 times, most recently from 4e16c52 to ef11583 Compare April 27, 2026 19:43
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 7167c11 to 72aa000 Compare April 27, 2026 19:43
@43jay 43jay force-pushed the mobile-935/data-classes branch from ef11583 to 4a96fe9 Compare April 27, 2026 20:16
@43jay 43jay force-pushed the mobile-935/new-swizzling branch from 72aa000 to 040e097 Compare April 27, 2026 20:16
Base automatically changed from mobile-935/data-classes to main April 27, 2026 20:37
43jay added 3 commits April 28, 2026 16:10
…onse capture

Add (no-op) callback into SentryNetworkTracker

Remove run-time discovery for swizzle targets; directly swizzle
  [NSURLSession class]
Add unit test to do run-time discovery and report if assumptions invalid
#7584 (comment)
test_dataTaskWithURL_doesNotCallThrough_dataTaskWithRequest
test_dataTaskWithRequest_doesNotCallThrough_dataTaskWithURL
were asserting that one impl does not rely on the other internally.
There's no reason to believe that they do - the test was probably checking
for something that would not happen.
Additionally it was relying on method_setImplementation which could cause
random failures in CI if not cleaned up properly => remove
@43jay 43jay force-pushed the mobile-935/new-swizzling branch 2 times, most recently from 06583c1 to 22e2b20 Compare April 28, 2026 20:16
@43jay
Copy link
Copy Markdown
Collaborator Author

43jay commented Apr 28, 2026

moved failing test to PR with implementation (#7590)

@43jay 43jay enabled auto-merge (squash) April 28, 2026 20:19
@itaybre itaybre added the ready-to-merge Use this label to trigger all PR workflows label Apr 28, 2026
@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 28, 2026

📲 Install Builds

iOS

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

⚙️ sentry-cocoa Build Distribution Settings

DebugWithoutUIKit build was failing, now it passes \0/
@43jay
Copy link
Copy Markdown
Collaborator Author

43jay commented Apr 28, 2026

added missing && ! SENTRY_NO_UI_FRAMEWORK in SentryNetworkTrackingIntegration.swift

@43jay 43jay disabled auto-merge April 28, 2026 21:05
@43jay 43jay enabled auto-merge (squash) April 28, 2026 21:06
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1222.10 ms 1250.00 ms 27.90 ms
Size 24.14 KiB 1.15 MiB 1.13 MiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
df67624 1225.12 ms 1259.90 ms 34.78 ms
787537a 1218.35 ms 1251.72 ms 33.38 ms
b6fa517 1218.83 ms 1257.47 ms 38.63 ms
ffb6adc 1218.60 ms 1247.47 ms 28.87 ms
1c5ecda 1219.35 ms 1253.76 ms 34.41 ms

App size

Revision Plain With Sentry Diff
df67624 24.14 KiB 1.14 MiB 1.12 MiB
787537a 24.14 KiB 1.15 MiB 1.12 MiB
b6fa517 24.14 KiB 1.14 MiB 1.12 MiB
ffb6adc 24.14 KiB 1.15 MiB 1.12 MiB
1c5ecda 24.14 KiB 1.15 MiB 1.12 MiB

Previous results on branch: mobile-935/new-swizzling

Startup times

Revision Plain With Sentry Diff
05663d8 1232.92 ms 1259.23 ms 26.31 ms
8fab221 1226.27 ms 1257.69 ms 31.43 ms

App size

Revision Plain With Sentry Diff
05663d8 24.14 KiB 1.15 MiB 1.13 MiB
8fab221 24.14 KiB 1.15 MiB 1.13 MiB

@43jay
Copy link
Copy Markdown
Collaborator Author

43jay commented Apr 28, 2026

missed make format on a file

@43jay
Copy link
Copy Markdown
Collaborator Author

43jay commented Apr 28, 2026

missed a make generate api - that job runs off the HEAD branch mobile-935/ios-network details

Run make generate-public-api
@github-actions
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/Sentry/SentryNetworkTracker.m

@43jay 43jay merged commit a7c42d9 into main Apr 28, 2026
203 of 207 checks passed
@43jay 43jay deleted the mobile-935/new-swizzling branch April 28, 2026 23:19
@43jay 43jay changed the title feat(network-details): Introduce new swizzling to capture response bodies for session replay feat(network-details): New swizzling to capture response bodies for session replay Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Use this label to trigger all PR workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants