Skip to content

Commit 9d634cb

Browse files
ref(vector): restructure code (#219)
- [x] remove debug logs - [x] move send_batch out of class - [x] use a generic error for vector errors --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent a735439 commit 9d634cb

File tree

2 files changed

+38
-83
lines changed

2 files changed

+38
-83
lines changed

src/producer.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ pub enum ExtractCodeError {
1616
Producer(#[from] ProducerError),
1717
#[error(transparent)]
1818
Schema(#[from] SchemaError),
19-
#[error(transparent)]
20-
VectorRequestError(#[from] reqwest::Error),
21-
#[error("Vector request failed with status: {0}")]
22-
VectorRequestStatusError(reqwest::StatusCode),
19+
#[error("Vector request failed")]
20+
VectorError,
2321
}
2422

2523
pub trait ResultsProducer: Send + Sync {

src/producer/vector_producer.rs

+36-79
Original file line numberDiff line numberDiff line change
@@ -25,78 +25,31 @@ impl VectorResultsProducer {
2525
(Self { schema, sender }, worker)
2626
}
2727

28-
async fn send_batch(
29-
batch: Vec<Vec<u8>>,
30-
client: Client,
31-
endpoint: String,
32-
) -> Result<(), ExtractCodeError> {
33-
if batch.is_empty() {
34-
return Ok(());
35-
}
36-
37-
let body: String = batch
38-
.iter()
39-
.filter_map(|json| String::from_utf8(json.clone()).ok())
40-
.map(|s| s + "\n")
41-
.collect();
42-
43-
// Log the exact payload for debugging
44-
tracing::debug!(%body, "payload.sending");
45-
tracing::debug!(size = body.len(), "request.sending_to_vector");
46-
47-
let response = client
48-
.post(&endpoint)
49-
.header("Content-Type", "application/json")
50-
.body(body)
51-
.send()
52-
.await;
53-
54-
match response {
55-
Ok(_) => {
56-
tracing::debug!("request.sent_successfully");
57-
Ok(())
58-
}
59-
Err(e) => {
60-
tracing::error!(error = ?e, "request.failed_to_vector");
61-
Err(ExtractCodeError::VectorRequestStatusError(
62-
reqwest::StatusCode::INTERNAL_SERVER_ERROR,
63-
))
64-
}
65-
}
66-
}
67-
6828
fn spawn_worker(
6929
vector_batch_size: usize,
7030
client: Client,
7131
endpoint: String,
7232
mut receiver: UnboundedReceiver<Vec<u8>>,
7333
) -> JoinHandle<()> {
7434
tokio::spawn(async move {
75-
tracing::debug!(%endpoint, "worker.started");
7635
let mut batch = Vec::with_capacity(vector_batch_size);
7736

7837
while let Some(json) = receiver.recv().await {
79-
tracing::debug!(size = json.len(), "event.received");
8038
batch.push(json);
81-
tracing::debug!(size = batch.len(), "batch.size.updated");
8239

8340
if batch.len() < vector_batch_size {
8441
continue;
8542
}
8643

87-
tracing::debug!(size = batch.len(), "batch.sending");
8844
let batch_to_send =
8945
std::mem::replace(&mut batch, Vec::with_capacity(vector_batch_size));
90-
if let Err(e) =
91-
Self::send_batch(batch_to_send, client.clone(), endpoint.clone()).await
92-
{
93-
tracing::error!(error = ?e, "batch.send_failed");
46+
if let Err(e) = send_batch(batch_to_send, client.clone(), endpoint.clone()).await {
47+
tracing::error!(error = ?e, "vector_batch.send_failed");
9448
}
9549
}
9650

9751
if !batch.is_empty() {
98-
tracing::debug!(size = batch.len(), "final_batch.sending");
99-
if let Err(e) = Self::send_batch(batch, client, endpoint).await {
52+
if let Err(e) = send_batch(batch, client, endpoint).await {
10053
tracing::error!(error = ?e, "final_batch.send_failed");
10154
}
10255
}
@@ -114,15 +67,44 @@ impl ResultsProducer for VectorResultsProducer {
11467
// Send the serialized result to the worker task
11568
if self.sender.send(json).is_err() {
11669
tracing::error!("event.send_failed_channel_closed");
117-
return Err(ExtractCodeError::VectorRequestStatusError(
118-
reqwest::StatusCode::INTERNAL_SERVER_ERROR,
119-
));
70+
return Err(ExtractCodeError::VectorError);
12071
}
12172

12273
Ok(())
12374
}
12475
}
12576

77+
async fn send_batch(
78+
batch: Vec<Vec<u8>>,
79+
client: Client,
80+
endpoint: String,
81+
) -> Result<(), ExtractCodeError> {
82+
if batch.is_empty() {
83+
return Ok(());
84+
}
85+
86+
let body: String = batch
87+
.iter()
88+
.filter_map(|json| String::from_utf8(json.clone()).ok())
89+
.map(|s| s + "\n")
90+
.collect();
91+
92+
let response = client
93+
.post(&endpoint)
94+
.header("Content-Type", "application/json")
95+
.body(body)
96+
.send()
97+
.await;
98+
99+
match response {
100+
Ok(_) => Ok(()),
101+
Err(e) => {
102+
tracing::error!(error = ?e, "request.failed_to_vector");
103+
Err(ExtractCodeError::VectorError)
104+
}
105+
}
106+
}
107+
126108
#[cfg(test)]
127109
mod tests {
128110
use super::VectorResultsProducer;
@@ -170,7 +152,6 @@ mod tests {
170152
#[tokio::test]
171153
async fn test_single_event() {
172154
let mock_server = MockServer::start();
173-
tracing::debug!("Mock server started at {}", mock_server.url("/"));
174155
let test_result = create_test_result();
175156

176157
let mock = mock_server.mock(|when, then| {
@@ -179,16 +160,11 @@ mod tests {
179160
.header("Content-Type", "application/json")
180161
.matches(|req| {
181162
if let Some(body) = &req.body {
182-
tracing::debug!(
183-
"Received request body: {:?}",
184-
String::from_utf8_lossy(body)
185-
);
186163
let lines: Vec<_> = body
187164
.split(|&b| b == b'\n')
188165
.filter(|l| !l.is_empty())
189166
.collect();
190167
if lines.len() != 1 {
191-
tracing::debug!("Expected 1 line, got {}", lines.len());
192168
return false;
193169
}
194170
if let Ok(value) = serde_json::from_slice::<serde_json::Value>(lines[0]) {
@@ -221,16 +197,14 @@ mod tests {
221197
#[tokio::test]
222198
async fn test_batch_events() {
223199
let mock_server = MockServer::start();
224-
tracing::debug!("Mock server started at {}", mock_server.url("/"));
225200
let (producer, worker) = VectorResultsProducer::new(
226201
"uptime-results",
227202
mock_server.url("/").to_string(),
228203
TEST_BATCH_SIZE,
229204
);
230205

231206
// Create and send BATCH_SIZE + 2 events
232-
for i in 0..(10 + 2) {
233-
tracing::debug!("Sending event {}", i + 1);
207+
for _ in 0..(TEST_BATCH_SIZE + 2) {
234208
let test_result = create_test_result();
235209
let result = producer.produce_checker_result(&test_result);
236210
assert!(result.is_ok());
@@ -243,17 +217,12 @@ mod tests {
243217
.header("Content-Type", "application/json")
244218
.matches(|req| {
245219
if let Some(body) = &req.body {
246-
tracing::debug!(
247-
"Received request body: {:?}",
248-
String::from_utf8_lossy(body)
249-
);
250220
let lines: Vec<_> = body
251221
.split(|&b| b == b'\n')
252222
.filter(|l| !l.is_empty())
253223
.collect();
254224
let len = lines.len();
255225
if len != 10 && len != 2 {
256-
tracing::debug!("Expected {} or 2 lines, got {}", 10, len);
257226
return false;
258227
}
259228
// Verify each line is valid JSON with expected fields
@@ -289,7 +258,6 @@ mod tests {
289258
#[tokio::test]
290259
async fn test_server_error() {
291260
let mock_server = MockServer::start();
292-
tracing::debug!("Mock server started at {}", mock_server.url("/"));
293261
let test_result = create_test_result();
294262

295263
let error_mock = mock_server.mock(|when, then| {
@@ -298,16 +266,11 @@ mod tests {
298266
.header("Content-Type", "application/json")
299267
.matches(|req| {
300268
if let Some(body) = &req.body {
301-
tracing::debug!(
302-
"Received request body: {:?}",
303-
String::from_utf8_lossy(body)
304-
);
305269
let lines: Vec<_> = body
306270
.split(|&b| b == b'\n')
307271
.filter(|l| !l.is_empty())
308272
.collect();
309273
if lines.len() != 1 {
310-
tracing::debug!("Expected 1 line, got {}", lines.len());
311274
return false;
312275
}
313276
if let Ok(value) = serde_json::from_slice::<serde_json::Value>(lines[0]) {
@@ -343,25 +306,19 @@ mod tests {
343306
#[tokio::test]
344307
async fn test_flush_on_shutdown() {
345308
let mock_server = MockServer::start();
346-
tracing::debug!("Mock server started at {}", mock_server.url("/"));
347309
// Create a mock that expects a single request with less than BATCH_SIZE events
348310
let mock = mock_server.mock(|when, then| {
349311
when.method(Method::POST)
350312
.path("/")
351313
.header("Content-Type", "application/json")
352314
.matches(|req| {
353315
if let Some(body) = &req.body {
354-
tracing::debug!(
355-
"Received request body: {:?}",
356-
String::from_utf8_lossy(body)
357-
);
358316
let lines: Vec<_> = body
359317
.split(|&b| b == b'\n')
360318
.filter(|l| !l.is_empty())
361319
.collect();
362320
// We expect only 1 event, which is less than BATCH_SIZE
363321
if lines.len() != 1 {
364-
tracing::debug!("Expected 1 line, got {}", lines.len());
365322
return false;
366323
}
367324
if let Ok(value) = serde_json::from_slice::<serde_json::Value>(lines[0]) {

0 commit comments

Comments
 (0)