Skip to content

Commit bfabfbb

Browse files
committed
misc: rework compression feature
1 parent 7751003 commit bfabfbb

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/compression.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[cfg(feature = "compression")]
2+
use libflate::gzip;
3+
4+
const TARGET_LOG_COMPRESSION: &str = "apollo-studio-extension-compression";
5+
6+
#[cfg(feature = "compression")]
7+
pub fn compress(msg: Vec<u8>) -> Result<Vec<u8>, std::io::Error> {
8+
let mut encoder = gzip::Encoder::new(Vec::new()).unwrap();
9+
let mut msg = std::io::Cursor::new(msg);
10+
11+
match std::io::copy(&mut msg, &mut encoder) {
12+
Ok(_) => {}
13+
Err(e) => {
14+
error!(target: TARGET_LOG_COMPRESSION, message = "An issue happened while GZIP compression", err = ?e);
15+
return Err(e);
16+
}
17+
};
18+
19+
encoder.finish().into_result()
20+
}
21+
22+
#[cfg(not(feature = "compression"))]
23+
pub fn compress(msg: Vec<u8>) -> Result<Vec<u8>, std::io::Error> {
24+
Ok::<Vec<u8>, std::io::Error>(msg)
25+
}

src/lib.rs

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//! # Apollo Studio Extension for Performance Tracing for async_graphql crates
2+
mod compression;
23
mod packages;
34
mod proto;
45
pub mod register;
56

67
#[macro_use]
78
extern crate tracing;
8-
/*
9-
* TODO:
10-
* - Gzip compression (libflate)
11-
*/
129
use packages::uname::Uname;
1310
use std::collections::HashMap;
1411
use std::sync::Arc;
@@ -203,41 +200,22 @@ impl ApolloTracing {
203200
}
204201
};
205202

206-
let result = if cfg!(features = "compression") {
207-
let mut encoder = libflate::gzip::Encoder::new(Vec::new()).unwrap();
208-
let mut msg = std::io::Cursor::new(msg);
209-
match std::io::copy(&mut msg, &mut encoder) {
210-
Ok(_) => {}
211-
Err(e) => {
212-
error!(target: TARGET_LOG, message = "An issue happened while GZIP compression", err = ?e);
213-
continue;
214-
}
215-
};
216-
match encoder.finish().into_result() {
217-
Ok(msg) => {
218-
client
219-
.post(REPORTING_URL)
220-
.body(msg)
221-
.header("content-type", "application/protobuf")
222-
.header("X-Api-Key", &authorization_token)
223-
.send()
224-
.await
225-
}
226-
Err(e) => {
227-
error!(target: TARGET_LOG, message = "An issue happened while GZIP compression", err = ?e);
228-
continue;
229-
}
203+
let msg = match compression::compress(msg) {
204+
Ok(result) => result,
205+
Err(e) => {
206+
error!(target: TARGET_LOG, message = "An issue happened while GZIP compression", err = ?e);
207+
continue;
230208
}
231-
} else {
232-
client
233-
.post(REPORTING_URL)
234-
.body(msg)
235-
.header("content-type", "application/protobuf")
236-
.header("X-Api-Key", &authorization_token)
237-
.send()
238-
.await
239209
};
240210

211+
let result = client
212+
.post(REPORTING_URL)
213+
.body(msg)
214+
.header("content-type", "application/protobuf")
215+
.header("X-Api-Key", &authorization_token)
216+
.send()
217+
.await;
218+
241219
match result {
242220
Ok(data) => {
243221
span_batch.record("response", &debug(&data));

0 commit comments

Comments
 (0)