Skip to content

Commit

Permalink
Merge pull request #521 from psarna/resetstats
Browse files Browse the repository at this point in the history
admin: allow resetting top and slowest queries
  • Loading branch information
MarinPostma committed Oct 29, 2023
2 parents c9b60a1 + ac74077 commit d98eeb5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libsql-server/src/http/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ where
)
.route("/v1/namespaces/:namespace", delete(handle_delete_namespace))
.route("/v1/namespaces/:namespace/stats", get(stats::handle_stats))
.route(
"/v1/namespaces/:namespace/stats/:stats_type",
delete(stats::handle_delete_stats),
)
.route("/v1/diagnostics", get(handle_diagnostics))
.route("/metrics", get(handle_metrics))
.with_state(Arc::new(AppState {
Expand Down
17 changes: 17 additions & 0 deletions libsql-server/src/http/admin/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,20 @@ pub(super) async fn handle_stats<M: MakeNamespace, C>(

Ok(Json(resp))
}

pub(super) async fn handle_delete_stats<M: MakeNamespace, C>(
State(app_state): State<Arc<AppState<M, C>>>,
Path((namespace, stats_type)): Path<(String, String)>,
) -> crate::Result<()> {
let stats = app_state
.namespaces
.stats(NamespaceName::from_string(namespace)?)
.await?;
match stats_type.as_str() {
"top" => stats.reset_top_queries(),
"slowest" => stats.reset_slowest_queries(),
_ => return Err(crate::error::Error::Internal("Invalid stats type".into())),
}

Ok(())
}
10 changes: 10 additions & 0 deletions libsql-server/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ impl Stats {
&self.top_queries
}

pub(crate) fn reset_top_queries(&self) {
self.top_queries.write().unwrap().clear();
self.top_query_threshold.store(0, Ordering::Relaxed);
}

pub(crate) fn add_slowest_query(&self, query: SlowestQuery) {
let mut slowest_queries = self.slowest_queries.write().unwrap();
tracing::debug!("slowest query: {}: {}", query.elapsed_ms, query.query);
Expand All @@ -186,6 +191,11 @@ impl Stats {
pub(crate) fn slowest_queries(&self) -> &Arc<RwLock<BTreeSet<SlowestQuery>>> {
&self.slowest_queries
}

pub(crate) fn reset_slowest_queries(&self) {
self.slowest_queries.write().unwrap().clear();
self.slowest_query_threshold.store(0, Ordering::Relaxed);
}
}

async fn spawn_stats_persist_thread(stats: Weak<Stats>, path: PathBuf) -> anyhow::Result<()> {
Expand Down

0 comments on commit d98eeb5

Please sign in to comment.