Skip to content

Commit 52757f0

Browse files
committed
revise tests
1 parent 2e6c2de commit 52757f0

File tree

4 files changed

+64
-76
lines changed

4 files changed

+64
-76
lines changed

opentelemetry-sdk/src/logs/log_emitter.rs

+54-73
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,63 @@ mod tests {
262262

263263
use super::*;
264264
use opentelemetry::global::{logger, set_logger_provider, shutdown_logger_provider};
265-
use opentelemetry::logs::Logger;
266-
use opentelemetry::{Key, KeyValue, Value};
267265
use opentelemetry::logs::{Logger, LoggerProvider as _};
266+
use opentelemetry::{Key, KeyValue, Value};
268267
use std::fmt::{Debug, Formatter};
269268
use std::sync::atomic::AtomicU64;
270269
use std::sync::{Arc, Mutex};
271270
use std::thread;
272271

272+
struct ShutdownTestLogProcessor {
273+
is_shutdown: Arc<Mutex<bool>>,
274+
counter: Arc<AtomicU64>,
275+
}
276+
277+
impl Debug for ShutdownTestLogProcessor {
278+
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
279+
todo!()
280+
}
281+
}
282+
283+
impl ShutdownTestLogProcessor {
284+
pub(crate) fn new(counter: Arc<AtomicU64>) -> Self {
285+
ShutdownTestLogProcessor {
286+
is_shutdown: Arc::new(Mutex::new(false)),
287+
counter,
288+
}
289+
}
290+
}
291+
292+
impl LogProcessor for ShutdownTestLogProcessor {
293+
fn emit(&self, _data: LogData) {
294+
self.is_shutdown
295+
.lock()
296+
.map(|is_shutdown| {
297+
if !*is_shutdown {
298+
self.counter
299+
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
300+
}
301+
})
302+
.expect("lock poisoned");
303+
}
304+
305+
fn force_flush(&self) -> LogResult<()> {
306+
Ok(())
307+
}
308+
309+
fn shutdown(&self) -> LogResult<()> {
310+
self.is_shutdown
311+
.lock()
312+
.map(|mut is_shutdown| *is_shutdown = true)
313+
.expect("lock poisoned");
314+
Ok(())
315+
}
316+
317+
#[cfg(feature = "logs_level_enabled")]
318+
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
319+
true
320+
}
321+
}
273322
#[test]
274323
fn test_logger_provider_default_resource() {
275324
let assert_resource = |provider: &super::LoggerProvider,
@@ -396,57 +445,6 @@ mod tests {
396445
assert_eq!(no_service_name.config().resource.len(), 0);
397446
}
398447

399-
struct ShutdownTestLogProcessor {
400-
is_shutdown: Arc<Mutex<bool>>,
401-
counter: Arc<AtomicU64>,
402-
}
403-
404-
impl Debug for ShutdownTestLogProcessor {
405-
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
406-
todo!()
407-
}
408-
}
409-
410-
impl ShutdownTestLogProcessor {
411-
pub(crate) fn new(counter: Arc<AtomicU64>) -> Self {
412-
ShutdownTestLogProcessor {
413-
is_shutdown: Arc::new(Mutex::new(false)),
414-
counter,
415-
}
416-
}
417-
}
418-
419-
impl LogProcessor for ShutdownTestLogProcessor {
420-
fn emit(&self, _data: LogData) {
421-
self.is_shutdown
422-
.lock()
423-
.map(|is_shutdown| {
424-
if !*is_shutdown {
425-
self.counter
426-
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
427-
}
428-
})
429-
.expect("lock poisoned");
430-
}
431-
432-
fn force_flush(&self) -> LogResult<()> {
433-
Ok(())
434-
}
435-
436-
fn shutdown(&self) -> LogResult<()> {
437-
self.is_shutdown
438-
.lock()
439-
.map(|mut is_shutdown| *is_shutdown = true)
440-
.expect("lock poisoned");
441-
Ok(())
442-
}
443-
444-
#[cfg(feature = "logs_level_enabled")]
445-
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
446-
true
447-
}
448-
}
449-
450448
#[test]
451449
fn shutdown_test() {
452450
let counter = Arc::new(AtomicU64::new(0));
@@ -517,36 +515,19 @@ mod tests {
517515
thread::sleep(std::time::Duration::from_millis(10));
518516
}
519517

520-
// Intentionally *not* calling shutdown/flush on the provider, but
521-
// instead relying on shutdown_logger_provider which causes the global
522-
// provider to be dropped, leading to the sdk logger provider's drop to
523-
// be called, which is expected to call shutdown on processors.
518+
// shutdown logger provider explicitly call shutdown on the logger
524519
shutdown_logger_provider();
525520

526521
// Assert
527522

528-
// shutdown_logger_provider is necessary but not sufficient, as loggers
529-
// hold on to the the provider (via inner provider clones).
530-
assert!(!*shutdown_called.lock().unwrap());
523+
// shutdown_logger_provider should be shutdown regardless if the logger is dropped.
524+
assert!(*shutdown_called.lock().unwrap());
531525

532526
// flush is never called by the sdk.
533527
assert!(!*flush_called.lock().unwrap());
534528

535-
// Drop one of the logger. Not enough!
536-
drop(logger1);
537-
assert!(!*shutdown_called.lock().unwrap());
538-
539-
// drop logger2, which is the only remaining logger in this thread.
540-
// Still not enough!
541-
drop(logger2);
542-
assert!(!*shutdown_called.lock().unwrap());
543-
544-
// now signal the spawned thread to end, which causes it to drop its
545-
// logger. Since that is the last logger, the provider (inner provider)
546-
// is finally dropped, triggering shutdown
547529
*signal_to_end.lock().unwrap() = true;
548530
handle.join().unwrap();
549-
assert!(*shutdown_called.lock().unwrap());
550531

551532
// flush is never called by the sdk.
552533
assert!(!*flush_called.lock().unwrap());

opentelemetry/src/global/logs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl LoggerProvider for GlobalLoggerProvider {
9898
fn library_logger(&self, library: Arc<InstrumentationLibrary>) -> Self::Logger {
9999
BoxedLogger(self.provider.boxed_logger(library))
100100
}
101+
102+
fn shutdown(&self) -> Vec<LogResult<()>> {
103+
self.provider.shutdown()
104+
}
101105
}
102106

103107
static GLOBAL_LOGGER_PROVIDER: Lazy<RwLock<GlobalLoggerProvider>> =

opentelemetry/src/logs/logger.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,5 @@ pub trait LoggerProvider {
7878
///
7979
/// Note that `shutdown` doesn't mean `Drop`(though `Drop` can call `shutdown`).
8080
/// It simply means the provider will emit anything in the buffer(if applicable) and stop processing
81-
fn shutdown(&self) -> Vec<LogResult<()>> {
82-
Vec::new() // noop
83-
}
81+
fn shutdown(&self) -> Vec<LogResult<()>>;
8482
}

opentelemetry/src/logs/noop.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{borrow::Cow, sync::Arc};
22

3+
use crate::logs::LogResult;
34
use crate::{
45
logs::{LogRecord, Logger, LoggerProvider},
56
InstrumentationLibrary, KeyValue,
@@ -32,6 +33,10 @@ impl LoggerProvider for NoopLoggerProvider {
3233
) -> Self::Logger {
3334
NoopLogger(())
3435
}
36+
37+
fn shutdown(&self) -> Vec<LogResult<()>> {
38+
vec![]
39+
}
3540
}
3641

3742
/// A no-op implementation of a [`Logger`]

0 commit comments

Comments
 (0)