Repository navigation
perf(attachments): Stream file attachments when the envelope is written - #6777
Draft
sentry-junior[bot] wants to merge 4 commits into
Draft
sentry-junior[bot] wants to merge 4 commits into
sentry-junior[bot] wants to merge 4 commits into
Conversation
An attachment with only a pathname used to be read into a byte[] on the first envelope serialization and kept for the envelope's lifetime, and its header length read the whole file. Now the item holds only the file, its header length is the file size, and JsonSerializer copies the file straight into the output stream each time the envelope is written.
Contributor
PR risk: mediumFor commit Main concerns: reach, public contract, data sent to Sentry. Rules that block low also fired:
This label is a hint for reviewers, not an approval. |
📲 Install BuildsAndroid
|
…chments # Conflicts: # CHANGELOG.md
Replace zero padding with an abort: if a file attachment shrinks, grows, or can't be read while it's streamed, serialization throws SentryEnvelopeAbortedException. The cache deletes the partial file, the HTTP connection disconnects before the buffered body is sent, and the transport drops the envelope without retrying. Record dropped items and aborted envelopes in client reports with the new `invalid` discard reason, once per item across serializations. Serialize the envelope for Spotlight on the calling thread, so it finishes before the transport serializes the same envelope. Only the HTTP post runs on Spotlight's executor. Co-Authored-By: Markus Hintersteiner <markus.hintersteiner@sentry.io>
Contributor
🚨 Detected changes in high risk code 🚨High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:
|
This branch has not been deployed
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.
Follow-up to #6743. That PR is the narrow fix for Perfetto chunks. This one starts the broader change: file-backed envelope item payloads should be read only when the envelope is written, and streamed rather than built as a
byte[]first.What actually happens today
File-backed items are already read lazily. Every file factory (
fromAttachmentwith a pathname,fromProfilingTrace,fromProfileChunk,fromPerfettoProfileChunk,fromReplay) wraps its read in aCachedItem. Nothing is read when the item is built. The firstgetData()happens inJsonSerializer.serialize(envelope, out). That is normallyenvelopeCache.storeEnvelopeinAsyncHttpTransport.EnvelopeSender.flush(), on the transport thread.The real costs are these:
byte[]lives inSentryEnvelopeItem.data/CachedItem.bytesfor the whole life of the envelope: the cache write, then the HTTP send, until the envelope is collected.readBytesFromFilegrows aByteArrayOutputStreamand then copies it withtoByteArray(). Profiles add a base64Stringand the serialized JSON on top. Replay adds msgpack copies.cachedItem.getBytes().length, soheader.getLength()loads the whole file.This PR: stream pathname attachments
The first slice covers an
Attachmentthat has only a pathname. That is the simplest file item: raw bytes, no transformation.fromAttachmentreturns a file-backed item that holds no bytes. Its header length is the file size, checked againstmaxAttachmentSize. Getting the length no longer reads the file.JsonSerializerasks the item for aPayloadthrough the new package-privateopenPayload(). For a file,openPayload()checks the file and fixes its length before anything is written, so a missing, unreadable, or too large file still drops only that item, with the same log as today. The header is then written with that length, and the file is copied to the output stream in 8 KB chunks.lengthbytes, and writing fewer would make every later item in the envelope unreadable, so this keeps the event.getData()keeps working for callers such asbeforeEnvelopecallbacks. For file items it reads the file on each call and no longer caches the result.FileUtils.ensureReadableFile, whichreadBytesFromFilealso uses.openPayload,Payload, and the new headerserializeoverload are package-private. The only.apichange isFileUtils.ensureReadableFile, andFileUtilsis already@ApiStatus.Internal.Side effects of reading at send time
RejectedExecutionHandlerstores the envelope on the thread that captured it, and that store now streams the file on that thread.EnvelopeReader, outbox, offline cache) are still byte-backed and unaffected.Follow-up slices
[metadata][trace]. This needs the trace file deleted after the envelope is done, not after the first read, because each serialization now reads the file.String. This needs a streaming JSON value for the sampled profile.CachedItemchange Roman suggested.EnvelopeReader: read cached and outbox envelopes as a stream instead of loading the whole file and copying each item withcopyOfRange.Open questions
SpotlightIntegrationserializes the envelope on its own executor whileAsyncHttpTransportserializes it on its thread.CachedItemisn't thread-safe, and the profile and replay factories delete their file after the first read. Two concurrent first reads can race: one may delete the file while the other is reading it. Streamed attachments avoid this because each serialization opens its own stream. The other slices need a decision here: lock theCachedItemresolution, or make SDK-owned files outlive the envelope and delete them when the envelope is done.via markus.hintersteiner.
--
View Session [Sentry]