Skip to content

Commit e9764bb

Browse files
committed
add admin endpoint to dynamically set log filter
1 parent d564d70 commit e9764bb

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct AppState<C> {
4848
user_http_server: Arc<hrana::http::Server>,
4949
connector: C,
5050
metrics: Metrics,
51+
set_env_filter: Option<Box<dyn Fn(&str) -> anyhow::Result<()> + Sync + Send + 'static>>,
5152
}
5253

5354
impl<C> FromRef<Arc<AppState<C>>> for Metrics {
@@ -66,6 +67,7 @@ pub async fn run<A, C>(
6667
disable_metrics: bool,
6768
shutdown: Arc<Notify>,
6869
auth: Option<Arc<str>>,
70+
set_env_filter: Option<Box<dyn Fn(&str) -> anyhow::Result<()> + Sync + Send + 'static>>,
6971
) -> anyhow::Result<()>
7072
where
7173
A: crate::net::Accept,
@@ -167,11 +169,13 @@ where
167169
.route("/profile/heap/enable", post(enable_profile_heap))
168170
.route("/profile/heap/disable/:id", post(disable_profile_heap))
169171
.route("/profile/heap/:id", delete(delete_profile_heap))
172+
.route("/log-filter", post(handle_set_log_filter))
170173
.with_state(Arc::new(AppState {
171174
namespaces: namespaces.clone(),
172175
connector,
173176
user_http_server,
174177
metrics,
178+
set_env_filter,
175179
}))
176180
.layer(
177181
tower_http::trace::TraceLayer::new_for_http()
@@ -512,6 +516,16 @@ async fn handle_delete_namespace<C>(
512516
Ok(())
513517
}
514518

519+
async fn handle_set_log_filter<C>(
520+
State(app_state): State<Arc<AppState<C>>>,
521+
body: String,
522+
) -> crate::Result<()> {
523+
if let Some(ref cb) = app_state.set_env_filter {
524+
cb(&body)?;
525+
}
526+
Ok(())
527+
}
528+
515529
async fn handle_checkpoint<C>(
516530
State(app_state): State<Arc<AppState<C>>>,
517531
Path(namespace): Path<NamespaceName>,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use axum::middleware::Next;
77
use axum::response::Response;
88
use hashbrown::HashMap;
99
use parking_lot::Mutex;
10-
use tracing::{Instrument, Span};
1110

1211
#[derive(Default, Clone, Debug)]
1312
pub struct Timings {

libsql-server/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub struct Server<C = HttpConnector, A = AddrIncoming, D = HttpsConnector<HttpCo
181181
pub should_sync_from_storage: bool,
182182
pub force_load_wals: bool,
183183
pub sync_conccurency: usize,
184-
pub set_log_level: Option<Box<dyn Fn(&str) -> anyhow::Result<()> + Send +'static>>
184+
pub set_log_level: Option<Box<dyn Fn(&str) -> anyhow::Result<()> + Send + Sync + 'static>>,
185185
}
186186

187187
impl<C, A, D> Default for Server<C, A, D> {
@@ -227,6 +227,7 @@ struct Services<A, P, S, C> {
227227
disable_default_namespace: bool,
228228
db_config: DbConfig,
229229
user_auth_strategy: Auth,
230+
pub set_log_level: Option<Box<dyn Fn(&str) -> anyhow::Result<()> + Send + Sync + 'static>>,
230231
}
231232

232233
struct TaskManager {
@@ -306,7 +307,7 @@ where
306307
S: ReplicationLog,
307308
C: Connector,
308309
{
309-
fn configure(self, task_manager: &mut TaskManager) {
310+
fn configure(mut self, task_manager: &mut TaskManager) {
310311
let user_http = UserApi {
311312
http_acceptor: self.user_api_config.http_acceptor,
312313
hrana_ws_acceptor: self.user_api_config.hrana_ws_acceptor,
@@ -341,6 +342,7 @@ where
341342
disable_metrics,
342343
shutdown,
343344
auth_key.map(Into::into),
345+
self.set_log_level.take(),
344346
)
345347
});
346348
}
@@ -540,7 +542,7 @@ where
540542
}
541543

542544
fn make_services<P: Proxy, L: ReplicationLog>(
543-
self,
545+
mut self,
544546
namespace_store: NamespaceStore,
545547
idle_shutdown_kicker: Option<IdleShutdownKicker>,
546548
proxy_service: P,
@@ -558,6 +560,7 @@ where
558560
disable_default_namespace: self.disable_default_namespace,
559561
db_config: self.db_config,
560562
user_auth_strategy,
563+
set_log_level: self.set_log_level.take(),
561564
}
562565
}
563566

libsql-server/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use hyper::client::HttpConnector;
1111
use libsql_server::auth::{parse_http_basic_auth_arg, parse_jwt_keys, user_auth_strategies, Auth};
1212
use tokio::sync::Notify;
1313
use tokio::time::Duration;
14-
use tracing_subscriber::{prelude::*, EnvFilter};
1514
use tracing_subscriber::util::SubscriberInitExt;
1615
use tracing_subscriber::Layer;
16+
use tracing_subscriber::{prelude::*, EnvFilter};
1717

1818
use libsql_server::config::{
1919
AdminApiConfig, BottomlessConfig, DbConfig, HeartbeatConfig, MetaStoreConfig, RpcClientConfig,
@@ -642,7 +642,10 @@ fn make_meta_store_config(config: &Cli) -> anyhow::Result<MetaStoreConfig> {
642642
})
643643
}
644644

645-
async fn build_server(config: &Cli, set_log_level: impl Fn(&str) -> anyhow::Result<()> + Send + 'static) -> anyhow::Result<Server> {
645+
async fn build_server(
646+
config: &Cli,
647+
set_log_level: impl Fn(&str) -> anyhow::Result<()> + Send + Sync + 'static,
648+
) -> anyhow::Result<Server> {
646649
let db_config = make_db_config(config)?;
647650
let user_api_config = make_user_api_config(config).await?;
648651
let admin_api_config = make_admin_api_config(config).await?;
@@ -750,9 +753,10 @@ async fn main() -> Result<()> {
750753
#[cfg(feature = "debug-tools")]
751754
enable_libsql_logging();
752755

753-
let (filter, reload_handle) = tracing_subscriber::reload::Layer::new(tracing_subscriber::EnvFilter::from_default_env());
756+
let (filter, reload_handle) =
757+
tracing_subscriber::reload::Layer::new(tracing_subscriber::EnvFilter::from_default_env());
754758
let set_log_level = move |s: &str| -> anyhow::Result<()> {
755-
let filter = EnvFilter::from_str(s)?;
759+
let filter = EnvFilter::from_str(s.trim())?;
756760
reload_handle.reload(filter)?;
757761
Ok(())
758762
};

0 commit comments

Comments
 (0)