From 282db822fc1831a64b0dcda8b6d5927fe6675352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 06:16:57 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20fix:=20request-id=20bound=20reac?= =?UTF-8?q?hes=20/limits;=20registry-read=20probe=20no=20longer=20depends?= =?UTF-8?q?=20on=20span=20thread?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MAX_REQUEST_ID_LEN sat in src/lib.rs, so the one bound a client can actually hit on a header it sends reached neither GET /limits nor the OpenAPI spec. Moved to src/constant.rs, published as `request.max_request_id_len`, and added to README's /limits sample. probe_registry_reads_per_request counted SurrealDB spans through a thread-local subscriber (`set_default`). tracing caches a callsite's interest process-wide the first time any thread reaches it: under parallel tests that thread is a sibling with no subscriber, so the SDK's callsites were disabled for the whole process before the probe asked and it panicked UNMEASURED (0 in 7 of 8 runs against noisy siblings). It now installs a counting Subscriber globally, with `Interest::sometimes` and an `enabled` that admits only surrealdb callsites on its own thread, so it stores nothing and no sibling test lands in the figure. --- README.md | 2 +- src/constant.rs | 10 +++++++ src/lib.rs | 4 +-- src/web/limits.rs | 5 ++++ tests/builder_api.rs | 65 ++++++++++++++++++++++++++++---------------- 5 files changed, 58 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 906965a..6f83e00 100644 --- a/README.md +++ b/README.md @@ -585,7 +585,7 @@ response in the same commit. "max_grade_bands": 20, "max_grade_label_len": 20, "required_attendance_statuses": ["present", "absent", "late", "excused"] }, "request": { "max_page_limit": 500, "schedule_past_grace_ms": 60000, - "request_timeout_secs": 30 }, + "request_timeout_secs": 30, "max_request_id_len": 64 }, "rate": { "window_secs": 60, "auth_per_minute": 10, "api_per_minute": 300, "chatbot_per_minute": 20 } } diff --git a/src/constant.rs b/src/constant.rs index d9ce291..8fc008b 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -435,6 +435,16 @@ pub const DB_PING_TIMEOUT_SECS: u64 = 2; /// Generous enough not to clip a legitimate slow upload at `max_file_bytes`. pub const REQUEST_TIMEOUT_SECS: u64 = 30; +/// The longest caller-supplied `x-request-id` header the server will carry. +/// +/// The id lands on `http.request.id` of every span, so anything a client puts +/// there it can export: free text and unbounded cardinality both. A header +/// longer than this (or one carrying anything outside `[A-Za-z0-9._-]`) is +/// dropped and a UUID minted in its place — the request still succeeds, it +/// just stops being correlatable by the caller's own id, which is exactly why +/// a client needs to know the bound. +pub const MAX_REQUEST_ID_LEN: usize = 64; + /// Backoff ceiling for the boot connection retry. The database is usually a /// sibling container coming up in parallel, so the first attempts fail; the /// process retries forever rather than exiting, because exiting turns a diff --git a/src/lib.rs b/src/lib.rs index 35e70bc..0395d94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ use utoipa_axum::router::OpenApiRouter; use utoipa_axum::routes; use utoipa_swagger_ui::SwaggerUi; +use crate::constant::MAX_REQUEST_ID_LEN; use crate::error::AppError; use crate::module::Module; use crate::rate_limit::RateLimiter; @@ -325,9 +326,6 @@ async fn drop_unusable_request_id(mut req: Request, next: Next) -> Response { next.run(req).await } -/// The longest caller-supplied request id we will carry. -const MAX_REQUEST_ID_LEN: usize = 64; - /// The header carrying the per-request id, in and out. const REQUEST_ID_HEADER: &str = "x-request-id"; diff --git a/src/web/limits.rs b/src/web/limits.rs index b91a0a4..0e195ff 100644 --- a/src/web/limits.rs +++ b/src/web/limits.rs @@ -442,6 +442,10 @@ struct RequestLimits { schedule_past_grace_ms: i64, /// Ceiling on how long one request may take before it is aborted. request_timeout_secs: u64, + /// Longest `x-request-id` header the server keeps. A longer one — or one + /// carrying anything outside `[A-Za-z0-9._-]` — is replaced by a + /// server-minted UUID, so the caller loses correlation on its own id. + max_request_id_len: usize, } /// Every fixed limit the API enforces, grouped by the resource it applies to. @@ -658,6 +662,7 @@ impl LimitsResponse { max_page_limit: MAX_PAGE_LIMIT, schedule_past_grace_ms: SCHEDULE_PAST_GRACE_MS, request_timeout_secs: REQUEST_TIMEOUT_SECS, + max_request_id_len: MAX_REQUEST_ID_LEN, }, } } diff --git a/tests/builder_api.rs b/tests/builder_api.rs index 26b4428..fe3f511 100644 --- a/tests/builder_api.rs +++ b/tests/builder_api.rs @@ -2043,39 +2043,56 @@ async fn probe_a_refused_batch_leaves_the_row_byte_identical() { /// counting the SDK's own tracing spans. Prints the figure; asserts only that /// the instrumentation saw *something*, so a zero is reported as UNMEASURED /// rather than as a passing measurement. +/// +/// The subscriber is **global**, not `set_default`'s thread-local one, because +/// a thread-local subscriber cannot measure this reliably: `tracing` caches a +/// callsite's interest process-wide the first time any thread reaches it, and +/// with tests running in parallel that thread is usually a sibling with no +/// subscriber — the SurrealDB callsites are then disabled for the whole +/// process before this test ever asks, and the count is 0 (measured: 0 in 7 of +/// 8 runs against noisy siblings; a global subscriber counted on 6 of 6). +/// Global is safe here because this subscriber only counts: it stores nothing, +/// prints nothing, and its `enabled` refuses every callsite that is not a +/// SurrealDB one on *this* test's thread, so a sibling test neither pays for it +/// nor lands in the figure. #[tokio::test] async fn probe_registry_reads_per_request() { use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; - use tracing_subscriber::layer::SubscriberExt; - - #[derive(Clone)] - struct Counter(Arc); - impl tracing_subscriber::Layer for Counter { - fn on_new_span( - &self, - attrs: &tracing::span::Attributes<'_>, - _id: &tracing::Id, - _ctx: tracing_subscriber::layer::Context<'_, S>, - ) { - if attrs.metadata().target().starts_with("surrealdb") { - self.0.fetch_add(1, Ordering::Relaxed); - } + use tracing::span; + + struct Counter { + thread: std::thread::ThreadId, + count: Arc, + } + impl tracing::Subscriber for Counter { + // Never `always`/`never`: those are cached per callsite, and this + // subscriber's answer depends on which thread is asking. + fn register_callsite(&self, _: &tracing::Metadata<'_>) -> tracing::subscriber::Interest { + tracing::subscriber::Interest::sometimes() + } + fn enabled(&self, meta: &tracing::Metadata<'_>) -> bool { + meta.target().starts_with("surrealdb") && std::thread::current().id() == self.thread + } + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + self.count.fetch_add(1, Ordering::Relaxed); + span::Id::from_u64(1) } - fn on_event( - &self, - event: &tracing::Event<'_>, - _ctx: tracing_subscriber::layer::Context<'_, S>, - ) { - if event.metadata().target().starts_with("surrealdb") { - self.0.fetch_add(1, Ordering::Relaxed); - } + fn event(&self, _: &tracing::Event<'_>) { + self.count.fetch_add(1, Ordering::Relaxed); } + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} + fn enter(&self, _: &span::Id) {} + fn exit(&self, _: &span::Id) {} } let count = Arc::new(AtomicUsize::new(0)); - let subscriber = tracing_subscriber::registry().with(Counter(count.clone())); - let _guard = tracing::subscriber::set_default(subscriber); + tracing::subscriber::set_global_default(Counter { + thread: std::thread::current().id(), + count: count.clone(), + }) + .expect("no other test in this binary installs a global subscriber"); let (app, db, _tenants) = deployment().await; let cookie = login_as(&app, &db, "boss", "admin").await;