init

Function init 

Source
pub async fn init(
    log_filter: &str,
    log_format: LogFormat,
    tracing_url: Option<&str>,
    trace_ratio: f64,
) -> Result<(), Error>
Expand description

Initializes logging and tracing subsystems.

This asynchronous function configures and initializes logging and tracing according to the provided format and filtering parameters. It supports both JSON and plain-text log formats, as well as OpenTelemetry tracing when a tracing URL is specified. If OpenTelemetry is enabled, traces are sent to the given URL using OTLP over gRPC.

§Example


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing with a JSON log format and a log filter of "info".
    let opentelemetry_endpoint_url = std::env::var("OPENTELEMETRY_ENDPOINT_URL").ok();
    init("info", LogFormat::Text, opentelemetry_endpoint_url.as_deref(), 0.1)
        .await?;

    // Application logic here...

    Ok(())
}

In this example, the logging system is initialized with a plain-text format, an info log level filter, and tracing disabled (as None is passed for the some_tracing_url).

§OpenTelemetry Integration

When a tracing URL is provided, OpenTelemetry tracing is configured using OTLP (OpenTelemetry Protocol) over gRPC. The function creates a tracing pipeline with a ratio-based trace sampler and a default random trace ID generator. Traces will be sampled based on the trace_ratio provided.

The function sets a global tracing subscriber using the combination of the tracing_subscriber logger and optionally the OpenTelemetry tracer if enabled.

If the tracing subsystem is successfully configured, the function returns Ok(()), otherwise an appropriate error is returned.