-
-
Notifications
You must be signed in to change notification settings - Fork 464
fix: improve network body parsing and truncation handling #4958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🤖 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]>
sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt
Outdated
Show resolved
Hide resolved
sentry/src/main/java/io/sentry/util/network/NetworkBodyParser.java
Outdated
Show resolved
Hide resolved
sentry/src/main/java/io/sentry/util/network/NetworkBodyParser.java
Outdated
Show resolved
Hide resolved
sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt
Outdated
Show resolved
Hide resolved
Performance metrics 🚀
|
| 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 |
...ry-android-replay/src/main/java/io/sentry/android/replay/DefaultReplayBreadcrumbConverter.kt
Show resolved
Hide resolved
| 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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😅
romtsn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Co-authored-by: Roman Zavarnitsyn <[email protected]>
| reader.endObject(); | ||
| } catch (Exception e) { | ||
| result.errored = true; | ||
| return map; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: unnecessary return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📜 Description
This PR refactors the
NetworkBodyclass and improves how network request/response bodies are parsed and handled, particularly for large or truncated content.Key changes:
NetworkBodyfrom an interface with multiple implementations to a single class with abodyfield and optionalwarningslistNetworkBodyWarningenum to communicate parsing issues (JSON_TRUNCATED, TEXT_TRUNCATED, INVALID_JSON, BODY_PARSE_ERROR)NetworkBodyParserto properly detect and mark truncated contentJsonObjectReaderto vendorJsonReaderfor more robust JSON parsingSentryOkHttpInterceptorto peek at the correct amount of bytes to detect truncation💡 Motivation and Context
Previously, the network body parsing had issues with:
This change provides a cleaner API and better error/warning reporting.
💚 How did you test it?
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
🤖 Generated with Claude Code