|
| 1 | +use opentelemetry::logs::{LogRecord, Severity}; |
| 2 | +use opentelemetry::InstrumentationScope; |
| 3 | +use opentelemetry_appender_tracing::layer; |
| 4 | +use opentelemetry_sdk::error::OTelSdkResult; |
| 5 | +use opentelemetry_sdk::logs::{LogProcessor, SdkLogRecord, SdkLoggerProvider, SimpleLogProcessor}; |
| 6 | +use opentelemetry_sdk::Resource; |
| 7 | +use tracing::{error, info}; |
| 8 | +use tracing_subscriber::{prelude::*, EnvFilter}; |
| 9 | + |
| 10 | +fn main() { |
| 11 | + let exporter = opentelemetry_stdout::LogExporter::default(); |
| 12 | + let enriching_processor = EnrichmentLogProcessor::new(SimpleLogProcessor::new(exporter)); |
| 13 | + let provider: SdkLoggerProvider = SdkLoggerProvider::builder() |
| 14 | + .with_resource( |
| 15 | + Resource::builder() |
| 16 | + .with_service_name("log-appender-tracing-example") |
| 17 | + .build(), |
| 18 | + ) |
| 19 | + .with_log_processor(enriching_processor) |
| 20 | + .build(); |
| 21 | + |
| 22 | + // To prevent a telemetry-induced-telemetry loop, OpenTelemetry's own internal |
| 23 | + // logging is properly suppressed. However, logs emitted by external components |
| 24 | + // (such as reqwest, tonic, etc.) are not suppressed as they do not propagate |
| 25 | + // OpenTelemetry context. Until this issue is addressed |
| 26 | + // (https://github.com/open-telemetry/opentelemetry-rust/issues/2877), |
| 27 | + // filtering like this is the best way to suppress such logs. |
| 28 | + // |
| 29 | + // The filter levels are set as follows: |
| 30 | + // - Allow `info` level and above by default. |
| 31 | + // - Completely restrict logs from `hyper`, `tonic`, `h2`, and `reqwest`. |
| 32 | + // |
| 33 | + // Note: This filtering will also drop logs from these components even when |
| 34 | + // they are used outside of the OTLP Exporter. |
| 35 | + let filter_otel = EnvFilter::new("info") |
| 36 | + .add_directive("hyper=off".parse().unwrap()) |
| 37 | + .add_directive("tonic=off".parse().unwrap()) |
| 38 | + .add_directive("h2=off".parse().unwrap()) |
| 39 | + .add_directive("reqwest=off".parse().unwrap()); |
| 40 | + let otel_layer = layer::OpenTelemetryTracingBridge::new(&provider).with_filter(filter_otel); |
| 41 | + |
| 42 | + // Create a new tracing::Fmt layer to print the logs to stdout. It has a |
| 43 | + // default filter of `info` level and above, and `debug` and above for logs |
| 44 | + // from OpenTelemetry crates. The filter levels can be customized as needed. |
| 45 | + let filter_fmt = EnvFilter::new("error").add_directive("opentelemetry=debug".parse().unwrap()); |
| 46 | + let fmt_layer = tracing_subscriber::fmt::layer() |
| 47 | + .with_thread_names(true) |
| 48 | + .with_filter(filter_fmt); |
| 49 | + |
| 50 | + tracing_subscriber::registry() |
| 51 | + .with(otel_layer) |
| 52 | + .with(fmt_layer) |
| 53 | + .init(); |
| 54 | + |
| 55 | + info!(name : "my-event-name", target : "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]", message = "This is an example message"); |
| 56 | + error!(name : "my-event-name", target : "my-system", event_id = 50, user_name = "otel", user_email = "[email protected]", message = "This is an example message"); |
| 57 | + let _ = provider.shutdown(); |
| 58 | +} |
| 59 | + |
| 60 | +/// A log processor that enriches log records with additional attributes before |
| 61 | +/// delegating to an underlying processor. |
| 62 | +/// |
| 63 | +/// If this were implemented as a standalone processor in a chain (e.g., |
| 64 | +/// EnrichmentProcessor -> SimpleLogProcessor), the performance benefits of the |
| 65 | +/// `event_enabled` check would be nullified. Here's why: |
| 66 | +/// |
| 67 | +/// - The `event_enabled` method is crucial for performance - it allows processors |
| 68 | +/// to skip expensive operations for logs that will ultimately be filtered out |
| 69 | +/// - A standalone EnrichmentProcessor would need to implement `event_enabled`, |
| 70 | +/// but it has no knowledge of downstream filtering logic |
| 71 | +/// - It would have to return `true` by default, causing unnecessary enrichment |
| 72 | +/// work even for logs that the downstream processor will discard |
| 73 | +/// |
| 74 | +/// Because this processor wraps another, it must delegate all trait methods |
| 75 | +/// to the underlying processor. This ensures the underlying processor receives |
| 76 | +/// all necessary lifecycle events. |
| 77 | +#[derive(Debug)] |
| 78 | +pub struct EnrichmentLogProcessor<P: LogProcessor> { |
| 79 | + /// The wrapped processor that will receive enriched log records |
| 80 | + delegate: P, |
| 81 | +} |
| 82 | + |
| 83 | +impl<P: LogProcessor> EnrichmentLogProcessor<P> { |
| 84 | + pub fn new(delegate: P) -> EnrichmentLogProcessor<P> { |
| 85 | + EnrichmentLogProcessor { delegate } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<P: LogProcessor> LogProcessor for EnrichmentLogProcessor<P> { |
| 90 | + fn emit(&self, data: &mut SdkLogRecord, instrumentation: &InstrumentationScope) { |
| 91 | + data.add_attribute("enriched", true); |
| 92 | + self.delegate.emit(data, instrumentation); |
| 93 | + } |
| 94 | + |
| 95 | + fn force_flush(&self) -> OTelSdkResult { |
| 96 | + self.delegate.force_flush() |
| 97 | + } |
| 98 | + |
| 99 | + fn shutdown_with_timeout(&self, timeout: std::time::Duration) -> OTelSdkResult { |
| 100 | + self.delegate.shutdown_with_timeout(timeout) |
| 101 | + } |
| 102 | + |
| 103 | + fn shutdown(&self) -> OTelSdkResult { |
| 104 | + self.delegate.shutdown() |
| 105 | + } |
| 106 | + |
| 107 | + fn set_resource(&mut self, resource: &Resource) { |
| 108 | + self.delegate.set_resource(resource); |
| 109 | + } |
| 110 | + |
| 111 | + fn event_enabled(&self, level: Severity, target: &str, name: Option<&str>) -> bool { |
| 112 | + self.delegate.event_enabled(level, target, name) |
| 113 | + } |
| 114 | +} |
0 commit comments