Skip to content

Commit 422852e

Browse files
committed
add timing infos to response header
1 parent 94217e2 commit 422852e

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

libsql-server/src/connection/libsql.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::query_analysis::StmtKind;
2323
use crate::query_result_builder::{QueryBuilderConfig, QueryResultBuilder};
2424
use crate::replication::FrameNo;
2525
use crate::stats::{Stats, StatsUpdateMessage};
26-
use crate::{Result, BLOCKING_RT};
26+
use crate::{record_time, Result, BLOCKING_RT};
2727

2828
use super::connection_manager::{
2929
ConnectionManager, InnerWalManager, ManagedConnectionWal, ManagedConnectionWalWrapper,
@@ -713,7 +713,10 @@ where
713713
builder: B,
714714
_replication_index: Option<FrameNo>,
715715
) -> Result<B> {
716-
self.execute(pgm, ctx, builder).await.map(|(b, _)| b)
716+
record_time! {
717+
"libsql_query_exec";
718+
self.execute(pgm, ctx, builder).await.map(|(b, _)| b)
719+
}
717720
}
718721

719722
async fn describe(

libsql-server/src/http/user/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ mod listen;
66
mod result_builder;
77
mod trace;
88
mod types;
9+
#[macro_use]
10+
pub mod timing;
911

1012
use std::path::Path;
1113
use std::sync::Arc;
@@ -16,7 +18,7 @@ use axum::http::request::Parts;
1618
use axum::http::HeaderValue;
1719
use axum::response::{Html, IntoResponse};
1820
use axum::routing::{get, post};
19-
use axum::Router;
21+
use axum::{middleware, Router};
2022
use axum_extra::middleware::option_layer;
2123
use base64::prelude::BASE64_STANDARD_NO_PAD;
2224
use base64::Engine;
@@ -37,6 +39,7 @@ use crate::connection::{Connection, RequestContext};
3739
use crate::error::Error;
3840
use crate::hrana;
3941
use crate::http::user::db_factory::MakeConnectionExtractorPath;
42+
use crate::http::user::timing::timings_middleware;
4043
use crate::http::user::types::HttpQuery;
4144
use crate::metrics::LEGACY_HTTP_CALL;
4245
use crate::namespace::NamespaceStore;
@@ -407,6 +410,7 @@ where
407410
)
408411
.route("/v1/jobs", get(handle_get_migrations))
409412
.route("/v1/jobs/:job_id", get(handle_get_migration_details))
413+
.layer(middleware::from_fn(timings_middleware))
410414
.with_state(state);
411415

412416
// Merge the grpc based axum router into our regular http router
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::fmt::Write as _;
2+
use std::sync::Arc;
3+
use std::time::Duration;
4+
5+
use axum::http::Request;
6+
use axum::middleware::Next;
7+
use axum::response::Response;
8+
use hashbrown::HashMap;
9+
use parking_lot::Mutex;
10+
11+
#[derive(Default, Clone, Debug)]
12+
pub struct Timings {
13+
records: Arc<Mutex<HashMap<&'static str, Duration>>>,
14+
}
15+
16+
impl Timings {
17+
pub fn record(&self, k: &'static str, d: Duration) {
18+
self.records.lock().insert(k, d);
19+
}
20+
21+
fn format(&self) -> String {
22+
let mut out = String::new();
23+
let records = self.records.lock();
24+
for (k, v) in records.iter() {
25+
write!(&mut out, "{k};dur={v:?},").unwrap();
26+
}
27+
out
28+
}
29+
}
30+
31+
tokio::task_local! {
32+
pub static TIMINGS: Timings;
33+
}
34+
35+
#[macro_export]
36+
macro_rules! record_time {
37+
($k:literal; $($rest:tt)*) => {
38+
{
39+
let __before__ = std::time::Instant::now();
40+
let __ret__ = {
41+
$($rest)*
42+
};
43+
let _ = $crate::http::user::timing::TIMINGS.try_with(|t| t.record($k, __before__.elapsed()));
44+
__ret__
45+
}
46+
};
47+
}
48+
49+
pub(crate) async fn timings_middleware<B>(request: Request<B>, next: Next<B>) -> Response {
50+
TIMINGS
51+
.scope(Default::default(), async move {
52+
let mut response = record_time! {
53+
"query_total";
54+
next.run(request).await
55+
};
56+
let timings = TIMINGS.get().format();
57+
response
58+
.headers_mut()
59+
.insert("Server-Timing", timings.parse().unwrap());
60+
response
61+
})
62+
.await
63+
}

0 commit comments

Comments
 (0)