hotpath-meta 0.28.0

Hotpath meta - a version of hotpath used to profile the profiler itself. Not intended for external use.
Documentation
//! Endpoint normalization: merges parameter-varied requests to the same route
//! into one bucket by collapsing identifier-like path segments into `{id}`.
//!
//! Input is the raw `METHOD host[:port]/path` pre-key built by the middleware
//! (query string, fragment, and credentials are already absent). The method
//! and host are left untouched, so numeric hosts like `127.0.0.1` survive; an
//! explicit port becomes `{port}`, since a port that is not the scheme's
//! default is most often a test server bound to a free one, and two runs
//! must key the same endpoint the same way to be comparable.
//!
//! A segment is treated as an identifier when it is:
//! - all decimal digits (`/users/123`)
//! - a UUID (`/jobs/550e8400-e29b-41d4-a716-446655440000`)
//! - a hex string of 16+ chars (`/blobs/deadbeefdeadbeef`)

use regex_lite::Regex;
use std::sync::LazyLock;

static UUID_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
        .unwrap()
});

static HEX_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[0-9a-fA-F]{16,}$").unwrap());

fn is_id_segment(segment: &str) -> bool {
    !segment.is_empty()
        && (segment.bytes().all(|b| b.is_ascii_digit())
            || UUID_RE.is_match(segment)
            || HEX_RE.is_match(segment))
}

/// Normalize a raw `METHOD host[:port]/path` pre-key into a stable bucket key.
pub(crate) fn normalize_endpoint(endpoint: &str) -> String {
    let Some(slash) = endpoint.find('/') else {
        return normalize_port(endpoint);
    };
    let (prefix, path) = endpoint.split_at(slash);
    let prefix = normalize_port(prefix);
    let normalized_path = path
        .split('/')
        .map(|segment| {
            if is_id_segment(segment) {
                "{id}"
            } else {
                segment
            }
        })
        .collect::<Vec<_>>()
        .join("/");
    format!("{prefix}{normalized_path}")
}

/// `METHOD host:1234` -> `METHOD host:{port}`; a prefix without a port, or
/// whose last `:` is not followed by digits (an IPv6 literal's last group),
/// is returned as is.
fn normalize_port(prefix: &str) -> String {
    match prefix.rsplit_once(':') {
        Some((host, port)) if !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()) => {
            format!("{host}:{{port}}")
        }
        _ => prefix.to_string(),
    }
}