Skip to content

Commit

Permalink
Export logs only if the level/logger/target is enabled. (#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Jul 29, 2023
1 parent 8975aa3 commit 2ca024e
Show file tree
Hide file tree
Showing 18 changed files with 197 additions and 64 deletions.
1 change: 1 addition & 0 deletions opentelemetry-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ trace = ["pin-project-lite"]
metrics = []
testing = ["trace"]
logs = []
logs_level_enabled = ["logs"]
5 changes: 5 additions & 0 deletions opentelemetry-api/src/global/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ impl Logger for BoxedLogger {
fn emit(&self, record: crate::logs::LogRecord) {
self.0.emit(record)
}

#[cfg(feature = "logs_level_enabled")]
fn event_enabled(&self, level: crate::logs::Severity, target: &str) -> bool {
self.0.event_enabled(level, target)
}
}

#[derive(Clone)]
Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-api/src/logs/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ use std::{borrow::Cow, sync::Arc};

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

#[cfg(feature = "logs_level_enabled")]
use super::Severity;

/// The interface for emitting [`LogRecord`]s.
pub trait Logger {
/// Emit a [`LogRecord`]. If there is active current thread's [`Context`],
/// the logger will set the record's [`TraceContext`] to the active trace context,
///
/// [`Context`]: crate::Context
/// [`TraceContext`]: crate::logs::TraceContext
fn emit(&self, record: LogRecord);

#[cfg(feature = "logs_level_enabled")]
/// Check if the given log level is enabled.
fn event_enabled(&self, level: Severity, target: &str) -> bool;
}

/// Interfaces that can create [`Logger`] instances.
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-api/src/logs/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ pub struct NoopLogger(());

impl Logger for NoopLogger {
fn emit(&self, _record: LogRecord) {}
#[cfg(feature = "logs_level_enabled")]
fn event_enabled(&self, _level: super::Severity, _target: &str) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions opentelemetry-appender-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ edition = "2021"
[dependencies]
opentelemetry_api = { version = "0.19", path = "../opentelemetry-api", features = ["logs"]}
log = {version = "0.4.17", features = ["kv_unstable", "std"]}

[features]
logs_level_enabled = ["opentelemetry_api/logs_level_enabled"]
default = ["logs_level_enabled"]
7 changes: 6 additions & 1 deletion opentelemetry-appender-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ where
L: Logger + Send + Sync,
{
fn enabled(&self, _metadata: &Metadata) -> bool {
// TODO: This should be dynamic instead of the current hardcoded value.
#[cfg(feature = "logs_level_enabled")]
return self.logger.event_enabled(
map_severity_to_otel_severity(_metadata.level()),
_metadata.target(),
);
#[cfg(not(feature = "logs_level_enabled"))]
true
}

Expand Down
7 changes: 6 additions & 1 deletion opentelemetry-appender-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ tracing-subscriber = { version = "0.3.0", default-features = false, features = [
once_cell = "1.13.0"

[dev-dependencies]
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] }
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] }

[features]
logs_level_enabled = ["opentelemetry_api/logs_level_enabled", "opentelemetry_sdk/logs_level_enabled"]
default = ["logs_level_enabled"]

11 changes: 11 additions & 0 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ where
event.record(&mut visitor);
self.logger.emit(log_record);
}

#[cfg(feature = "logs_level_enabled")]
fn event_enabled(
&self,
_event: &tracing_core::Event<'_>,
_ctx: tracing_subscriber::layer::Context<'_, S>,
) -> bool {
let severity = map_severity_to_otel_severity(_event.metadata().level().as_str());
self.logger
.event_enabled(severity, _event.metadata().target())
}
}

fn map_severity_to_otel_severity(level: &str) -> Severity {
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ default = ["trace"]
trace = ["opentelemetry_api/trace", "crossbeam-channel", "rand", "async-trait", "percent-encoding"]
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url"]
logs = ["opentelemetry_api/logs", "crossbeam-channel", "async-trait", "serde_json"]
logs_level_enabled = ["logs", "opentelemetry_api/logs_level_enabled"]
metrics = ["opentelemetry_api/metrics", "regex", "async-trait"]
testing = ["opentelemetry_api/testing", "trace", "metrics", "logs", "rt-async-std", "rt-tokio", "rt-tokio-current-thread", "tokio/macros", "tokio/rt-multi-thread"]
rt-tokio = ["tokio", "tokio-stream"]
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-sdk/src/export/logs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Log exporters
use crate::Resource;
use async_trait::async_trait;
#[cfg(feature = "logs_level_enabled")]
use opentelemetry_api::logs::Severity;
use opentelemetry_api::{
logs::{LogError, LogRecord, LogResult},
InstrumentationLibrary,
Expand All @@ -14,6 +16,11 @@ pub trait LogExporter: Send + Debug {
async fn export(&mut self, batch: Vec<LogData>) -> LogResult<()>;
/// Shuts down the expoter.
fn shutdown(&mut self) {}
#[cfg(feature = "logs_level_enabled")]
/// Chek if logs are enabled.
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
true
}
}

/// `LogData` associates a [`LogRecord`] with a [`Resource`] and
Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use opentelemetry_api::{
trace::TraceContextExt,
Context, InstrumentationLibrary,
};

#[cfg(feature = "logs_level_enabled")]
use opentelemetry_api::logs::Severity;

use std::{
borrow::Cow,
sync::{Arc, Weak},
Expand Down Expand Up @@ -225,4 +229,23 @@ impl opentelemetry_api::logs::Logger for Logger {
processor.emit(data);
}
}

#[cfg(feature = "logs_level_enabled")]
fn event_enabled(&self, level: Severity, target: &str) -> bool {
let provider = match self.provider() {
Some(provider) => provider,
None => return false,
};

let mut enabled = false;
for processor in provider.log_processors() {
enabled = enabled
|| processor.event_enabled(
level,
target,
self.instrumentation_library().name.as_ref(),
);
}
enabled
}
}
16 changes: 15 additions & 1 deletion opentelemetry-sdk/src/logs/log_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use futures_util::{
future::{self, Either},
{pin_mut, stream, StreamExt as _},
};
#[cfg(feature = "logs_level_enabled")]
use opentelemetry_api::logs::Severity;
use opentelemetry_api::{
global,
logs::{LogError, LogResult},
Expand All @@ -27,6 +29,9 @@ pub trait LogProcessor: Send + Sync + Debug {
fn force_flush(&self) -> LogResult<()>;
/// Shuts down the processor.
fn shutdown(&mut self) -> LogResult<()>;
#[cfg(feature = "logs_level_enabled")]
/// Check if logging is enabled
fn event_enabled(&self, level: Severity, target: &str, name: &str) -> bool;
}

/// A [`LogProcessor`] that exports synchronously when logs are emitted.
Expand Down Expand Up @@ -93,9 +98,13 @@ impl LogProcessor for SimpleLogProcessor {
)))
}
}

Ok(())
}

#[cfg(feature = "logs_level_enabled")]
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
true
}
}

/// A [`LogProcessor`] that asynchronously buffers log records and reports
Expand All @@ -121,6 +130,11 @@ impl<R: RuntimeChannel<BatchMessage>> LogProcessor for BatchLogProcessor<R> {
}
}

#[cfg(feature = "logs_level_enabled")]
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
true
}

fn force_flush(&self) -> LogResult<()> {
let (res_sender, res_receiver) = oneshot::channel();
self.message_sender
Expand Down
12 changes: 10 additions & 2 deletions opentelemetry-user-events-logs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ keywords = ["opentelemetry", "log", "trace", "user_events"]
license = "Apache-2.0"

[dependencies]
eventheader = "0.2"
eventheader_dynamic = "0.2"
eventheader = "0.3.2"
eventheader_dynamic = "0.3.2"
opentelemetry_api = { version = "0.19", path = "../opentelemetry-api", features = ["logs"] }
opentelemetry_sdk = { version = "0.19", path = "../opentelemetry-sdk", features = ["logs"] }
async-std = { version="1.6" }
Expand All @@ -24,4 +24,12 @@ opentelemetry-appender-tracing = { path = "../opentelemetry-appender-tracing" }
tracing = { version = "0.1.37", default-features = false, features = ["std"] }
tracing-core = "0.1.31"
tracing-subscriber = { version = "0.3.0", default-features = false, features = ["registry", "std"] }
microbench = "0.5"

[features]
logs_level_enabled = ["opentelemetry_api/logs_level_enabled", "opentelemetry_sdk/logs_level_enabled"]
default=["logs_level_enabled"]

[[example]]
name = "basic"
path = "examples/basic.rs"
6 changes: 5 additions & 1 deletion opentelemetry-user-events-logs/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
use opentelemetry_appender_tracing::layer;
use opentelemetry_sdk::logs::LoggerProvider;
use opentelemetry_user_events_logs::{ExporterConfig, ReentrantLogProcessor};
use std::collections::HashMap;
use tracing::error;
use tracing_subscriber::prelude::*;

fn init_logger() -> LoggerProvider {
let exporter_config = ExporterConfig { keyword: 1 };
let exporter_config = ExporterConfig {
default_keyword: 1,
keywords_map: HashMap::new(),
};
let reenterant_processor = ReentrantLogProcessor::new("test", None, exporter_config);
LoggerProvider::builder()
.with_log_processor(reenterant_processor)
Expand Down
Loading

0 comments on commit 2ca024e

Please sign in to comment.