1
1
use super :: { BatchLogProcessor , LogProcessor , LogRecord , SimpleLogProcessor , TraceContext } ;
2
2
use crate :: { export:: logs:: LogExporter , runtime:: RuntimeChannel , Resource } ;
3
3
use crate :: { logs:: LogError , logs:: LogResult } ;
4
- use opentelemetry:: otel_info;
5
- use opentelemetry:: { otel_debug, trace:: TraceContextExt , Context , InstrumentationScope } ;
4
+ use opentelemetry:: { otel_debug, otel_info, trace:: TraceContextExt , Context , InstrumentationScope } ;
6
5
7
6
#[ cfg( feature = "spec_unstable_logs_enabled" ) ]
8
7
use opentelemetry:: logs:: Severity ;
@@ -45,19 +44,10 @@ pub struct LoggerProvider {
45
44
inner : Arc < LoggerProviderInner > ,
46
45
}
47
46
48
- /// Default logger name if empty string is provided.
49
- const DEFAULT_COMPONENT_NAME : & str = "rust.opentelemetry.io/sdk/logger" ;
50
-
51
47
impl opentelemetry:: logs:: LoggerProvider for LoggerProvider {
52
48
type Logger = Logger ;
53
49
54
50
fn logger ( & self , name : impl Into < Cow < ' static , str > > ) -> Self :: Logger {
55
- let mut name = name. into ( ) ;
56
-
57
- if name. is_empty ( ) {
58
- name = Cow :: Borrowed ( DEFAULT_COMPONENT_NAME )
59
- } ;
60
-
61
51
let scope = InstrumentationScope :: builder ( name) . build ( ) ;
62
52
self . logger_with_scope ( scope)
63
53
}
@@ -67,6 +57,9 @@ impl opentelemetry::logs::LoggerProvider for LoggerProvider {
67
57
if self . inner . is_shutdown . load ( Ordering :: Relaxed ) {
68
58
return Logger :: new ( scope, NOOP_LOGGER_PROVIDER . clone ( ) ) ;
69
59
}
60
+ if scope. name ( ) . is_empty ( ) {
61
+ otel_info ! ( name: "LoggerNameEmpty" , message = "Logger name is empty; consider providing a meaningful name. Logger will function normally and the provided name will be used as-is." ) ;
62
+ } ;
70
63
Logger :: new ( scope, self . clone ( ) )
71
64
}
72
65
}
@@ -706,6 +699,42 @@ mod tests {
706
699
assert_eq ! ( * shutdown_called. lock( ) . unwrap( ) , 1 ) ;
707
700
}
708
701
702
+ #[ test]
703
+ fn test_empty_logger_name ( ) {
704
+ let exporter = InMemoryLogExporter :: default ( ) ;
705
+ let logger_provider = LoggerProvider :: builder ( )
706
+ . with_simple_exporter ( exporter. clone ( ) )
707
+ . build ( ) ;
708
+ let logger = logger_provider. logger ( "" ) ;
709
+ let mut record = logger. create_log_record ( ) ;
710
+ record. set_body ( "Testing empty logger name" . into ( ) ) ;
711
+ logger. emit ( record) ;
712
+
713
+ // Create a logger using a scope with an empty name
714
+ let scope = InstrumentationScope :: builder ( "" ) . build ( ) ;
715
+ let scoped_logger = logger_provider. logger_with_scope ( scope) ;
716
+ let mut scoped_record = scoped_logger. create_log_record ( ) ;
717
+ scoped_record. set_body ( "Testing empty logger scope name" . into ( ) ) ;
718
+ scoped_logger. emit ( scoped_record) ;
719
+
720
+ // Assert: Verify that the emitted logs are processed correctly
721
+ let emitted_logs = exporter. get_emitted_logs ( ) . unwrap ( ) ;
722
+ assert_eq ! ( emitted_logs. len( ) , 2 ) ;
723
+ // Assert the first log
724
+ assert_eq ! (
725
+ emitted_logs[ 0 ] . clone( ) . record. body,
726
+ Some ( AnyValue :: String ( "Testing empty logger name" . into( ) ) )
727
+ ) ;
728
+ assert_eq ! ( logger. instrumentation_scope( ) . name( ) , "" ) ;
729
+
730
+ // Assert the second log created through the scope
731
+ assert_eq ! (
732
+ emitted_logs[ 1 ] . clone( ) . record. body,
733
+ Some ( AnyValue :: String ( "Testing empty logger scope name" . into( ) ) )
734
+ ) ;
735
+ assert_eq ! ( scoped_logger. instrumentation_scope( ) . name( ) , "" ) ;
736
+ }
737
+
709
738
#[ derive( Debug ) ]
710
739
pub ( crate ) struct LazyLogProcessor {
711
740
shutdown_called : Arc < Mutex < bool > > ,
0 commit comments