Skip to content

Conversation

@markushi
Copy link
Member

@markushi markushi commented Dec 9, 2025

📜 Description

This PR refactors the NetworkBody class and improves how network request/response bodies are parsed and handled, particularly for large or truncated content.

Key changes:

  • Simplified NetworkBody from an interface with multiple implementations to a single class with a body field and optional warnings list
  • Added NetworkBodyWarning enum to communicate parsing issues (JSON_TRUNCATED, TEXT_TRUNCATED, INVALID_JSON, BODY_PARSE_ERROR)
  • Improved truncation handling in NetworkBodyParser to properly detect and mark truncated content
  • Switched from JsonObjectReader to vendor JsonReader for more robust JSON parsing
  • Updated SentryOkHttpInterceptor to peek at the correct amount of bytes to detect truncation

💡 Motivation and Context

Previously, the network body parsing had issues with:

  1. Inconsistent handling of truncated content
  2. No way to communicate parse warnings to consumers
  3. Less robust JSON parsing

This change provides a cleaner API and better error/warning reporting.

💚 How did you test it?

  • Existing tests should pass
  • Manual testing with large response bodies

📝 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.
  • 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

🤖 Generated with Claude Code

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 292.30 ms 347.96 ms 55.66 ms
Size 1.58 MiB 2.13 MiB 559.07 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
dba088c 321.78 ms 364.59 ms 42.82 ms
fcec2f2 328.91 ms 387.75 ms 58.84 ms
3998a95 415.94 ms 478.54 ms 62.60 ms
f634d01 359.58 ms 433.88 ms 74.30 ms
fc5ccaf 322.49 ms 405.25 ms 82.76 ms
9fbb112 404.51 ms 475.65 ms 71.14 ms
ee747ae 405.43 ms 485.70 ms 80.28 ms
951caf7 323.66 ms 392.82 ms 69.16 ms
ee747ae 358.21 ms 389.41 ms 31.20 ms
f634d01 375.06 ms 420.04 ms 44.98 ms

App size

Revision Plain With Sentry Diff
dba088c 1.58 MiB 2.13 MiB 558.99 KiB
fcec2f2 1.58 MiB 2.12 MiB 551.50 KiB
3998a95 1.58 MiB 2.10 MiB 532.96 KiB
f634d01 1.58 MiB 2.10 MiB 533.40 KiB
fc5ccaf 1.58 MiB 2.13 MiB 557.54 KiB
9fbb112 1.58 MiB 2.11 MiB 539.18 KiB
ee747ae 1.58 MiB 2.10 MiB 530.95 KiB
951caf7 1.58 MiB 2.13 MiB 558.77 KiB
ee747ae 1.58 MiB 2.10 MiB 530.95 KiB
f634d01 1.58 MiB 2.10 MiB 533.40 KiB

Previous results on branch: fix/network-request-response-body-parsing

Startup times

Revision Plain With Sentry Diff
8287b45 308.53 ms 359.32 ms 50.79 ms

App size

Revision Plain With Sentry Diff
8287b45 1.58 MiB 2.13 MiB 559.07 KiB

@romtsn romtsn requested a review from 43jay December 9, 2025 14:28
@markushi markushi requested a review from 43jay December 11, 2025 08:17
private final @NotNull String value;
// Based on
// https://github.com/getsentry/sentry/blob/ccb61aa9b0f33e1333830093a5ce3bd5db88ef33/static/app/utils/replays/replay.tsx#L5-L12
public enum NetworkBodyWarning {
Copy link
Member

Choose a reason for hiding this comment

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

so we can't exactly match all of the reasons on Android?

Copy link
Member Author

Choose a reason for hiding this comment

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

Some of them simply don't make sense on mobile, like BODY_PARSE_TIMEOUT. So I only added those which we actually use

}
return new NetworkBody(data, warnings);
}
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

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

so we still wanna catch Exception and not Throwable?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've read up on this and it seems like it's not safe it simply catch a StackOverflow as the JVM could be in an inconsistent state. I've added a max_depth const for parsing instead and combined with the truncation I think we have enough safe guards in place now.

Copy link
Member

Choose a reason for hiding this comment

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

wondering if we this is already a problem elsewhere where we catch Throwables 😅

Copy link
Member

@romtsn romtsn left a comment

Choose a reason for hiding this comment

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

LGTM!

reader.endObject();
} catch (Exception e) {
result.errored = true;
return map;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: unnecessary return

Copy link
Collaborator

@43jay 43jay left a comment

Choose a reason for hiding this comment

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

looks great!, and confirmed that

  1. works as expected for SecondActivity on my device (when string value is truncated/malformed SR)
  2. WAE when key name is truncated/malformed (malformed keys) SR

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.

5 participants