#http-header #web-server #axum-server #axum

axum-server-timing

An axum layer to inject the Server-Timing HTTP header into the response

8 releases (4 stable)

3.0.0 Jun 11, 2025
2.0.0 Jan 1, 2025
1.0.1 Aug 19, 2024
1.0.0 Jul 23, 2024
0.1.0 Apr 19, 2023

#194 in HTTP server

Download history 650/week @ 2025-08-08 646/week @ 2025-08-15 836/week @ 2025-08-22 777/week @ 2025-08-29 2029/week @ 2025-09-05 3031/week @ 2025-09-12 2091/week @ 2025-09-19 2355/week @ 2025-09-26 2336/week @ 2025-10-03 2360/week @ 2025-10-10 2767/week @ 2025-10-17 2759/week @ 2025-10-24 2024/week @ 2025-10-31 1937/week @ 2025-11-07 2389/week @ 2025-11-14 848/week @ 2025-11-21

7,626 downloads per month

MIT/Apache

21KB
264 lines

axum-server-timing

Latest Version

An axum layer to inject the Server-Timing HTTP header into the response.

For a reference on the header please see developer.mozilla.org.

Examples

Using for Request Timing

Using the layer to inject the Server-Timing Header.

    let app = Router::new()
        .route("/", get(handler))
        .layer(axum_server_timing::ServerTimingLayer::new("HelloService"));

Output

HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 22
server-timing: HelloService;dur=102
date: Wed, 19 Apr 2023 15:25:40 GMT

<h1>Hello, World!</h1>

Using for Request Timing with Description

Using the layer to inject the Server-Timing Header with description.

    let app = Router::new()
        .route("/", get(handler))
        .layer(
        axum_server_timing::ServerTimingLayer::new("HelloService")
            .with_description("whatever")
        );

Output

HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 22
server-timing: HelloService;desc="whatever";dur=102
date: Wed, 19 Apr 2023 15:25:40 GMT

<h1>Hello, World!</h1>

Recording exection Steps

use axum::{response::Html, routing::get, Extension, Router};
use axum_server_timing::ServerTimingExtension;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler)).layer(
        axum_server_timing::ServerTimingLayer::new("HelloService").with_description("whatever"),
    );

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    println!("listening on 0.0.0.0:3000");
    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();
}

async fn handler(Extension(timing): Extension<ServerTimingExtension>) -> Html<&'static str> {
    // intentional sleep, so the duration does not report 0
    tokio::time::sleep(Duration::from_millis(100)).await;
    timing
        .lock()
        .unwrap()
        .record("after-sleep".to_string(), None);
    timing
        .lock()
        .unwrap()
        .record_timing("custom".to_string(), Duration::from_millis(66), None);
    Html("<h1>Hello, World!</h1>")
}

Output

HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
server-timing: HelloService;desc="whatever";dur=102.43, after-sleep;dur=102.40, custom;dur=66.00
content-length: 22
date: Sat, 22 Mar 2025 20:32:45 GMT

<h1>Hello, World!</h1>

Dependencies

~1MB
~17K SLoC