Reduce excessive CPU usage when serializing breadcrumbs to disk#4181
Merged
Reduce excessive CPU usage when serializing breadcrumbs to disk#4181
Conversation
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 473dcc6 | 406.02 ms | 455.16 ms | 49.14 ms |
| d877760 | 672.94 ms | 788.46 ms | 115.52 ms |
| ca480b4 | 389.82 ms | 456.52 ms | 66.70 ms |
| 247dd70 | 396.72 ms | 419.14 ms | 22.42 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 473dcc6 | 1.58 MiB | 2.21 MiB | 645.19 KiB |
| d877760 | 1.58 MiB | 2.21 MiB | 646.64 KiB |
| ca480b4 | 1.58 MiB | 2.21 MiB | 645.20 KiB |
| 247dd70 | 1.58 MiB | 2.21 MiB | 646.61 KiB |
stefanosiano
approved these changes
Feb 27, 2025
Contributor
stefanosiano
left a comment
There was a problem hiding this comment.
Just a question regarding setBreadcrumbs(), but it's good
markushi
approved these changes
Mar 14, 2025
Member
markushi
left a comment
There was a problem hiding this comment.
Looks good to me! Do I assume right that if someone upgrades the SDK, and the devices has pending ANRs, they won't be enriched, as the scope data is only available in the old format?
| } | ||
| if (event.getBreadcrumbs() == null) { | ||
| event.setBreadcrumbs(new ArrayList<>(breadcrumbs)); | ||
| event.setBreadcrumbs(breadcrumbs); |
Member
Author
Yes that's right (only breadcrumbs would be missing though)! But I guess it's fine to accept that trade-off (and it's probably not going to be a very common scenario to have an ANR in-between app updates). Left a comment also: try {
queueFile = new QueueFile.Builder(file).size(options.getMaxBreadcrumbs()).build();
} catch (IOException e) {
// if file is corrupted we simply delete it and try to create it again. We accept
// the trade
// off of losing breadcrumbs for ANRs that happened right before the app has
// received an
// update where the new format was introduced
file.delete();
queueFile = new QueueFile.Builder(file).size(options.getMaxBreadcrumbs()).build();
} |
romtsn
added a commit
that referenced
this pull request
Mar 14, 2025
* WIP * WIP * Remove redundant line * Add Tests * api dump * Formatting * REset scope cache on new init * Clean up * Comment * Changelog * Workaround square/tape#173 * Add a comment to setBreadcrumbs * Address PR review * Update CHANGELOG.md
romtsn
added a commit
that referenced
this pull request
Mar 17, 2025
… (#4260) * WIP * WIP * Remove redundant line * Add Tests * api dump * Formatting * REset scope cache on new init * Clean up * Comment * Changelog * Workaround square/tape#173 * Add a comment to setBreadcrumbs * Address PR review * Update CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📜 Description
QueueFile(vendored from https://github.com/square/tape) which allows us to write breadcrumbs atomically one-by-one using RandomAccessFile under the hoodmaxElementsand works as a ring buffer based on the number of elements in the queueresetCachemethod that cleans up the disk cache on every init (but after the ANR processing is done) to ensure clean state and that we don't enrich with outdated scope valuesBefore
After
Also posting a perfetto trace query where we have ~50
addBreadcrumbcalls to confirm that time spent inaddBreadcrumbdoes not increase with the number of breadcrumbs added:💡 Motivation and Context
Closes #3168
💚 How did you test it?
manually + automated
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
Another potential improvement could be to batch breadcrumbs in-memory first, before writing to disk, and then flush every 1-2 seconds as batches to reduce I/O even further. The trade-off would be that we can lose some important breadcrumbs if an error occurs right within that timeframe. For now I would still like to keep it as-is, but if we receive further reports about excessive I/O we can implement that improvement too. I'm leaving the code snippet here so we can come back to it later and just copy-paste it:
Code snippet for batched writes
as a result we just had 2 I/O writes:
