servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Generated correlation and trusted route templates, never raw URLs or headers.
use super::RequestId;
use axum::{
    extract::{MatchedPath, Request},
    middleware::Next,
    response::Response,
};
use zlid::ZLID;

pub(super) fn request_id() -> zlid::Result<RequestId> {
    ZLID::random().map(|id| RequestId(id.text()))
}

pub(super) fn method(method: &axum::http::Method) -> &'static str {
    match method.as_str() {
        "GET" => "GET",
        "POST" => "POST",
        "PUT" => "PUT",
        "PATCH" => "PATCH",
        "DELETE" => "DELETE",
        "HEAD" => "HEAD",
        "OPTIONS" => "OPTIONS",
        "CONNECT" => "CONNECT",
        "TRACE" => "TRACE",
        "QUERY" => "QUERY",
        _ => "_OTHER",
    }
}
#[derive(Clone)]
pub(super) struct RequestSpan(pub(super) tracing::Span);

pub(super) async fn route(mut request: Request, next: Next) -> Response {
    if let Some(RequestSpan(span)) = request.extensions_mut().remove()
        && let Some(path) = request.extensions().get::<MatchedPath>()
    {
        span.record("route", path.as_str());
        #[cfg(feature = "otel")]
        {
            use opentelemetry::trace::TraceContextExt;
            use tracing_opentelemetry::OpenTelemetrySpanExt;
            span.record("http.route", path.as_str());
            let method = match method(request.method()) {
                "_OTHER" => "HTTP",
                method => method,
            };
            // The bridge starts on enter and ignores subsequent otel.name records.
            span.context()
                .span()
                .update_name(format!("{} {}", method, path.as_str()));
        }
    }
    next.run(request).await
}