Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs API/SDK - few perf improvements #1134

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions opentelemetry-api/src/global/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use once_cell::sync::Lazy;

use crate::{
logs::{Logger, LoggerProvider, NoopLoggerProvider},
KeyValue,
InstrumentationLibrary,
};

/// Allows a specific [`LoggerProvider`] to be used generically, by mirroring
Expand All @@ -21,12 +21,9 @@ pub trait ObjectSafeLoggerProvider {
///
/// [`Logger`]: crate::logs::Logger
/// [`LoggerProvider`]: crate::logs::LoggerProvider
fn versioned_logger_boxed(
fn boxed_logger(
&self,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
attributes: Option<Vec<KeyValue>>,
library: Arc<InstrumentationLibrary>,
include_trace_context: bool,
) -> Box<dyn Logger + Send + Sync + 'static>;
}
Expand All @@ -36,21 +33,12 @@ where
L: Logger + Send + Sync + 'static,
P: LoggerProvider<Logger = L>,
{
fn versioned_logger_boxed(
fn boxed_logger(
&self,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
attributes: Option<Vec<KeyValue>>,
library: Arc<InstrumentationLibrary>,
include_trace_context: bool,
) -> Box<dyn Logger + Send + Sync + 'static> {
Box::new(self.versioned_logger(
name,
version,
schema_url,
attributes,
include_trace_context,
))
Box::new(self.library_logger(library, include_trace_context))
}
}

Expand Down Expand Up @@ -96,21 +84,12 @@ impl GlobalLoggerProvider {
impl LoggerProvider for GlobalLoggerProvider {
type Logger = BoxedLogger;

fn versioned_logger(
fn library_logger(
&self,
name: impl Into<Cow<'static, str>>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
attributes: Option<Vec<KeyValue>>,
library: Arc<InstrumentationLibrary>,
include_trace_context: bool,
) -> Self::Logger {
BoxedLogger(self.provider.versioned_logger_boxed(
name.into(),
version,
schema_url,
attributes,
include_trace_context,
))
BoxedLogger(self.provider.boxed_logger(library, include_trace_context))
}
}

Expand Down
37 changes: 35 additions & 2 deletions opentelemetry-api/src/logs/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::{borrow::Cow, sync::Arc};

use crate::{logs::LogRecord, KeyValue};
use crate::{logs::LogRecord, InstrumentationLibrary, KeyValue};

/// The interface for emitting [`LogRecord`]s.
pub trait Logger {
Expand Down Expand Up @@ -39,6 +39,39 @@ pub trait LoggerProvider {
schema_url: Option<Cow<'static, str>>,
attributes: Option<Vec<KeyValue>>,
include_trace_context: bool,
) -> Self::Logger {
self.library_logger(
Arc::new(InstrumentationLibrary::new(
name, version, schema_url, attributes,
)),
include_trace_context,
)
}

/// Returns a new versioned logger with the given instrumentation library.
///
/// # Examples
///
/// ```
/// use opentelemetry_api::{global, InstrumentationLibrary, logs::LoggerProvider};
///
/// let provider = global::logger_provider();
///
/// // logger used in applications/binaries
/// let logger = provider.logger("my_app");
/// // logger used in libraries/crates that optionally includes version and schema url
/// let library = std::sync::Arc::new(InstrumentationLibrary::new(
/// env!("CARGO_PKG_NAME"),
/// Some(env!("CARGO_PKG_VERSION")),
/// Some("https://opentelemetry.io/schema/1.0.0"),
/// None,
/// ));
/// let logger = provider.library_logger(library, true);
/// ```
fn library_logger(
&self,
library: Arc<InstrumentationLibrary>,
include_trace_context: bool,
) -> Self::Logger;

/// Returns a new logger with the given name.
Expand Down
12 changes: 10 additions & 2 deletions opentelemetry-api/src/logs/noop.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use std::{borrow::Cow, sync::Arc};

use crate::{
logs::{LogRecord, Logger, LoggerProvider},
KeyValue,
InstrumentationLibrary, KeyValue,
};

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

fn library_logger(
&self,
_library: Arc<InstrumentationLibrary>,
_include_trace_context: bool,
) -> Self::Logger {
NoopLogger(())
}

fn versioned_logger(
&self,
_name: impl Into<Cow<'static, str>>,
Expand Down
24 changes: 18 additions & 6 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,24 @@ impl opentelemetry_api::logs::LoggerProvider for LoggerProvider {
name
};

Logger::new(
InstrumentationLibrary::new(component_name, version, schema_url, attributes),
Arc::downgrade(&self.inner),
self.library_logger(
Arc::new(InstrumentationLibrary::new(
component_name,
version,
schema_url,
attributes,
)),
include_trace_context,
)
}

fn library_logger(
&self,
library: Arc<InstrumentationLibrary>,
include_trace_context: bool,
) -> Self::Logger {
Logger::new(library, Arc::downgrade(&self.inner), include_trace_context)
}
}

impl LoggerProvider {
Expand Down Expand Up @@ -171,13 +183,13 @@ impl Builder {
/// [`LogRecord`]: opentelemetry_api::logs::LogRecord
pub struct Logger {
include_trace_context: bool,
instrumentation_lib: InstrumentationLibrary,
instrumentation_lib: Arc<InstrumentationLibrary>,
provider: Weak<LoggerProviderInner>,
}

impl Logger {
pub(crate) fn new(
instrumentation_lib: InstrumentationLibrary,
instrumentation_lib: Arc<InstrumentationLibrary>,
provider: Weak<LoggerProviderInner>,
include_trace_context: bool,
) -> Self {
Expand Down Expand Up @@ -220,7 +232,7 @@ impl opentelemetry_api::logs::Logger for Logger {
let data = LogData {
record,
resource: config.resource.clone(),
instrumentation: self.instrumentation_lib.clone(),
instrumentation: self.instrumentation_library().clone(),
};
processor.emit(data);
}
Expand Down