Skip to content

Commit

Permalink
fix: omit Content-Encoding: zstd if compression fails
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed Feb 27, 2025
1 parent 6212316 commit 2c7bb8e
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions dogstatsd/src/datadog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,34 +193,27 @@ impl DdApi {
.as_ref()
.ok_or_else(|| ShippingError::Destination(None, "No client".to_string()))?;
let start = std::time::Instant::now();
let final_body = {
let result = (|| -> Result<Vec<u8>, Box<dyn Error>> {
let mut encoder =
Encoder::new(Vec::new(), 6).map_err(|e| Box::new(e) as Box<dyn Error>)?;

encoder
.write_all(&body)
.map_err(|e| Box::new(e) as Box<dyn Error>)?;

encoder.finish().map_err(|e| Box::new(e) as Box<dyn Error>)
})();

if let Ok(compressed_data) = result {
compressed_data
} else {
debug!("Failed to compress data, sending uncompressed data");
body
}
};

let resp = client
let result = (|| -> std::io::Result<Vec<u8>> {
let mut encoder = Encoder::new(Vec::new(), 6)?;
encoder.write_all(&body)?;
encoder.finish()
})();

let mut builder = client
.post(&url)
.header("DD-API-KEY", &self.api_key)
.header("Content-Type", content_type)
.header("Content-Encoding", "zstd")
.body(final_body)
.send()
.await;
.header("Content-Type", content_type);

builder = match result {
Ok(compressed) => builder.header("Content-Encoding", "zstd").body(compressed),
Err(err) => {
debug!("Sending uncompressed data, failed to compress: {err}");
builder.body(body)
}
};

let resp = builder.send().await;

let elapsed = start.elapsed();
debug!("Request to {} took {}ms", url, elapsed.as_millis());
Expand Down

0 comments on commit 2c7bb8e

Please sign in to comment.