#actix-web #actix-web-middleware #api-request #correlation #middleware #request-id #web-api

actix-web-correlation-id

An Actix-web middleware component which synchronises a correlation ID for cross API request logging

2 releases (1 stable)

1.0.0 Aug 1, 2023
0.1.0 Mar 10, 2021

#1048 in HTTP server

Download history 15/week @ 2024-07-22 24/week @ 2024-07-29 46/week @ 2024-08-05 18/week @ 2024-08-12 17/week @ 2024-08-19 97/week @ 2024-08-26 31/week @ 2024-09-02 64/week @ 2024-09-09 107/week @ 2024-09-16 45/week @ 2024-09-23 13/week @ 2024-09-30 23/week @ 2024-10-07 16/week @ 2024-10-21 32/week @ 2024-10-28 16/week @ 2024-11-04

65 downloads per month

BSD-2-Clause

27KB
516 lines

Documentation crates.io

actix-web-correlation-id

An Actix-web middleware component which synchronises a correlation ID for cross API request logging

Example:

use actix_web::{
    client::Client,
    middleware::Logger,
    web::{self},
    App, Error, HttpResponse, HttpServer,
};
use actix_web_correlation_id::{
    Correlation, CorrelationId, CorrelationIdPropagate, CorrelationIdVariable,
};

async fn index(corr_id: CorrelationId) -> Result<HttpResponse, Error> {
    let client = Client::new();

    let mut res = client
        .get("http://google.com/")
        .with_corr_id(corr_id)
        .send()
        .await?;

    let mut client_resp = HttpResponse::build(res.status());

    Ok(client_resp.body(res.body().await?))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

    HttpServer::new(move || {
        App::new()
            .wrap(
                Logger::new("%{corr-id}xi %a \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\" %T")
                    .add_corr_id(),
            )
            .wrap(
                Correlation::new()
                    .header_name("x-correlation-id")
                    .enforce_header(false)
                    .resp_header_name(Some("x-correlation-id"))
                    .include_in_resp(true),
            )
            .service(web::resource("/simple").route(web::post().to(index)))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

Dependencies

~15–26MB
~445K SLoC