Skip to content

Commit 1b96b67

Browse files
Document EnrichmentLogProcessor more comprehensively open-telemetry#3266 (comment)
1 parent 00c0897 commit 1b96b67

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

examples/logs-advanced/src/main.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,42 @@ fn main() {
5757
let _ = provider.shutdown();
5858
}
5959

60+
/// A log processor that enriches log records with additional attributes before
61+
/// delegating to an underlying processor.
62+
///
63+
/// # Why Wrapping Instead of Chaining?
64+
///
65+
/// If this were implemented as a standalone processor in a chain (e.g.,
66+
/// EnrichmentProcessor -> SimpleLogProcessor), the performance benefits of the
67+
/// `event_enabled` check would be nullified. Here's why:
68+
///
69+
/// - The `event_enabled` method is crucial for performance - it allows processors
70+
/// to skip expensive operations for logs that will ultimately be filtered out
71+
/// - A standalone EnrichmentProcessor would need to implement `event_enabled`,
72+
/// but it has no knowledge of downstream filtering logic
73+
/// - It would have to return `true` by default, causing unnecessary enrichment
74+
/// work even for logs that the downstream processor will discard
75+
///
76+
/// By wrapping the underlying processor, we ensure that:
77+
///
78+
/// 1. **Performance**: The wrapped processor makes the authoritative decision
79+
/// about whether a log should be processed via `event_enabled`
80+
/// 2. **Correctness**: All filtering logic from the underlying processor is
81+
/// properly honored without duplication
82+
/// 3. **Transparency**: The enrichment is applied only when logs will actually
83+
/// be processed, avoiding waste
84+
///
85+
/// ## Delegation Requirements
86+
///
87+
/// Because this processor wraps another, it **must delegate all trait methods**
88+
/// to the underlying processor, including:
89+
/// - `shutdown_with_timeout` / `shutdown` - For proper resource cleanup
90+
/// - `set_resource` - To propagate resource information
91+
/// - `force_flush` - To ensure data consistency
92+
/// - `event_enabled` - To maintain filtering logic
93+
///
94+
/// This ensures the underlying processor receives all necessary lifecycle events
95+
/// and configuration updates.
6096
#[derive(Debug)]
6197
pub struct EnrichmentLogProcessor<P: LogProcessor> {
6298
/// The wrapped processor that will receive enriched log records
@@ -93,11 +129,6 @@ impl<P: LogProcessor> LogProcessor for EnrichmentLogProcessor<P> {
93129

94130
#[cfg(feature = "spec_unstable_logs_enabled")]
95131
fn event_enabled(&self, level: Severity, target: &str, name: Option<&str>) -> bool {
96-
// It is important to call the delegate's event_enabled method to ensure that
97-
// any filtering or logic implemented by downstream processors is respected so
98-
// that no unnecessary work is done, causing an unwanted performance issue.
99-
// Skipping this call could result in logs being emitted that should have been filtered out
100-
// or in bypassing other custom logic in the processor chain.
101132
self.delegate.event_enabled(level, target, name)
102133
}
103134
}

0 commit comments

Comments
 (0)