Skip to content

Commit 8fb522e

Browse files
fix(logs): remove logs to avoid circular logging
1 parent 84d3221 commit 8fb522e

File tree

5 files changed

+25
-47
lines changed

5 files changed

+25
-47
lines changed

dd-tracing-layer/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dd-tracing-layer"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["Roberto Huertas <[email protected]>"]
55
description = "Send your logs to Datadog"
66
edition = "2021"
@@ -24,9 +24,7 @@ serde_json = "1"
2424
chrono = "0.4"
2525
async-trait = "0.1"
2626
async-recursion = "1.0"
27-
log = "0.4"
2827

2928
[dev-dependencies]
3029
tracing-subscriber = { version = "0.3.16", features = ["json", "registry"] }
3130
httpmock = "0.6.7"
32-
simple_logger = "4.0.0"

dd-tracing-layer/src/datadog_ingestor.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{collections::VecDeque, sync::Arc};
77
use tokio::sync::RwLock;
88

99
const DD_SOURCE: &str = "dd-tracing-layer";
10-
const DD_TAGS: &str = "version:0.1.0";
10+
const DD_TAGS: &str = "version:0.1.2";
1111
const MAX_BATCH_SIZE: usize = 1000;
1212
const MAX_BATCH_DURATION_SECS: i64 = 5;
1313
const MAX_RETRIES: u8 = 3;
@@ -99,13 +99,10 @@ impl DatadogLogIngestor {
9999
#[async_recursion]
100100
async fn send_logs(&self, logs: &[Log], retries: u8) {
101101
if retries > MAX_RETRIES {
102-
log::error!("Failed to send logs after {} retries", retries);
102+
eprintln!("Failed to send logs after {} retries", retries);
103103
}
104104

105-
let retry = || {
106-
log::info!("Retrying...");
107-
self.send_logs(logs, retries + 1)
108-
};
105+
let retry = || self.send_logs(logs, retries + 1);
109106

110107
// https://docs.datadoghq.com/api/latest/logs/?code-lang=typescript
111108
match self
@@ -119,23 +116,23 @@ impl DatadogLogIngestor {
119116
{
120117
Ok(res) => match res.status().as_u16() {
121118
202 => {
122-
log::info!("Accepted: the request has been accepted for processing");
119+
//println!("Accepted: the request has been accepted for processing");
123120
}
124121
400 => {
125-
log::error!("Bad request (likely an issue in the payload formatting)");
122+
eprintln!("Bad request (likely an issue in the payload formatting)");
126123
}
127124
401 => {
128-
log::error!("Unauthorized (likely a missing API Key)");
125+
eprintln!("Unauthorized (likely a missing API Key)");
129126
}
130127
403 => {
131-
log::error!("Permission issue (likely using an invalid API Key)");
128+
eprintln!("Permission issue (likely using an invalid API Key)");
132129
}
133130
408 => {
134-
log::error!("Request Timeout, request should be retried after some time");
131+
eprintln!("Request Timeout, request should be retried after some time");
135132
retry().await;
136133
}
137134
413 => {
138-
log::error!("Payload too large (batch is above 5MB uncompressed)");
135+
eprintln!("Payload too large (batch is above 5MB uncompressed)");
139136
// split batch
140137
let logs_len = logs.len();
141138
let half = logs_len / 2;
@@ -144,36 +141,34 @@ impl DatadogLogIngestor {
144141
self.send_logs(right, retries + 1).await;
145142
}
146143
429 => {
147-
log::error!("Too Many Requests, request should be retried after some time");
144+
eprintln!("Too Many Requests, request should be retried after some time");
148145
retry().await;
149146
}
150147
500 => {
151-
log::error!("Internal Server Error, the server encountered an unexpected condition that prevented it from fulfilling the request, request should be retried after some time");
148+
eprintln!("Internal Server Error, the server encountered an unexpected condition that prevented it from fulfilling the request, request should be retried after some time");
152149
retry().await;
153150
}
154151
503 => {
155-
log::error!("Service Unavailable, the server is not ready to handle the request probably because it is overloaded, request should be retried after some time");
152+
eprintln!("Service Unavailable, the server is not ready to handle the request probably because it is overloaded, request should be retried after some time");
156153
retry().await;
157154
}
158155
_ => {
159-
log::error!("Unknown error, try again later");
156+
eprintln!("Unknown error, try again later");
160157
retry().await;
161158
}
162159
},
163160
Err(e) => {
164-
log::error!("Failed to send logs to Datadog: {:?}", e);
161+
eprintln!("Failed to send logs to Datadog: {:?}", e);
165162
}
166163
}
167164
}
168165

169166
#[async_recursion]
170167
async fn try_send(&mut self, is_flush: bool) {
171-
log::info!("Flushing...");
172168
{
173169
// send current logs to datadog if there are any
174170
let queue = self.queue.read().await;
175171
if queue.is_empty() {
176-
log::info!("Queue is empty, skipping flush");
177172
return;
178173
}
179174
if !is_flush {
@@ -182,7 +177,6 @@ impl DatadogLogIngestor {
182177
let now = Utc::now();
183178
let diff = now - last_log.received_at;
184179
if diff < Duration::seconds(MAX_BATCH_DURATION_SECS) {
185-
log::info!("Last log is too recent, skipping flush: {:?}", diff);
186180
return;
187181
}
188182
}
@@ -198,7 +192,6 @@ impl DatadogLogIngestor {
198192

199193
// send them (retries if it fails)
200194
self.send_logs(&logs, 0).await;
201-
log::info!("Flushed {} logs", logs.len());
202195

203196
// check if the queue is empty and flush again if it's not
204197
let is_queue_empty = { self.queue.read().await.is_empty() };

dd-tracing-layer/tests/datadog.rs

-10
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ mod tests {
1818

1919
#[test]
2020
fn datadog_works() {
21-
simple_logger::SimpleLogger::new()
22-
.with_level(log::LevelFilter::Info)
23-
.with_module_level("log_tracing_layer::layer", log::LevelFilter::Debug)
24-
.with_module_level(
25-
"dd_tracing_layer::datadog_ingestor",
26-
log::LevelFilter::Debug,
27-
)
28-
.without_timestamps()
29-
.init()
30-
.unwrap();
3121
let server = httpmock::MockServer::start();
3222
let _mock = server.mock(|when, then| {
3323
when.any_request();

log-tracing-layer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "log-tracing-layer"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["Roberto Huertas <[email protected]>"]
55
description = "Build your own custom tracing layer."
66
edition = "2021"

log-tracing-layer/src/layer.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl LogLayer {
3232
.build()
3333
{
3434
Err(e) => {
35-
log::error!("Runtime creation failure: {:?}", e);
35+
eprintln!("Runtime creation failure: {:?}", e);
3636
return;
3737
}
3838
Ok(r) => r,
@@ -41,13 +41,10 @@ impl LogLayer {
4141
rt.block_on(async move {
4242
ingestor.start();
4343
while let Some(log) = rx.recv().await {
44-
log::info!("LAYER: Adding log to ingestor");
4544
ingestor.ingest(log).await;
4645
}
47-
log::info!("LAYER: Done sending logs");
4846
ingestor.flush().await;
4947
});
50-
log::info!("LAYER: Dropping runtime");
5148
drop(rt);
5249
})
5350
.expect("Something went wrong spawning the thread");
@@ -76,10 +73,11 @@ impl LogLayer {
7673
}
7774
}
7875

79-
// there will be one for sure. We use that to get the span
80-
let last = spans.last().unwrap();
81-
log.insert("span".to_string(), json!(last));
82-
log.insert("spans".to_string(), json!(spans));
76+
// if no last span, it means there are no spans at all
77+
if let Some(last) = spans.last() {
78+
log.insert("span".to_string(), json!(last));
79+
log.insert("spans".to_string(), json!(spans));
80+
}
8381

8482
log.insert(
8583
"level".to_string(),
@@ -106,7 +104,6 @@ impl LogLayer {
106104
json!(chrono::Utc::now().to_rfc3339()),
107105
);
108106

109-
log::debug!("LAYER: log = {:#?}", log);
110107
log
111108
}
112109
}
@@ -121,7 +118,6 @@ impl Drop for LogLayer {
121118
if let Some(handle) = self.handle.take() {
122119
let _result = handle.join();
123120
}
124-
log::info!("LAYER: Dropped!");
125121
}
126122
}
127123

@@ -146,10 +142,11 @@ where
146142

147143
fn on_event(&self, event: &tracing::Event<'_>, ctx: tracing_subscriber::layer::Context<'_, S>) {
148144
// send to the channel
149-
log::info!("LAYER: Sending to ingestor");
150145
if let Some(tx) = &self.tx {
151146
let log = Self::create_log(event, &ctx);
152-
tx.send(log).unwrap();
147+
if let Err(e) = tx.send(log) {
148+
eprintln!("LAYER: Error sending log to ingestor: {:?}", e);
149+
}
153150
}
154151
}
155152
}

0 commit comments

Comments
 (0)