Skip to content

Commit 0f3545a

Browse files
Remove unnecessary debug print
1 parent 565d640 commit 0f3545a

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

examples/logs-enrichment/README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
# OpenTelemetry Log Appender for tracing - Example
1+
# OpenTelemetry Log Processor Implemenation and Composition - Example
22

3-
This example shows how to use the opentelemetry-appender-tracing crate, which is a
4-
[logging
5-
appender](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/glossary.md#log-appender--bridge)
6-
that bridges logs from the [tracing crate](https://tracing.rs/tracing/#events) to
7-
OpenTelemetry. The example setups a LoggerProvider with stdout exporter, so logs
8-
are emitted to stdout.
3+
This example builds on top of the `logs-basic`, showing how to implement and compose
4+
`LogProcessor`s correctly.
95

10-
## Usage
6+
The `FilteringLogProcessor` applies a severity-based filtering. The main purpose is
7+
mostly to serve as a way to show how to compose processors by wrapping them into
8+
each other, in a way akin to the [delegation pattern](https://en.wikipedia.org/wiki/Delegation_pattern).
9+
10+
The `SlowEnrichmentProcessor` simulates a (very!) slow processor adding information
11+
to the log captured by the OpenTelemetry SDK, which correctly ensures that the
12+
downstream processor's filtering is captured, avoiding unnecessary work.
1113

12-
Run the following, and Logs emitted using [tracing](https://docs.rs/tracing/latest/tracing/)
13-
will be written out to stdout.
14+
## Usage
1415

1516
```shell
16-
cargo run
17+
cargo run --features="spec_unstable_logs_enabled"
1718
```
19+
20+
Notice:
21+
1. only the error log is enriched with the `enriched: true` attribute
22+
2. the slow enrichment process only happens for the error log, without unnecessary work
23+
24+
## Extra credit
25+
26+
Tweak the `SlowEnrichmentProcessor` by removing the implementation of `event_enabled`.
27+
The default implementation always accepts the event from the wrapped processor, even
28+
when it's set up to ignore a specific event. As a consequence, the slow enrichment
29+
processor will act on every log record, regardless of whether they are ultimately
30+
ignored. As a consequence, the filtering happening upstream will not be respected,
31+
causing info logs being enriched (with the resulting unnecessary work).

examples/logs-enrichment/src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use opentelemetry_sdk::Resource;
66
use tracing::{info, error};
77
use tracing_subscriber::{prelude::*, EnvFilter};
88
use opentelemetry::InstrumentationScope;
9-
use opentelemetry::logs::Severity;
9+
use opentelemetry::logs::{Severity, LogRecord};
1010
use opentelemetry_sdk::error::OTelSdkResult;
1111

1212
fn main() {
@@ -90,7 +90,6 @@ impl<P: LogProcessor> LogProcessor for FilteringLogProcessor<P> {
9090

9191
#[cfg(feature = "spec_unstable_logs_enabled")]
9292
fn event_enabled(&self, level: Severity, target: &str, name: Option<&str>) -> bool {
93-
println!("filtering: severity is {level:?}");
9493
self.delegate.event_enabled(level, target, name) && level >= self.min_severity
9594
}
9695
}
@@ -111,8 +110,12 @@ impl<P: LogProcessor> SlowEnrichmentLogProcessor<P> {
111110

112111
impl<P: LogProcessor> LogProcessor for SlowEnrichmentLogProcessor<P> {
113112
fn emit(&self, data: &mut SdkLogRecord, instrumentation: &InstrumentationScope) {
114-
println!("slow enrichment log processor is waiting 1 s");
113+
// Simulate an expensive enrichment step (e.g., fetching from a DB or service)
115114
sleep(Duration::from_secs(1));
115+
// Enrich the log record with a custom attribute using the public API
116+
data.add_attribute("enriched", true);
117+
// You could also add more context, e.g., thread id, timestamp, etc.
118+
// data.add_attribute("thread_id", format!("{:?}", std::thread::current().id()));
116119
self.delegate.emit(data, instrumentation);
117120
}
118121

@@ -122,7 +125,10 @@ impl<P: LogProcessor> LogProcessor for SlowEnrichmentLogProcessor<P> {
122125

123126
#[cfg(feature = "spec_unstable_logs_enabled")]
124127
fn event_enabled(&self, level: Severity, target: &str, name: Option<&str>) -> bool {
125-
println!("slow enrichment: severity is {level:?}");
128+
// It is important to call the delegate's event_enabled method to ensure that
129+
// any filtering or logic implemented by downstream processors is respected.
130+
// Skipping this call could result in logs being emitted that should have been filtered out
131+
// or in bypassing other custom logic in the processor chain.
126132
self.delegate.event_enabled(level, target, name)
127133
}
128134
}

0 commit comments

Comments
 (0)