Request-id bound reaches /limits
Browse files at this commit282db822fc1831a64b0dcda8b6d5927fe6675352Parent: 11230a0- fix
- request-id bound reaches /limits
- registry-read probe no longer depends on span thread
5 changed files +58 -28
- README.md +1 -1
- src/constant.rs +10 -0
- src/lib.rs +1 -3
- src/web/limits.rs +5 -0
- tests/builder_api.rs +41 -24
README.mdmodified+1 -1
| @@ -585,7 +585,7 @@ response in the same commit. | |||
| 585 | "max_grade_bands": 20, "max_grade_label_len": 20, | 585 | "max_grade_bands": 20, "max_grade_label_len": 20, |
| 586 | "required_attendance_statuses": ["present", "absent", "late", "excused"] }, | 586 | "required_attendance_statuses": ["present", "absent", "late", "excused"] }, |
| 587 | "request": { "max_page_limit": 500, "schedule_past_grace_ms": 60000, | 587 | "request": { "max_page_limit": 500, "schedule_past_grace_ms": 60000, |
| 588 | - "request_timeout_secs": 30 }, | 588 | + "request_timeout_secs": 30, "max_request_id_len": 64 }, |
| 589 | "rate": { "window_secs": 60, "auth_per_minute": 10, | 589 | "rate": { "window_secs": 60, "auth_per_minute": 10, |
| 590 | "api_per_minute": 300, "chatbot_per_minute": 20 } | 590 | "api_per_minute": 300, "chatbot_per_minute": 20 } |
| 591 | } | 591 | } |
src/constant.rsmodified+10 -0
| @@ -435,6 +435,16 @@ pub const DB_PING_TIMEOUT_SECS: u64 = 2; | |||
| 435 | /// Generous enough not to clip a legitimate slow upload at `max_file_bytes`. | 435 | /// Generous enough not to clip a legitimate slow upload at `max_file_bytes`. |
| 436 | pub const REQUEST_TIMEOUT_SECS: u64 = 30; | 436 | pub const REQUEST_TIMEOUT_SECS: u64 = 30; |
| 437 | 437 | ||
| 438 | +/// The longest caller-supplied `x-request-id` header the server will carry. | ||
| 439 | +/// | ||
| 440 | +/// The id lands on `http.request.id` of every span, so anything a client puts | ||
| 441 | +/// there it can export: free text and unbounded cardinality both. A header | ||
| 442 | +/// longer than this (or one carrying anything outside `[A-Za-z0-9._-]`) is | ||
| 443 | +/// dropped and a UUID minted in its place — the request still succeeds, it | ||
| 444 | +/// just stops being correlatable by the caller's own id, which is exactly why | ||
| 445 | +/// a client needs to know the bound. | ||
| 446 | +pub const MAX_REQUEST_ID_LEN: usize = 64; | ||
| 447 | + | ||
| 438 | /// Backoff ceiling for the boot connection retry. The database is usually a | 448 | /// Backoff ceiling for the boot connection retry. The database is usually a |
| 439 | /// sibling container coming up in parallel, so the first attempts fail; the | 449 | /// sibling container coming up in parallel, so the first attempts fail; the |
| 440 | /// process retries forever rather than exiting, because exiting turns a | 450 | /// process retries forever rather than exiting, because exiting turns a |
src/lib.rsmodified+1 -3
| @@ -29,6 +29,7 @@ use utoipa_axum::router::OpenApiRouter; | |||
| 29 | use utoipa_axum::routes; | 29 | use utoipa_axum::routes; |
| 30 | use utoipa_swagger_ui::SwaggerUi; | 30 | use utoipa_swagger_ui::SwaggerUi; |
| 31 | 31 | ||
| 32 | +use crate::constant::MAX_REQUEST_ID_LEN; | ||
| 32 | use crate::error::AppError; | 33 | use crate::error::AppError; |
| 33 | use crate::module::Module; | 34 | use crate::module::Module; |
| 34 | use crate::rate_limit::RateLimiter; | 35 | use crate::rate_limit::RateLimiter; |
| @@ -325,9 +326,6 @@ async fn drop_unusable_request_id(mut req: Request, next: Next) -> Response { | |||
| 325 | next.run(req).await | 326 | next.run(req).await |
| 326 | } | 327 | } |
| 327 | 328 | ||
| 328 | -/// The longest caller-supplied request id we will carry. | ||
| 329 | -const MAX_REQUEST_ID_LEN: usize = 64; | ||
| 330 | - | ||
| 331 | /// The header carrying the per-request id, in and out. | 329 | /// The header carrying the per-request id, in and out. |
| 332 | const REQUEST_ID_HEADER: &str = "x-request-id"; | 330 | const REQUEST_ID_HEADER: &str = "x-request-id"; |
| 333 | 331 | ||
src/web/limits.rsmodified+5 -0
| @@ -442,6 +442,10 @@ struct RequestLimits { | |||
| 442 | schedule_past_grace_ms: i64, | 442 | schedule_past_grace_ms: i64, |
| 443 | /// Ceiling on how long one request may take before it is aborted. | 443 | /// Ceiling on how long one request may take before it is aborted. |
| 444 | request_timeout_secs: u64, | 444 | request_timeout_secs: u64, |
| 445 | + /// Longest `x-request-id` header the server keeps. A longer one — or one | ||
| 446 | + /// carrying anything outside `[A-Za-z0-9._-]` — is replaced by a | ||
| 447 | + /// server-minted UUID, so the caller loses correlation on its own id. | ||
| 448 | + max_request_id_len: usize, | ||
| 445 | } | 449 | } |
| 446 | 450 | ||
| 447 | /// Every fixed limit the API enforces, grouped by the resource it applies to. | 451 | /// Every fixed limit the API enforces, grouped by the resource it applies to. |
| @@ -658,6 +662,7 @@ impl LimitsResponse { | |||
| 658 | max_page_limit: MAX_PAGE_LIMIT, | 662 | max_page_limit: MAX_PAGE_LIMIT, |
| 659 | schedule_past_grace_ms: SCHEDULE_PAST_GRACE_MS, | 663 | schedule_past_grace_ms: SCHEDULE_PAST_GRACE_MS, |
| 660 | request_timeout_secs: REQUEST_TIMEOUT_SECS, | 664 | request_timeout_secs: REQUEST_TIMEOUT_SECS, |
| 665 | + max_request_id_len: MAX_REQUEST_ID_LEN, | ||
| 661 | }, | 666 | }, |
| 662 | } | 667 | } |
| 663 | } | 668 | } |
tests/builder_api.rsmodified+41 -24
| @@ -2043,39 +2043,56 @@ async fn probe_a_refused_batch_leaves_the_row_byte_identical() { | |||
| 2043 | /// counting the SDK's own tracing spans. Prints the figure; asserts only that | 2043 | /// counting the SDK's own tracing spans. Prints the figure; asserts only that |
| 2044 | /// the instrumentation saw *something*, so a zero is reported as UNMEASURED | 2044 | /// the instrumentation saw *something*, so a zero is reported as UNMEASURED |
| 2045 | /// rather than as a passing measurement. | 2045 | /// rather than as a passing measurement. |
| 2046 | +/// | ||
| 2047 | +/// The subscriber is **global**, not `set_default`'s thread-local one, because | ||
| 2048 | +/// a thread-local subscriber cannot measure this reliably: `tracing` caches a | ||
| 2049 | +/// callsite's interest process-wide the first time any thread reaches it, and | ||
| 2050 | +/// with tests running in parallel that thread is usually a sibling with no | ||
| 2051 | +/// subscriber — the SurrealDB callsites are then disabled for the whole | ||
| 2052 | +/// process before this test ever asks, and the count is 0 (measured: 0 in 7 of | ||
| 2053 | +/// 8 runs against noisy siblings; a global subscriber counted on 6 of 6). | ||
| 2054 | +/// Global is safe here because this subscriber only counts: it stores nothing, | ||
| 2055 | +/// prints nothing, and its `enabled` refuses every callsite that is not a | ||
| 2056 | +/// SurrealDB one on *this* test's thread, so a sibling test neither pays for it | ||
| 2057 | +/// nor lands in the figure. | ||
| 2046 | #[tokio::test] | 2058 | #[tokio::test] |
| 2047 | async fn probe_registry_reads_per_request() { | 2059 | async fn probe_registry_reads_per_request() { |
| 2048 | use std::sync::Arc; | 2060 | use std::sync::Arc; |
| 2049 | use std::sync::atomic::{AtomicUsize, Ordering}; | 2061 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 2050 | - use tracing_subscriber::layer::SubscriberExt; | 2062 | + use tracing::span; |
| 2051 | - | 2063 | + |
| 2052 | - #[derive(Clone)] | 2064 | + struct Counter { |
| 2053 | - struct Counter(Arc<AtomicUsize>); | 2065 | + thread: std::thread::ThreadId, |
| 2054 | - impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Counter { | 2066 | + count: Arc<AtomicUsize>, |
| 2055 | - fn on_new_span( | 2067 | + } |
| 2056 | - &self, | 2068 | + impl tracing::Subscriber for Counter { |
| 2057 | - attrs: &tracing::span::Attributes<'_>, | 2069 | + // Never `always`/`never`: those are cached per callsite, and this |
| 2058 | - _id: &tracing::Id, | 2070 | + // subscriber's answer depends on which thread is asking. |
| 2059 | - _ctx: tracing_subscriber::layer::Context<'_, S>, | 2071 | + fn register_callsite(&self, _: &tracing::Metadata<'_>) -> tracing::subscriber::Interest { |
| 2060 | - ) { | 2072 | + tracing::subscriber::Interest::sometimes() |
| 2061 | - if attrs.metadata().target().starts_with("surrealdb") { | 2073 | + } |
| 2062 | - self.0.fetch_add(1, Ordering::Relaxed); | 2074 | + fn enabled(&self, meta: &tracing::Metadata<'_>) -> bool { |
| 2063 | - } | 2075 | + meta.target().starts_with("surrealdb") && std::thread::current().id() == self.thread |
| 2076 | + } | ||
| 2077 | + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { | ||
| 2078 | + self.count.fetch_add(1, Ordering::Relaxed); | ||
| 2079 | + span::Id::from_u64(1) | ||
| 2064 | } | 2080 | } |
| 2065 | - fn on_event( | 2081 | + fn event(&self, _: &tracing::Event<'_>) { |
| 2066 | - &self, | 2082 | + self.count.fetch_add(1, Ordering::Relaxed); |
| 2067 | - event: &tracing::Event<'_>, | ||
| 2068 | - _ctx: tracing_subscriber::layer::Context<'_, S>, | ||
| 2069 | - ) { | ||
| 2070 | - if event.metadata().target().starts_with("surrealdb") { | ||
| 2071 | - self.0.fetch_add(1, Ordering::Relaxed); | ||
| 2072 | - } | ||
| 2073 | } | 2083 | } |
| 2084 | + fn record(&self, _: &span::Id, _: &span::Record<'_>) {} | ||
| 2085 | + fn record_follows_from(&self, _: &span::Id, _: &span::Id) {} | ||
| 2086 | + fn enter(&self, _: &span::Id) {} | ||
| 2087 | + fn exit(&self, _: &span::Id) {} | ||
| 2074 | } | 2088 | } |
| 2075 | 2089 | ||
| 2076 | let count = Arc::new(AtomicUsize::new(0)); | 2090 | let count = Arc::new(AtomicUsize::new(0)); |
| 2077 | - let subscriber = tracing_subscriber::registry().with(Counter(count.clone())); | 2091 | + tracing::subscriber::set_global_default(Counter { |
| 2078 | - let _guard = tracing::subscriber::set_default(subscriber); | 2092 | + thread: std::thread::current().id(), |
| 2093 | + count: count.clone(), | ||
| 2094 | + }) | ||
| 2095 | + .expect("no other test in this binary installs a global subscriber"); | ||
| 2079 | 2096 | ||
| 2080 | let (app, db, _tenants) = deployment().await; | 2097 | let (app, db, _tenants) = deployment().await; |
| 2081 | let cookie = login_as(&app, &db, "boss", "admin").await; | 2098 | let cookie = login_as(&app, &db, "boss", "admin").await; |