Skip to main content

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. JSON output uses newline-delimited JSON (NDJSON): every tracing event is one complete JSON object on one physical output line. It also supports OpenTelemetry tracing when a tracing URL is specified. If OpenTelemetry is enabled, traces are sent to the given URL using OTLP over HTTP.

§Example


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let opentelemetry_endpoint_url = std::env::var("OPENTELEMETRY_ENDPOINT_URL").ok();
    init("info", LogFormat::Text, opentelemetry_endpoint_url.as_deref(), 0.1)
        .await?;

    Ok(())
}

§OpenTelemetry Integration

When a tracing URL is provided, OpenTelemetry tracing is configured using OTLP. 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.