Skip to content

Commit 2ca024e

Browse files
authored
Export logs only if the level/logger/target is enabled. (#1147)
1 parent 8975aa3 commit 2ca024e

File tree

18 files changed

+197
-64
lines changed

18 files changed

+197
-64
lines changed

opentelemetry-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ trace = ["pin-project-lite"]
3131
metrics = []
3232
testing = ["trace"]
3333
logs = []
34+
logs_level_enabled = ["logs"]

opentelemetry-api/src/global/logs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ impl Logger for BoxedLogger {
5252
fn emit(&self, record: crate::logs::LogRecord) {
5353
self.0.emit(record)
5454
}
55+
56+
#[cfg(feature = "logs_level_enabled")]
57+
fn event_enabled(&self, level: crate::logs::Severity, target: &str) -> bool {
58+
self.0.event_enabled(level, target)
59+
}
5560
}
5661

5762
#[derive(Clone)]

opentelemetry-api/src/logs/logger.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ use std::{borrow::Cow, sync::Arc};
22

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

5+
#[cfg(feature = "logs_level_enabled")]
6+
use super::Severity;
7+
58
/// The interface for emitting [`LogRecord`]s.
9+
610
pub trait Logger {
711
/// Emit a [`LogRecord`]. If there is active current thread's [`Context`],
812
/// the logger will set the record's [`TraceContext`] to the active trace context,
913
///
1014
/// [`Context`]: crate::Context
1115
/// [`TraceContext`]: crate::logs::TraceContext
1216
fn emit(&self, record: LogRecord);
17+
18+
#[cfg(feature = "logs_level_enabled")]
19+
/// Check if the given log level is enabled.
20+
fn event_enabled(&self, level: Severity, target: &str) -> bool;
1321
}
1422

1523
/// Interfaces that can create [`Logger`] instances.

opentelemetry-api/src/logs/noop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ pub struct NoopLogger(());
4040

4141
impl Logger for NoopLogger {
4242
fn emit(&self, _record: LogRecord) {}
43+
#[cfg(feature = "logs_level_enabled")]
44+
fn event_enabled(&self, _level: super::Severity, _target: &str) -> bool {
45+
true
46+
}
4347
}

opentelemetry-appender-log/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ edition = "2021"
1313
[dependencies]
1414
opentelemetry_api = { version = "0.19", path = "../opentelemetry-api", features = ["logs"]}
1515
log = {version = "0.4.17", features = ["kv_unstable", "std"]}
16+
17+
[features]
18+
logs_level_enabled = ["opentelemetry_api/logs_level_enabled"]
19+
default = ["logs_level_enabled"]

opentelemetry-appender-log/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ where
1717
L: Logger + Send + Sync,
1818
{
1919
fn enabled(&self, _metadata: &Metadata) -> bool {
20-
// TODO: This should be dynamic instead of the current hardcoded value.
20+
#[cfg(feature = "logs_level_enabled")]
21+
return self.logger.event_enabled(
22+
map_severity_to_otel_severity(_metadata.level()),
23+
_metadata.target(),
24+
);
25+
#[cfg(not(feature = "logs_level_enabled"))]
2126
true
2227
}
2328

opentelemetry-appender-tracing/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ tracing-subscriber = { version = "0.3.0", default-features = false, features = [
1919
once_cell = "1.13.0"
2020

2121
[dev-dependencies]
22-
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] }
22+
opentelemetry-stdout = { path = "../opentelemetry-stdout", features = ["logs"] }
23+
24+
[features]
25+
logs_level_enabled = ["opentelemetry_api/logs_level_enabled", "opentelemetry_sdk/logs_level_enabled"]
26+
default = ["logs_level_enabled"]
27+

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ where
106106
event.record(&mut visitor);
107107
self.logger.emit(log_record);
108108
}
109+
110+
#[cfg(feature = "logs_level_enabled")]
111+
fn event_enabled(
112+
&self,
113+
_event: &tracing_core::Event<'_>,
114+
_ctx: tracing_subscriber::layer::Context<'_, S>,
115+
) -> bool {
116+
let severity = map_severity_to_otel_severity(_event.metadata().level().as_str());
117+
self.logger
118+
.event_enabled(severity, _event.metadata().target())
119+
}
109120
}
110121

111122
fn map_severity_to_otel_severity(level: &str) -> Severity {

opentelemetry-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ default = ["trace"]
4545
trace = ["opentelemetry_api/trace", "crossbeam-channel", "rand", "async-trait", "percent-encoding"]
4646
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url"]
4747
logs = ["opentelemetry_api/logs", "crossbeam-channel", "async-trait", "serde_json"]
48+
logs_level_enabled = ["logs", "opentelemetry_api/logs_level_enabled"]
4849
metrics = ["opentelemetry_api/metrics", "regex", "async-trait"]
4950
testing = ["opentelemetry_api/testing", "trace", "metrics", "logs", "rt-async-std", "rt-tokio", "rt-tokio-current-thread", "tokio/macros", "tokio/rt-multi-thread"]
5051
rt-tokio = ["tokio", "tokio-stream"]

opentelemetry-sdk/src/export/logs/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Log exporters
22
use crate::Resource;
33
use async_trait::async_trait;
4+
#[cfg(feature = "logs_level_enabled")]
5+
use opentelemetry_api::logs::Severity;
46
use opentelemetry_api::{
57
logs::{LogError, LogRecord, LogResult},
68
InstrumentationLibrary,
@@ -14,6 +16,11 @@ pub trait LogExporter: Send + Debug {
1416
async fn export(&mut self, batch: Vec<LogData>) -> LogResult<()>;
1517
/// Shuts down the expoter.
1618
fn shutdown(&mut self) {}
19+
#[cfg(feature = "logs_level_enabled")]
20+
/// Chek if logs are enabled.
21+
fn event_enabled(&self, _level: Severity, _target: &str, _name: &str) -> bool {
22+
true
23+
}
1724
}
1825

1926
/// `LogData` associates a [`LogRecord`] with a [`Resource`] and

0 commit comments

Comments
 (0)