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.

feat(flags): evolve cache instrumentation (#58878)

Matheus Batista75d75465bb65
diff --git a/rust/common/hypercache/src/lib.rs b/rust/common/hypercache/src/lib.rs
index 6b07581ad451..4cd9a8f3afee 100644
--- a/rust/common/hypercache/src/lib.rs
+++ b/rust/common/hypercache/src/lib.rs
@@ -32,7 +32,7 @@ pub mod writer;
use anyhow::Result;
use aws_config::BehaviorVersion;
use aws_sdk_s3::Client as AwsS3SdkClient;
-use common_metrics::inc;
+use common_metrics::{histogram, inc};
use common_redis::Client as RedisClient;
#[cfg(all(test, feature = "mock-client"))]
use common_s3::MockS3Client;
@@ -44,7 +44,7 @@ use serde::de::DeserializeOwned;
use serde_json::Value;
use std::fmt::{self, Display};
use std::sync::Arc;
-use std::time::Duration;
+use std::time::{Duration, Instant};
use thiserror::Error;
use tokio::time::timeout;
use tracing::debug;
@@ -55,6 +55,14 @@ pub const HYPERCACHE_COUNTER_NAME: &str = "posthog_hypercache_get_from_cache";
/// Metric name for tracking Redis failure reasons (timeout, get_error, pickle_error, json_error)
const HYPERCACHE_REDIS_MISS_REASON_COUNTER_NAME: &str = "posthog_hypercache_redis_miss_reason";
+/// Per-tier latency histogram for the Redis read inside `get_typed_with_source`.
+/// Labels: `namespace`, `value`, `outcome` (hit | miss | timeout | error).
+pub const HYPERCACHE_REDIS_OP_MS: &str = "posthog_hypercache_redis_op_ms";
+
+/// Per-tier latency histogram for the S3 read inside `get_typed_with_source`.
+/// Labels: `namespace`, `value`, `outcome` (hit | miss | timeout | error).
+pub const HYPERCACHE_S3_OP_MS: &str = "posthog_hypercache_s3_op_ms";
+
/// Tombstone metric for tracking "impossible" failures that should never happen in production.
/// This is duplicated from feature_flags::metrics::consts::TOMBSTONE_COUNTER because hypercache
/// is a standalone crate that shouldn't depend on feature-flags.
@@ -477,12 +485,28 @@ impl HyperCacheReader {
let mut infra_error: Option<HyperCacheError> = None;
// Try Redis first
- match timeout(
+ let redis_start = Instant::now();
+ let redis_result = timeout(
self.config.redis_timeout,
self.try_get_typed_from_redis::<T>(&redis_cache_key),
)
- .await
- {
+ .await;
+ let redis_outcome = match &redis_result {
+ Ok(Ok(_)) => "hit",
+ Ok(Err(HyperCacheError::CacheMiss)) => "miss",
+ Ok(Err(_)) => "error",
+ Err(_) => "timeout",
+ };
+ histogram(
+ HYPERCACHE_REDIS_OP_MS,
+ &[
+ ("namespace".to_string(), self.config.namespace.clone()),
+ ("value".to_string(), self.config.object_name.clone()),
+ ("outcome".to_string(), redis_outcome.to_string()),
+ ],
+ redis_start.elapsed().as_secs_f64() * 1000.0,
+ );
+ match redis_result {
Ok(Ok(Some(data))) => {
debug!(
cache_key = %redis_cache_key,
@@ -551,12 +575,28 @@ impl HyperCacheReader {
// Try S3 fallback
let s3_cache_key = self.config.get_s3_cache_key(key);
- match timeout(
+ let s3_start = Instant::now();
+ let s3_result = timeout(
self.config.s3_timeout,
self.try_get_typed_from_s3::<T>(&s3_cache_key),
)
- .await
- {
+ .await;
+ let s3_outcome = match &s3_result {
+ Ok(Ok(_)) => "hit",
+ Ok(Err(HyperCacheError::S3(S3Error::NotFound(_)))) => "miss",
+ Ok(Err(_)) => "error",
+ Err(_) => "timeout",
+ };
+ histogram(
+ HYPERCACHE_S3_OP_MS,
+ &[
+ ("namespace".to_string(), self.config.namespace.clone()),
+ ("value".to_string(), self.config.object_name.clone()),
+ ("outcome".to_string(), s3_outcome.to_string()),
+ ],
+ s3_start.elapsed().as_secs_f64() * 1000.0,
+ );
+ match s3_result {
Ok(Ok(data)) => {
debug!(
cache_key = %s3_cache_key,
diff --git a/rust/feature-flags/src/metrics/buckets.rs b/rust/feature-flags/src/metrics/buckets.rs
index 83b559f4d1ac..04eac71f7850 100644
--- a/rust/feature-flags/src/metrics/buckets.rs
+++ b/rust/feature-flags/src/metrics/buckets.rs
@@ -60,6 +60,16 @@ const INMEM_LOAD_BUCKETS_MS: &[f64] = &[
0.05, 0.1, 0.5, 1.0, 2.5, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0, 5000.0, 10000.0,
];
+// HyperCache S3 read. Network-bound: warm S3 reads sit in the 5-50ms
+// band, so the sub-ms buckets that `INMEM_LOAD_BUCKETS_MS` carries for
+// Redis would be permanently empty here. 1ms floor still captures
+// immediate errors (short-circuit before any network call) without
+// losing them in an unbounded `<le_inf>` bucket. 10s ceiling covers the
+// 3s `s3_timeout` plus headroom for the post-timeout emission.
+const S3_OP_BUCKETS_MS: &[f64] = &[
+ 1.0, 2.5, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0, 2500.0, 5000.0, 10000.0,
+];
+
/// Returns the bucket-override matrix for the feature-flags recorder.
///
/// `Matcher::Suffix` is used for `_queue_time_ms` / `_pre_handler_time_ms`
@@ -106,5 +116,13 @@ pub fn bucket_overrides() -> Vec<(Matcher, &'static [f64])> {
Matcher::Full("flags_definitions_inmem_load_ms".into()),
INMEM_LOAD_BUCKETS_MS,
),
+ (
+ Matcher::Full("posthog_hypercache_redis_op_ms".into()),
+ INMEM_LOAD_BUCKETS_MS,
+ ),
+ (
+ Matcher::Full("posthog_hypercache_s3_op_ms".into()),
+ S3_OP_BUCKETS_MS,
+ ),
]
}