Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- (snapshots) Compress preprod snapshot manifest with zstd ([#3336](https://github.com/getsentry/sentry-cli/pull/3336))

## 3.5.1

### Internal Changes 🔧
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ walkdir = "2.3.2"
which = "4.4.0"
whoami = "1.5.2"
zip = "2.4.2"
zstd = "0.13.3"
data-encoding = "2.3.3"
magic_string = "0.3.4"
chrono-tz = "0.8.4"
Expand Down
21 changes: 20 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,9 @@ impl AuthenticatedApi<'_> {
PathArg(org),
PathArg(project)
);
self.post(&path, body)
self.request(Method::Post, &path)?
.with_zstd_json_body(body)?
.send()
}

/// Fetches upload options for snapshots.
Expand Down Expand Up @@ -1360,6 +1362,23 @@ impl ApiRequest {
Ok(self)
}

pub fn with_zstd_json_body<S: Serialize>(mut self, body: &S) -> ApiResult<Self> {
let mut body_bytes: Vec<u8> = vec![];
serde_json::to_writer(&mut body_bytes, &body)
.map_err(|err| ApiError::with_source(ApiErrorKind::CannotSerializeAsJson, err))?;
let compressed = zstd::encode_all(body_bytes.as_slice(), 0)
.map_err(|err| ApiError::with_source(ApiErrorKind::CompressionFailed, err))?;
debug!(
"zstd json body: {} bytes compressed to {} bytes",
body_bytes.len(),
compressed.len()
);
self.body = Some(compressed);
self.headers.append("Content-Type: application/json")?;
self.headers.append("Content-Encoding: zstd")?;
Ok(self)
}
Comment on lines +1365 to +1380

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

m: The current code unnecessarily serializes the entire body into memory uncompressed prior to compressing it.

With the following patch, we avoid the extra allocation by writing directly into a zstd encoder.

diff --git a/src/api/mod.rs b/src/api/mod.rs
index e7ca1c00..24a37418 100644
--- a/src/api/mod.rs
+++ b/src/api/mod.rs
@@ -42,6 +42,7 @@ use symbolic::common::DebugId;
 use symbolic::debuginfo::ObjectKind;
 use url::Url;
 use uuid::Uuid;
+use zstd::Encoder as ZstdEncoder;
 
 use crate::api::errors::{ProjectRenamedError, RetryError};
 use crate::config::{Auth, Config};
@@ -1363,16 +1364,23 @@ impl ApiRequest {
     }
 
     pub fn with_zstd_json_body<S: Serialize>(mut self, body: &S) -> ApiResult<Self> {
-        let mut body_bytes: Vec<u8> = vec![];
-        serde_json::to_writer(&mut body_bytes, &body)
-            .map_err(|err| ApiError::with_source(ApiErrorKind::CannotSerializeAsJson, err))?;
-        let compressed = zstd::encode_all(body_bytes.as_slice(), 0)
+        let mut encoder = ZstdEncoder::new(Vec::new(), 0)
             .map_err(|err| ApiError::with_source(ApiErrorKind::CompressionFailed, err))?;
-        debug!(
-            "zstd json body: {} bytes compressed to {} bytes",
-            body_bytes.len(),
-            compressed.len()
-        );
+
+        serde_json::to_writer(&mut encoder, &body).map_err(|err| {
+            let kind = if err.is_io() {
+                ApiErrorKind::CompressionFailed
+            } else {
+                ApiErrorKind::CannotSerializeAsJson
+            };
+            ApiError::with_source(kind, err)
+        })?;
+
+        let compressed = encoder
+            .finish()
+            .map_err(|err| ApiError::with_source(ApiErrorKind::CompressionFailed, err))?;
+
+        debug!("zstd json body: {} bytes compressed", compressed.len());
         self.body = Some(compressed);
         self.headers.append("Content-Type: application/json")?;
         self.headers.append("Content-Encoding: zstd")?;


pub fn with_body(mut self, body: Vec<u8>) -> Self {
self.body = Some(body);
self
Expand Down
Loading