Skip to content

Commit cc46244

Browse files
Remove setting of global subscriber (#61)
* Remove setting of global subscriber Now just return the tracer, where it can be used in the standard "with" commands to layer the tracer. The logger still is set, as recommended by the Rust docs, but the tracer must be set EXTERNAL to the DatadogTracing::new) command. The env config still works for both the logger and the tracing log events, so that can still be used to filter appropriately, although they can be set to null and other filters used as other layers instead. Tests were added and modified to test that the logs do show when needed and filter out when the config syas so. Version was updated by major version (0.7 -> 0.8) as thgis would require usage change by all users (must now set subscriber after calling new()) * Add test for standard filter target --------- Co-authored-by: kitsuneninetails <kitsuneninetails@users.noreply.github.com>
1 parent 1332dc6 commit cc46244

2 files changed

Lines changed: 87 additions & 26 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "datadog-apm-sync"
3-
version = "0.7.1"
3+
version = "0.8.0"
44
authors = ["Michael Micucci <9975355+kitsuneninetails@users.noreply.github.com>", "Fernando Gonçalves <fernando.goncalves@pipefy.com> (original base code)"]
55
edition = "2018"
66
license = "MIT"
@@ -20,6 +20,7 @@ tracing = "0.1"
2020

2121
[dev-dependencies]
2222
tokio = { version = "1.0", features = ["full"] }
23+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
2324

2425
[features]
2526
json = ["log/kv", "log/kv_serde"]

src/client.rs

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{api::RawSpan, model::Span};
33
use atomic_float::AtomicF64;
44
use attohttpc;
55
use chrono::{DateTime, Duration, Utc};
6-
use log::{warn, Level as LogLevel, Log, Record};
6+
use log::{Level as LogLevel, Log, Record};
77
use serde_json::to_string;
88
use std::{
99
cell::Cell,
@@ -354,7 +354,7 @@ impl SpanStorage {
354354
}
355355

356356
fn filter_log(storage: &SpanStorage, log_config: &LoggingConfig, record: LogRecord) {
357-
let skip = record
357+
let mod_skip = record
358358
.module
359359
.as_ref()
360360
.map(|m: &String| {
@@ -368,7 +368,7 @@ fn filter_log(storage: &SpanStorage, log_config: &LoggingConfig, record: LogReco
368368
.body_filter
369369
.iter()
370370
.any(|f| record.msg_str.contains(*f));
371-
if !skip && !body_skip {
371+
if !mod_skip && !body_skip {
372372
let log_body = build_log_body(&record);
373373
match storage
374374
.get_trace_id_for_thread(record.thread_id)
@@ -537,13 +537,6 @@ impl DatadogTracing {
537537
// This must be marked unsafe because we are overwriting a global, but it only gets done
538538
// once in a process's lifetime.
539539
SAMPLING_RATE.store(sample_rate, Ordering::Release);
540-
541-
tracing::subscriber::set_global_default(tracer.clone()).unwrap_or_else(|_| {
542-
warn!(
543-
"Global subscriber has already been set! \
544-
This should only be set once in the executable."
545-
)
546-
});
547540
}
548541
tracer
549542
}
@@ -885,7 +878,8 @@ impl DdAgentClient {
885878
#[cfg(test)]
886879
mod tests {
887880
use super::*;
888-
use log::{debug, info, Level};
881+
use log::{debug, info};
882+
use tracing::level_filters;
889883
use tracing::{event, span};
890884

891885
fn long_call(trace_id: u64) {
@@ -1004,27 +998,41 @@ mod tests {
1004998
);
1005999
}
10061000

1007-
fn trace_config() {
1001+
fn trace_config(log_level: log::Level, trace_level: tracing::Level) {
10081002
let config = Config {
10091003
service: String::from("datadog_apm_test"),
10101004
env: Some("staging-01".into()),
10111005
logging_config: Some(LoggingConfig {
1012-
level: Level::Trace,
1013-
mod_filter: vec!["hyper", "mime"],
1006+
level: log_level,
1007+
mod_filter: vec!["hyper", "mime", "test_log"],
10141008
..LoggingConfig::default()
10151009
}),
10161010
enable_tracing: true,
10171011
..Default::default()
10181012
};
1019-
let _client = DatadogTracing::new(config);
1013+
let _client = DatadogTracing::new(config)
1014+
.with(
1015+
filter::targets::Targets::new()
1016+
.with_target(
1017+
"datadog_apm_sync::client::tests::test_trace",
1018+
level_filters::LevelFilter::OFF,
1019+
)
1020+
.with_default(trace_level),
1021+
)
1022+
.try_init()
1023+
.or_else(|e| {
1024+
log::warn!("Error initializing logger: {e}");
1025+
Result::<(), ()>::Ok(())
1026+
})
1027+
.unwrap();
10201028
}
10211029

10221030
#[test]
10231031
fn test_exit_child_span() {
1024-
trace_config();
1032+
trace_config(log::Level::Trace, tracing::Level::TRACE);
10251033
let trace_id = 1u64;
10261034

1027-
let f1 = std::thread::spawn(move || {
1035+
let f1: std::thread::JoinHandle<()> = std::thread::spawn(move || {
10281036
let span = span!(tracing::Level::INFO, "parent_span", trace_id = trace_id);
10291037
let _e = span.enter();
10301038
info!("Inside parent_span, should print trace and span ID");
@@ -1043,7 +1051,7 @@ mod tests {
10431051
#[test]
10441052
fn test_trace_one_func_stack() {
10451053
let trace_id = create_unique_id64();
1046-
trace_config();
1054+
trace_config(log::Level::Trace, tracing::Level::TRACE);
10471055

10481056
debug!(
10491057
"Outside of span, this should be None: {:?}",
@@ -1071,7 +1079,7 @@ mod tests {
10711079
fn test_parallel_two_threads_two_traces() {
10721080
let trace_id1 = create_unique_id64();
10731081
let trace_id2 = create_unique_id64();
1074-
trace_config();
1082+
trace_config(log::Level::Trace, tracing::Level::TRACE);
10751083
let f1 = std::thread::spawn(move || {
10761084
traced_func_no_send(trace_id1);
10771085
event!(tracing::Level::INFO, send_trace = trace_id1);
@@ -1098,7 +1106,7 @@ mod tests {
10981106
let trace_id8 = create_unique_id64() + 7;
10991107
let trace_id9 = create_unique_id64() + 8;
11001108
let trace_id10 = create_unique_id64() + 9;
1101-
trace_config();
1109+
trace_config(log::Level::Trace, tracing::Level::TRACE);
11021110
let f1 = std::thread::spawn(move || {
11031111
traced_func_no_send(trace_id1);
11041112
event!(tracing::Level::INFO, send_trace = trace_id1);
@@ -1156,7 +1164,7 @@ mod tests {
11561164
#[test]
11571165
fn test_error_span() {
11581166
let trace_id = create_unique_id64();
1159-
trace_config();
1167+
trace_config(log::Level::Trace, tracing::Level::TRACE);
11601168
let f3 = std::thread::spawn(move || {
11611169
traced_error_func(trace_id);
11621170
});
@@ -1167,7 +1175,7 @@ mod tests {
11671175
#[test]
11681176
fn test_error_span_as_single_event() {
11691177
let trace_id = create_unique_id64();
1170-
trace_config();
1178+
trace_config(log::Level::Trace, tracing::Level::TRACE);
11711179
let f4 = std::thread::spawn(move || {
11721180
traced_error_func_single_event(trace_id);
11731181
});
@@ -1178,7 +1186,7 @@ mod tests {
11781186
#[test]
11791187
fn test_two_funcs_in_one_span() {
11801188
let trace_id = create_unique_id64();
1181-
trace_config();
1189+
trace_config(log::Level::Trace, tracing::Level::TRACE);
11821190
let f5 = std::thread::spawn(move || {
11831191
traced_func_no_send(trace_id);
11841192
traced_func_no_send(trace_id);
@@ -1193,7 +1201,7 @@ mod tests {
11931201
fn test_one_thread_two_funcs_serial_two_traces() {
11941202
let trace_id1 = create_unique_id64();
11951203
let trace_id2 = create_unique_id64();
1196-
trace_config();
1204+
trace_config(log::Level::Trace, tracing::Level::TRACE);
11971205
let f7 = std::thread::spawn(move || {
11981206
traced_func_no_send(trace_id1);
11991207
event!(tracing::Level::INFO, send_trace = trace_id1);
@@ -1208,11 +1216,63 @@ mod tests {
12081216
#[test]
12091217
fn test_http_span() {
12101218
let trace_id = create_unique_id64();
1211-
trace_config();
1219+
trace_config(log::Level::Trace, tracing::Level::TRACE);
12121220
let f3 = std::thread::spawn(move || {
12131221
traced_http_func(trace_id);
12141222
});
12151223
f3.join().unwrap();
12161224
::std::thread::sleep(::std::time::Duration::from_millis(1000));
12171225
}
1226+
1227+
pub mod test_log {
1228+
pub fn test_log_fn() {
1229+
tracing::event!(
1230+
tracing::Level::INFO,
1231+
message = "TEST_INFO EVENT in filtered mod - SHOULD ____NOT____ SEE!!"
1232+
);
1233+
log::info!("TEST_INFO LOG in filtered mod - SHOULD ____NOT____ SEE!!");
1234+
}
1235+
}
1236+
1237+
pub mod test_trace {
1238+
pub fn test_trace_fn() {
1239+
tracing::event!(
1240+
tracing::Level::INFO,
1241+
message = "TEST_INFO EVENT in filtered trace mod - SHOULD ____NOT____ SEE!!"
1242+
);
1243+
}
1244+
}
1245+
1246+
#[test]
1247+
fn test_log() {
1248+
let _trace_id = create_unique_id64();
1249+
trace_config(log::Level::Info, tracing::Level::INFO);
1250+
log::info!("TEST_INFO - SHOULD SEE!!");
1251+
log::debug!("TEST_DEBUG - SHOULD NOT SEE!!");
1252+
1253+
test_log::test_log_fn();
1254+
}
1255+
1256+
use tracing_subscriber::filter;
1257+
use tracing_subscriber::prelude::*;
1258+
1259+
#[test]
1260+
fn test_trace_event_log() {
1261+
let _trace_id = create_unique_id64();
1262+
trace_config(log::Level::Info, tracing::Level::INFO);
1263+
1264+
tracing::info!("TEST_INFO EVENT - SHOULD SEE!!");
1265+
tracing::debug!("TEST_DEBUG - SHOULD ____NOT____ SEE!!");
1266+
tracing::event!(
1267+
tracing::Level::INFO,
1268+
message = "TEST_INFO EVENT - SHOULD SEE!!"
1269+
);
1270+
tracing::event!(
1271+
tracing::Level::DEBUG,
1272+
message = "TEST_DEBUG EVENT - SHOULD ____NOT____ SEE!!"
1273+
);
1274+
1275+
test_log::test_log_fn();
1276+
test_trace::test_trace_fn();
1277+
}
12181278
}

0 commit comments

Comments
 (0)