Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added in a SQL execution time calculation #2297

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/client-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl Host {
// We need a header for query results
let mut header = vec![];

let sql_start = std::time::Instant::now();
let sql_span =
tracing::trace_span!("execute_sql", total_duration = tracing::field::Empty,).entered();

let rows = sql::execute::run(
// Returns an empty result set for mutations
db,
Expand All @@ -101,13 +105,20 @@ impl Host {
}
})?;

let total_duration = sql_start.elapsed();
sql_span.record("total_duration", tracing::field::debug(total_duration));

// Turn the header into a `ProductType`
let schema = header
.into_iter()
.map(|(col_name, col_type)| ProductTypeElement::new(col_type, Some(col_name)))
.collect();

Ok(vec![StmtResultJson { schema, rows }])
Ok(vec![StmtResultJson {
schema,
rows,
total_duration_micros: total_duration.as_micros() as u64,
}])
},
)
.await
Expand Down
9 changes: 8 additions & 1 deletion crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::auth::{
anon_auth_middleware, SpacetimeAuth, SpacetimeEnergyUsed, SpacetimeExecutionDurationMicros, SpacetimeIdentity,
SpacetimeIdentityToken,
Expand Down Expand Up @@ -385,7 +387,12 @@ where
.ok_or(StatusCode::NOT_FOUND)?;
let json = host.exec_sql(auth, database, body).await?;

Ok(axum::Json(json))
let total_duration = json.iter().fold(0, |acc, x| acc + x.total_duration_micros);

Ok((
TypedHeader(SpacetimeExecutionDurationMicros(Duration::from_micros(total_duration))),
axum::Json(json),
))
}

#[derive(Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/json/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ use spacetimedb_lib::{ProductType, ProductValue};
pub struct StmtResultJson {
pub schema: ProductType,
pub rows: Vec<ProductValue>,
pub total_duration_micros: u64,
}
Loading