Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(flags): instrument concurrency layer (#57738)

Matheus Batista607747753bd2
diff --git a/rust/feature-flags/src/api/concurrency_metrics.rs b/rust/feature-flags/src/api/concurrency_metrics.rs
new file mode 100644
index 000000000000..d656a85a5d3a
--- /dev/null
+++ b/rust/feature-flags/src/api/concurrency_metrics.rs
@@ -0,0 +1,187 @@
+//! Tower-layer instrumentation for `ConcurrencyLimitLayer` permit-wait.
+//!
+//! `tower::limit::ConcurrencyLimitLayer` exposes no timing hook for permit
+//! acquisition, so we sandwich it between two `axum::middleware::from_fn`
+//! shims that communicate via request extensions:
+//!
+//! 1. [`record_concurrency_enter`] runs *before* the request reaches
+//! `ConcurrencyLimitLayer` and stamps the entry instant on the request.
+//! 2. [`record_concurrency_wait`] runs *after* the layer has handed off a
+//! permit and computes `Instant::elapsed`, which the handler then reads
+//! out of the extensions to populate the canonical log.
+//!
+//! Layer wiring lives in `router::router` — see the comment block there.
+//! Both shims are no-ops when their counterpart is missing, so they can be
+//! rolled out / reverted independently without breaking requests.
+//!
+//! # Load-bearing axum invariant
+//!
+//! The metric only captures *true* permit-wait because each `Router::layer()`
+//! call in axum wraps the inner chain in a `Route` whose `poll_ready` returns
+//! `Poll::Ready(Ok(()))` unconditionally (see
+//! `axum::routing::route::Route::poll_ready`). That breaks the synchronous
+//! `poll_ready` cascade at every layer boundary, so
+//! `tower::limit::ConcurrencyLimit::poll_ready` (which is what blocks on the
+//! semaphore) does not run during the outer chain's readiness check — it
+//! runs inside `Route::call` *after* `record_concurrency_enter::call` has
+//! already stamped `Instant::now()`. If a future axum version changes
+//! `Route::poll_ready` to delegate to its inner service, this metric will
+//! silently report ~0 ms regardless of real wait. Re-verify against
+//! `axum::routing::route::Route::poll_ready` on every axum bump; a
+//! regression there will not surface in unit tests (the shim contract is
+//! decoupled from the cascade behavior) but will show up as the
+//! `flags_concurrency_limit_wait_ms` histogram collapsing to ~0 in
+//! production dashboards.
+
+use std::time::{Duration, Instant};
+
+use axum::{extract::Request, middleware::Next, response::Response};
+
+/// Wall-clock instant captured immediately before the request enters
+/// `ConcurrencyLimitLayer`. Read by [`record_concurrency_wait`] to compute
+/// permit-acquisition latency. `Copy` so the shim doesn't need to clone.
+#[derive(Clone, Copy, Debug)]
+pub struct ConcurrencyEnterTime(pub Instant);
+
+/// Time spent waiting on a permit from `ConcurrencyLimitLayer`. Inserted
+/// into the request extensions by [`record_concurrency_wait`] and read by
+/// the `flags` handler to populate `FlagsCanonicalLogLine::concurrency_limit_wait_ms`.
+#[derive(Clone, Copy, Debug)]
+pub struct ConcurrencyLimitWait(pub Duration);
+
+/// Stamps `ConcurrencyEnterTime` on the request before it reaches
+/// `ConcurrencyLimitLayer`. Wired between `TimeoutLayer` and the
+/// concurrency limiter so the captured instant happens *after* timeout
+/// deadline propagation but *before* permit acquisition.
+pub async fn record_concurrency_enter(mut req: Request, next: Next) -> Response {
+ req.extensions_mut()
+ .insert(ConcurrencyEnterTime(Instant::now()));
+ next.run(req).await
+}
+
+/// Runs *after* `ConcurrencyLimitLayer` has handed off a permit. Computes
+/// the elapsed permit-wait and stores it on the request extensions so the
+/// handler can pull it into the canonical log.
+///
+/// No-ops if `ConcurrencyEnterTime` is missing — keeps the two shims
+/// independently rollout-able and tolerant of layer-ordering mistakes
+/// during refactors.
+pub async fn record_concurrency_wait(mut req: Request, next: Next) -> Response {
+ if let Some(enter) = req.extensions().get::<ConcurrencyEnterTime>().copied() {
+ req.extensions_mut()
+ .insert(ConcurrencyLimitWait(enter.0.elapsed()));
+ }
+ next.run(req).await
+}
+
+#[cfg(test)]
+mod tests {
+ //! These tests exercise the middleware's contract directly — capture
+ //! `Instant` at `enter`, write `Duration` since at `wait` — without
+ //! depending on `ConcurrencyLimitLayer`'s real-runtime permit
+ //! semantics. That keeps tests deterministic. The router-level
+ //! interaction (this pair sandwiches `ConcurrencyLimitLayer`) is
+ //! enforced by layer ordering in `router::router`; we test the layer
+ //! contract here, not the layer placement.
+ use super::*;
+ use axum::{body::Body, extract::Extension, http::StatusCode, routing::get, Router};
+ use rstest::rstest;
+ use tower::ServiceExt;
+
+ /// Handler that echoes the captured wait as plain text. `"missing"`
+ /// distinguishes the "extension never set" case from a real `0 ms`.
+ async fn echo_wait_ms(wait: Option<Extension<ConcurrencyLimitWait>>) -> String {
+ wait.map(|Extension(w)| w.0.as_millis().to_string())
+ .unwrap_or_else(|| "missing".to_string())
+ }
+
+ async fn read_body(resp: Response) -> String {
+ let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ String::from_utf8(bytes.to_vec()).unwrap()
+ }
+
+ fn make_request() -> axum::http::Request<Body> {
+ axum::http::Request::builder()
+ .uri("/")
+ .body(Body::empty())
+ .unwrap()
+ }
+
+ #[tokio::test]
+ async fn enter_then_wait_records_elapsed_duration() {
+ // Insert a known sleep between `record_concurrency_enter` and
+ // `record_concurrency_wait`. The wait middleware must record at
+ // least that much elapsed — proving the extension flows and that
+ // any latency introduced between the shims is captured. In
+ // production the latency source is `ConcurrencyLimitLayer`;
+ // here it's a sleep, so the test is deterministic.
+ const DELAY_MS: u64 = 50;
+
+ let app = Router::new()
+ .route("/", get(echo_wait_ms))
+ .layer(axum::middleware::from_fn(record_concurrency_wait))
+ .layer(axum::middleware::from_fn(
+ |req: axum::extract::Request, next: axum::middleware::Next| async move {
+ tokio::time::sleep(Duration::from_millis(DELAY_MS)).await;
+ next.run(req).await
+ },
+ ))
+ .layer(axum::middleware::from_fn(record_concurrency_enter));
+
+ let resp = app.oneshot(make_request()).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body = read_body(resp).await;
+ let wait_ms: u128 = body
+ .parse()
+ .unwrap_or_else(|_| panic!("expected numeric wait, got {body:?}"));
+
+ // Allow scheduler jitter on the lower bound while still catching
+ // the "stuck at 0 / missing" regressions. Upper bound guards
+ // against runaway clock skew or accidental double-stamping.
+ let lower = (DELAY_MS - 10) as u128;
+ let upper = (DELAY_MS + 500) as u128;
+ assert!(
+ (lower..=upper).contains(&wait_ms),
+ "expected wait in [{lower}, {upper}] ms, got {wait_ms} ms"
+ );
+ }
+
+ /// Builds a router that only installs the named shim. The other shim
+ /// is intentionally absent so the test can verify each is independently
+ /// rollout-safe — neither panics, and the wait extension is only ever
+ /// written by `record_concurrency_wait`.
+ fn router_with_single_shim(shim: SingleShim) -> Router {
+ let r = Router::new().route("/", get(echo_wait_ms));
+ match shim {
+ SingleShim::WaitOnly => r.layer(axum::middleware::from_fn(record_concurrency_wait)),
+ SingleShim::EnterOnly => r.layer(axum::middleware::from_fn(record_concurrency_enter)),
+ }
+ }
+
+ #[derive(Copy, Clone)]
+ enum SingleShim {
+ /// Drops `record_concurrency_enter`. Verifies `record_concurrency_wait`
+ /// no-ops rather than panicking when the entry instant is missing.
+ WaitOnly,
+ /// Drops `record_concurrency_wait`. Verifies that no other code path
+ /// accidentally writes the wait extension — catches a future
+ /// regression where someone merges the two shims into one and
+ /// breaks the layer-ordering semantics.
+ EnterOnly,
+ }
+
+ #[rstest]
+ #[case::wait_shim_alone(SingleShim::WaitOnly)]
+ #[case::enter_shim_alone(SingleShim::EnterOnly)]
+ #[tokio::test]
+ async fn single_shim_leaves_wait_extension_absent(#[case] shim: SingleShim) {
+ let app = router_with_single_shim(shim);
+ let resp = app.oneshot(make_request()).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ let body = read_body(resp).await;
+ assert_eq!(body, "missing");
+ }
+}
diff --git a/rust/feature-flags/src/api/endpoint.rs b/rust/feature-flags/src/api/endpoint.rs
index c567954c0c1a..a4b9f455bd0d 100644
--- a/rust/feature-flags/src/api/endpoint.rs
+++ b/rust/feature-flags/src/api/endpoint.rs
@@ -1,5 +1,6 @@
use crate::{
api::{
+ concurrency_metrics::ConcurrencyLimitWait,
errors::{ClientFacingError, FlagError},
flags_rate_limiter::RateLimitResult,
types::{
@@ -15,7 +16,7 @@ use crate::{
utils::user_agent::UserAgentInfo,
};
// TODO: stream this instead
-use axum::extract::{MatchedPath, Query, State};
+use axum::extract::{Extension, MatchedPath, Query, State};
use axum::http::{HeaderMap, HeaderValue, Method, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::{debug_handler, Json};
@@ -199,10 +200,15 @@ fn get_versioned_response(
/// Feature flag evaluation endpoint.
/// Only supports a specific shape of data, and rejects any malformed data.
#[debug_handler]
+#[allow(clippy::too_many_arguments)]
pub async fn flags(
state: State<router::State>,
InsecureClientIp(direct_ip): InsecureClientIp,
Query(query_params): Query<FlagsQueryParams>,
+ // Populated by the `record_concurrency_wait` middleware after
+ // `ConcurrencyLimitLayer` hands off a permit. Optional so the handler
+ // tolerates the layer pair being removed or temporarily disabled.
+ concurrency_wait: Option<Extension<ConcurrencyLimitWait>>,
headers: HeaderMap,
method: Method,
path: MatchedPath,
@@ -280,6 +286,12 @@ pub async fn flags(
.map(|start_ms| now_ms - start_ms)
.filter(|&delta| delta >= 0);
+ // Concurrency-limit permit-wait, captured by the `record_concurrency_wait`
+ // middleware. `as_millis()` returns `u128`; the `as u64` cast truncates
+ // above `u64::MAX` ms — irrelevant in practice since reaching that bound
+ // would take longer than the age of the universe.
+ let concurrency_limit_wait_ms = concurrency_wait.map(|Extension(w)| w.0.as_millis() as u64);
+
// Initialize canonical log with all upfront request metadata.
// Fields discovered during processing (team_id, flags_evaluated, etc.) are set via with_canonical_log().
let canonical_log = FlagsCanonicalLogLine {
@@ -291,6 +303,7 @@ pub async fn flags(
lib_version: query_params.lib_version.clone().or(ua_info.sdk_version),
api_version: query_params.version.clone(),
queue_time_ms,
+ concurrency_limit_wait_ms,
..Default::default()
};
diff --git a/rust/feature-flags/src/api/mod.rs b/rust/feature-flags/src/api/mod.rs
index 0c1941dce5ef..339b38d92f60 100644
--- a/rust/feature-flags/src/api/mod.rs
+++ b/rust/feature-flags/src/api/mod.rs
@@ -1,4 +1,5 @@
pub mod auth;
+pub mod concurrency_metrics;
pub mod endpoint;
pub mod errors;
pub mod flag_definitions;
diff --git a/rust/feature-flags/src/router.rs b/rust/feature-flags/src/router.rs
index ec5455dc8bd7..3bc4a49f3f82 100644
--- a/rust/feature-flags/src/router.rs
+++ b/rust/feature-flags/src/router.rs
@@ -33,6 +33,7 @@ use tower_http::{
use crate::{
api::{
+ concurrency_metrics::{record_concurrency_enter, record_concurrency_wait},
endpoint, flag_definitions,
flag_definitions_rate_limiter::FlagDefinitionsRateLimiter,
flags_rate_limiter::{FlagsRateLimiter, IpRateLimiter},
@@ -261,7 +262,19 @@ pub fn router(
// 1. HandleErrorLayer (outermost): converts timeout errors into 503 responses.
// 2. TimeoutLayer: cancels the entire request after request_timeout_ms,
// ensuring zombie tasks don't hold connections after Envoy kills the downstream.
- // 3. ConcurrencyLimitLayer (innermost): bounds in-flight requests.
+ // 3. record_concurrency_enter: stamps the request with `Instant::now()`
+ // just before permit acquisition. Inside `TimeoutLayer` so timeouts
+ // still fire while the request is parked at the limiter.
+ // 4. ConcurrencyLimitLayer: bounds in-flight requests.
+ // 5. record_concurrency_wait (innermost): computes elapsed permit-wait
+ // immediately after the layer hands off a permit, before the handler.
+ //
+ // Note: this entire chain wraps both `/flags|/decide` AND
+ // `/flags/definitions|/api/feature_flag/local_evaluation`. The shim pair
+ // is harmless on definitions (the handler never extracts the wait
+ // extension), but does add ~one extension-insert + one extension-read
+ // per definitions request. If that overhead ever becomes load-bearing,
+ // scope the shims to `/flags|/decide` via a dedicated sub-router.
let mut flags_router = Router::new();
if matches!(config.service_mode, ServiceMode::All | ServiceMode::Flags) {
@@ -298,7 +311,15 @@ pub fn router(
}
let flags_router = flags_router
+ // Innermost: runs *after* `ConcurrencyLimitLayer` releases a
+ // permit, so it can compute the elapsed permit-wait before the
+ // handler runs.
+ .layer(axum::middleware::from_fn(record_concurrency_wait))
.layer(ConcurrencyLimitLayer::new(config.max_concurrency))
+ // Sandwiched between `TimeoutLayer` and `ConcurrencyLimitLayer`:
+ // captures `Instant::now()` *after* timeout deadline propagation
+ // but *before* permit acquisition.
+ .layer(axum::middleware::from_fn(record_concurrency_enter))
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(|err: tower::BoxError| async move {