Skip to content

Commit 654cb97

Browse files
committed
feat: Add Server-Timing response header
1 parent 7749919 commit 654cb97

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/context.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::any::Any;
55
use std::collections::{BTreeMap, HashMap};
66
use std::fmt::{Debug, Display};
77
use std::sync::Arc;
8-
8+
use std::time::SystemTime;
99
use bytes::Bytes;
1010
use chrono::{DateTime, FixedOffset};
1111
use maplit::hashmap;
@@ -374,7 +374,9 @@ pub struct WebmachineContext {
374374
/// If a new resource was created
375375
pub new_resource: bool,
376376
/// General store of metadata. You can use this to store attributes as the webmachine executes.
377-
pub metadata: HashMap<String, MetaDataValue>
377+
pub metadata: HashMap<String, MetaDataValue>,
378+
/// Start time instant when the context was created
379+
pub start_time: SystemTime
378380
}
379381

380382
impl WebmachineContext {
@@ -400,7 +402,8 @@ impl Default for WebmachineContext {
400402
if_modified_since: None,
401403
redirect: false,
402404
new_resource: false,
403-
metadata: HashMap::new()
405+
metadata: HashMap::new(),
406+
start_time: SystemTime::now()
404407
}
405408
}
406409
}

src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ use std::collections::{BTreeMap, HashMap};
126126
use std::fmt::{Debug, Formatter};
127127
use std::future::ready;
128128
use std::pin::Pin;
129-
129+
use std::time::SystemTime;
130130
use async_trait::async_trait;
131131
use bytes::Bytes;
132132
use chrono::{DateTime, FixedOffset, Utc};
@@ -1485,6 +1485,17 @@ async fn finalise_response(context: &mut WebmachineContext, resource: &(dyn Reso
14851485
resource.finish_request(context);
14861486
resource.finalise_response(context);
14871487

1488+
if let Ok(duration) = context.start_time.elapsed() {
1489+
context.response.add_header("Server-Timing", vec![HeaderValue {
1490+
value: "response".to_string(),
1491+
params: hashmap!{
1492+
"desc".to_string() => "Total Response Time".to_string(),
1493+
"dur".to_string() => format!("{:?}", duration)
1494+
},
1495+
quote: true
1496+
}])
1497+
}
1498+
14881499
let body_size = context.response.body.as_ref().map(|bytes| bytes.len()).unwrap_or_default();
14891500
debug!(status = context.response.status, headers = ?context.response.headers, body_size, "Final response");
14901501
}
@@ -1518,10 +1529,12 @@ impl WebmachineDispatcher {
15181529
}
15191530

15201531
async fn context_from_http_request(&self, req: Request<Incoming>) -> WebmachineContext {
1532+
let now = SystemTime::now();
15211533
let request = request_from_http_request(req).await;
15221534
WebmachineContext {
15231535
request,
15241536
response: WebmachineResponse::default(),
1537+
start_time: now,
15251538
.. WebmachineContext::default()
15261539
}
15271540
}

0 commit comments

Comments
 (0)