Skip to content

Commit a6e2cd6

Browse files
lalitbcijothomas
andauthored
Remove default logger name (#2316)
Co-authored-by: Cijo Thomas <[email protected]> Co-authored-by: Cijo Thomas <[email protected]>
1 parent 882e1c2 commit a6e2cd6

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
"rust.opentelemetry.io/sdk/meter"
2323
[#2334](https://github.com/open-telemetry/opentelemetry-rust/pull/2334)
2424

25+
- Bug fix: Empty Logger names are retained as-is instead of replacing with
26+
"rust.opentelemetry.io/sdk/logger"
27+
[#2316](https://github.com/open-telemetry/opentelemetry-rust/pull/2316)
28+
2529
## 0.27.0
2630

2731
Released 2024-Nov-11

opentelemetry-sdk/src/logs/log_emitter.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::{BatchLogProcessor, LogProcessor, LogRecord, SimpleLogProcessor, TraceContext};
22
use crate::{export::logs::LogExporter, runtime::RuntimeChannel, Resource};
33
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};
65

76
#[cfg(feature = "spec_unstable_logs_enabled")]
87
use opentelemetry::logs::Severity;
@@ -45,19 +44,10 @@ pub struct LoggerProvider {
4544
inner: Arc<LoggerProviderInner>,
4645
}
4746

48-
/// Default logger name if empty string is provided.
49-
const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/logger";
50-
5147
impl opentelemetry::logs::LoggerProvider for LoggerProvider {
5248
type Logger = Logger;
5349

5450
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-
6151
let scope = InstrumentationScope::builder(name).build();
6252
self.logger_with_scope(scope)
6353
}
@@ -67,6 +57,9 @@ impl opentelemetry::logs::LoggerProvider for LoggerProvider {
6757
if self.inner.is_shutdown.load(Ordering::Relaxed) {
6858
return Logger::new(scope, NOOP_LOGGER_PROVIDER.clone());
6959
}
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+
};
7063
Logger::new(scope, self.clone())
7164
}
7265
}
@@ -706,6 +699,42 @@ mod tests {
706699
assert_eq!(*shutdown_called.lock().unwrap(), 1);
707700
}
708701

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+
709738
#[derive(Debug)]
710739
pub(crate) struct LazyLogProcessor {
711740
shutdown_called: Arc<Mutex<bool>>,

opentelemetry/src/logs/logger.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ pub trait LoggerProvider {
5757
/// Returns a new logger with the given name.
5858
///
5959
/// The `name` should be the application name or the name of the library
60-
/// providing instrumentation. If the name is empty, then an
61-
/// implementation-defined default name may be used instead.
60+
/// providing instrumentation.
6261
fn logger(&self, name: impl Into<Cow<'static, str>>) -> Self::Logger {
6362
let scope = InstrumentationScope::builder(name).build();
6463
self.logger_with_scope(scope)

0 commit comments

Comments
 (0)