Skip to content

Commit 309b4e5

Browse files
authored
Logs API/SDK - few perf improvements (#1134)
* initial commit * fix unit test * fix doctest * fix doctest
1 parent 2baa379 commit 309b4e5

File tree

4 files changed

+72
-40
lines changed

4 files changed

+72
-40
lines changed

opentelemetry-api/src/global/logs.rs

+9-30
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use once_cell::sync::Lazy;
88

99
use crate::{
1010
logs::{Logger, LoggerProvider, NoopLoggerProvider},
11-
KeyValue,
11+
InstrumentationLibrary,
1212
};
1313

1414
/// Allows a specific [`LoggerProvider`] to be used generically, by mirroring
@@ -21,12 +21,9 @@ pub trait ObjectSafeLoggerProvider {
2121
///
2222
/// [`Logger`]: crate::logs::Logger
2323
/// [`LoggerProvider`]: crate::logs::LoggerProvider
24-
fn versioned_logger_boxed(
24+
fn boxed_logger(
2525
&self,
26-
name: Cow<'static, str>,
27-
version: Option<Cow<'static, str>>,
28-
schema_url: Option<Cow<'static, str>>,
29-
attributes: Option<Vec<KeyValue>>,
26+
library: Arc<InstrumentationLibrary>,
3027
include_trace_context: bool,
3128
) -> Box<dyn Logger + Send + Sync + 'static>;
3229
}
@@ -36,21 +33,12 @@ where
3633
L: Logger + Send + Sync + 'static,
3734
P: LoggerProvider<Logger = L>,
3835
{
39-
fn versioned_logger_boxed(
36+
fn boxed_logger(
4037
&self,
41-
name: Cow<'static, str>,
42-
version: Option<Cow<'static, str>>,
43-
schema_url: Option<Cow<'static, str>>,
44-
attributes: Option<Vec<KeyValue>>,
38+
library: Arc<InstrumentationLibrary>,
4539
include_trace_context: bool,
4640
) -> Box<dyn Logger + Send + Sync + 'static> {
47-
Box::new(self.versioned_logger(
48-
name,
49-
version,
50-
schema_url,
51-
attributes,
52-
include_trace_context,
53-
))
41+
Box::new(self.library_logger(library, include_trace_context))
5442
}
5543
}
5644

@@ -96,21 +84,12 @@ impl GlobalLoggerProvider {
9684
impl LoggerProvider for GlobalLoggerProvider {
9785
type Logger = BoxedLogger;
9886

99-
fn versioned_logger(
87+
fn library_logger(
10088
&self,
101-
name: impl Into<Cow<'static, str>>,
102-
version: Option<Cow<'static, str>>,
103-
schema_url: Option<Cow<'static, str>>,
104-
attributes: Option<Vec<KeyValue>>,
89+
library: Arc<InstrumentationLibrary>,
10590
include_trace_context: bool,
10691
) -> Self::Logger {
107-
BoxedLogger(self.provider.versioned_logger_boxed(
108-
name.into(),
109-
version,
110-
schema_url,
111-
attributes,
112-
include_trace_context,
113-
))
92+
BoxedLogger(self.provider.boxed_logger(library, include_trace_context))
11493
}
11594
}
11695

opentelemetry-api/src/logs/logger.rs

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

3-
use crate::{logs::LogRecord, KeyValue};
3+
use crate::{logs::LogRecord, InstrumentationLibrary, KeyValue};
44

55
/// The interface for emitting [`LogRecord`]s.
66
pub trait Logger {
@@ -39,6 +39,39 @@ pub trait LoggerProvider {
3939
schema_url: Option<Cow<'static, str>>,
4040
attributes: Option<Vec<KeyValue>>,
4141
include_trace_context: bool,
42+
) -> Self::Logger {
43+
self.library_logger(
44+
Arc::new(InstrumentationLibrary::new(
45+
name, version, schema_url, attributes,
46+
)),
47+
include_trace_context,
48+
)
49+
}
50+
51+
/// Returns a new versioned logger with the given instrumentation library.
52+
///
53+
/// # Examples
54+
///
55+
/// ```
56+
/// use opentelemetry_api::{global, InstrumentationLibrary, logs::LoggerProvider};
57+
///
58+
/// let provider = global::logger_provider();
59+
///
60+
/// // logger used in applications/binaries
61+
/// let logger = provider.logger("my_app");
62+
/// // logger used in libraries/crates that optionally includes version and schema url
63+
/// let library = std::sync::Arc::new(InstrumentationLibrary::new(
64+
/// env!("CARGO_PKG_NAME"),
65+
/// Some(env!("CARGO_PKG_VERSION")),
66+
/// Some("https://opentelemetry.io/schema/1.0.0"),
67+
/// None,
68+
/// ));
69+
/// let logger = provider.library_logger(library, true);
70+
/// ```
71+
fn library_logger(
72+
&self,
73+
library: Arc<InstrumentationLibrary>,
74+
include_trace_context: bool,
4275
) -> Self::Logger;
4376

4477
/// Returns a new logger with the given name.

opentelemetry-api/src/logs/noop.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::borrow::Cow;
1+
use std::{borrow::Cow, sync::Arc};
22

33
use crate::{
44
logs::{LogRecord, Logger, LoggerProvider},
5-
KeyValue,
5+
InstrumentationLibrary, KeyValue,
66
};
77

88
/// A no-op implementation of a [`LoggerProvider`].
@@ -19,6 +19,14 @@ impl NoopLoggerProvider {
1919
impl LoggerProvider for NoopLoggerProvider {
2020
type Logger = NoopLogger;
2121

22+
fn library_logger(
23+
&self,
24+
_library: Arc<InstrumentationLibrary>,
25+
_include_trace_context: bool,
26+
) -> Self::Logger {
27+
NoopLogger(())
28+
}
29+
2230
fn versioned_logger(
2331
&self,
2432
_name: impl Into<Cow<'static, str>>,

opentelemetry-sdk/src/logs/log_emitter.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,24 @@ impl opentelemetry_api::logs::LoggerProvider for LoggerProvider {
4343
name
4444
};
4545

46-
Logger::new(
47-
InstrumentationLibrary::new(component_name, version, schema_url, attributes),
48-
Arc::downgrade(&self.inner),
46+
self.library_logger(
47+
Arc::new(InstrumentationLibrary::new(
48+
component_name,
49+
version,
50+
schema_url,
51+
attributes,
52+
)),
4953
include_trace_context,
5054
)
5155
}
56+
57+
fn library_logger(
58+
&self,
59+
library: Arc<InstrumentationLibrary>,
60+
include_trace_context: bool,
61+
) -> Self::Logger {
62+
Logger::new(library, Arc::downgrade(&self.inner), include_trace_context)
63+
}
5264
}
5365

5466
impl LoggerProvider {
@@ -171,13 +183,13 @@ impl Builder {
171183
/// [`LogRecord`]: opentelemetry_api::logs::LogRecord
172184
pub struct Logger {
173185
include_trace_context: bool,
174-
instrumentation_lib: InstrumentationLibrary,
186+
instrumentation_lib: Arc<InstrumentationLibrary>,
175187
provider: Weak<LoggerProviderInner>,
176188
}
177189

178190
impl Logger {
179191
pub(crate) fn new(
180-
instrumentation_lib: InstrumentationLibrary,
192+
instrumentation_lib: Arc<InstrumentationLibrary>,
181193
provider: Weak<LoggerProviderInner>,
182194
include_trace_context: bool,
183195
) -> Self {
@@ -223,7 +235,7 @@ impl opentelemetry_api::logs::Logger for Logger {
223235
let data = LogData {
224236
record,
225237
resource: config.resource.clone(),
226-
instrumentation: self.instrumentation_lib.clone(),
238+
instrumentation: self.instrumentation_library().clone(),
227239
};
228240
processor.emit(data);
229241
}

0 commit comments

Comments
 (0)