signal-kit 0.2.3

Helpers for initializing OpenTelemetry-based observability (tracing, metrics, logs) in async Rust services.
Documentation
use opentelemetry_sdk::logs::SdkLoggerProvider;

use crate::traces::otel_resource_attributes::*;

/// Initialize the logging provider for the given service.
///
/// # Arguments
///
/// * `service_name` - The name of the service to initialize the logging
///   provider for.
///
/// # Returns
/// Bridges an SDK 0.31 gap: `BatchLogProcessor` (async-runtime flavour)
/// implements `shutdown` but not `shutdown_with_timeout`, whose trait default
/// is a no-op — so provider shutdowns would silently skip flushing queued
/// records. Forwarding the timeout variant to `shutdown` restores the drain.
/// `set_resource` must be forwarded too: its trait default is a no-op, which
/// would leave the async-runtime processor on the exporter's fallback resource
/// (`service.name=unknown_service`) instead of the caller's resource.
#[derive(Debug)]
struct ShutdownFlushingLogProcessor {
    inner: opentelemetry_sdk::logs::log_processor_with_async_runtime::BatchLogProcessor<
        crate::otel_runtime::SignalKitOtelRuntime,
    >,
}

impl opentelemetry_sdk::logs::LogProcessor for ShutdownFlushingLogProcessor {
    fn emit(
        &self,
        data: &mut opentelemetry_sdk::logs::SdkLogRecord,
        instrumentation: &opentelemetry::InstrumentationScope,
    ) {
        self.inner.emit(data, instrumentation);
    }
    fn force_flush(&self) -> opentelemetry_sdk::error::OTelSdkResult {
        self.inner.force_flush()
    }
    fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
        self.inner.set_resource(resource);
    }
    fn shutdown_with_timeout(
        &self,
        _timeout: std::time::Duration,
    ) -> opentelemetry_sdk::error::OTelSdkResult {
        self.inner.shutdown()
    }
}

/// Initialize the logging provider for the given service.
///
/// # Arguments
///
/// * `service_name` - The name of the service to initialize the logging
///   provider for.
/// * `additional_attributes` - The caller's resource attributes, merged into
///   the log resource exactly as they are into the trace resource.
/// * `otel_runtime` - The runtime driving the batch log processor.
pub(crate) fn init_logging_provider(
    service_name: &str,
    additional_attributes: &[opentelemetry::KeyValue],
    otel_runtime: &crate::otel_runtime::SignalKitOtelRuntime,
) -> SdkLoggerProvider {
    #[cfg_attr(not(feature = "logs-otlp"), allow(unused_mut))]
    let mut builder = SdkLoggerProvider::builder().with_resource(build_resource(
        service_name,
        additional_attributes.iter().cloned(),
    ));

    #[cfg(all(
        feature = "logs-otlp",
        any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json")
    ))]
    // Check if OTLP endpoint is configured for logs
    if crate::get_otlp_endpoint("logs").is_some() {
        let exporter =
            crate::transport::log_exporter::build(crate::resolve_otlp_transport("logs"));
        let batch_processor =
            opentelemetry_sdk::logs::log_processor_with_async_runtime::BatchLogProcessor::builder(
                exporter,
                otel_runtime.clone(),
            )
            .build();
        let flushing = ShutdownFlushingLogProcessor {
            inner: batch_processor,
        };
        builder = builder.with_log_processor(flushing);
    }

    builder.build()
}