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.

fix(capture): clamp future timestamps when skew correction is disabled (#127067)

Eli Reisman38ff65ac733d
diff --git a/nodejs/src/ingestion/common/timestamps.ts b/nodejs/src/ingestion/common/timestamps.ts
index c5e722a9989e..56e87e0a3cc1 100644
--- a/nodejs/src/ingestion/common/timestamps.ts
+++ b/nodejs/src/ingestion/common/timestamps.ts
@@ -12,6 +12,7 @@ type IngestionWarningCallback = (type: IngestionWarningType, details: Record<str
* is now handled in the Rust capture service. This function only parses the timestamp string
* that comes from the event data. The timestamp in event data is already normalized by the Rust
* capture service (via parse_event_timestamp in rust/common/types/src/timestamp.rs).
+ * Capture v1 applies skew correction and the future clamp in normalize_timestamp in rust/capture/src/v1/analytics/process.rs.
*
* The Rust capture service handles:
* - Clock skew adjustment using sent_at and now
diff --git a/rust/capture/src/v1/analytics/constants.rs b/rust/capture/src/v1/analytics/constants.rs
index c42f263231c2..aafd144d00a2 100644
--- a/rust/capture/src/v1/analytics/constants.rs
+++ b/rust/capture/src/v1/analytics/constants.rs
@@ -134,8 +134,8 @@ pub(super) const ILLEGAL_DISTINCT_IDS: &[&str] = &[
// Timestamp normalization
// ---------------------------------------------------------------------------
-/// Events whose skew-adjusted timestamp is more than 23 hours in the future
-/// (in milliseconds) are clamped to server `now`.
+/// Events whose timestamp, after any skew correction, is more than 23 hours in
+/// the future (in milliseconds) are clamped to server `now`.
pub(super) const FUTURE_EVENT_HOURS_CUTOFF_MS: i64 = 23 * 3600 * 1000;
/// Counter for adjustments applied to accepted events (label: reason).
diff --git a/rust/capture/src/v1/analytics/process.rs b/rust/capture/src/v1/analytics/process.rs
index 44d70d5d736e..5335e65a0035 100644
--- a/rust/capture/src/v1/analytics/process.rs
+++ b/rust/capture/src/v1/analytics/process.rs
@@ -745,11 +745,11 @@ fn normalize_timestamp(
disable_skew_correction: bool,
raw_event_ts: DateTime<Utc>,
) -> DateTime<Utc> {
- if disable_skew_correction {
- return raw_event_ts;
- }
-
- let adjusted = raw_event_ts - context.clock_skew();
+ let adjusted = if disable_skew_correction {
+ raw_event_ts
+ } else {
+ raw_event_ts - context.clock_skew()
+ };
let now = context.server_received_at;
if adjusted.signed_duration_since(now).num_milliseconds() > FUTURE_EVENT_HOURS_CUTOFF_MS {
metrics::counter!(CAPTURE_V1_EVENT_ADJUSTMENTS_APPLIED, "reason" => "future_timestamp_clamp")
@@ -2003,31 +2003,61 @@ mod tests {
assert_eq!(result, dt("2026-03-19T11:00:10Z"));
}
- #[test]
- fn normalize_clamps_far_future() {
- let now = dt("2026-03-19T12:00:00Z");
- let ctx = ctx_with_skew(now, Duration::zero());
- let event_ts = dt("2026-03-21T12:00:00Z");
- let result = normalize_timestamp(&ctx, false, event_ts);
- assert_eq!(result, now);
- }
-
- #[test]
- fn normalize_allows_near_future() {
- let now = dt("2026-03-19T12:00:00Z");
- let ctx = ctx_with_skew(now, Duration::zero());
- let event_ts = dt("2026-03-20T10:00:00Z");
- let result = normalize_timestamp(&ctx, false, event_ts);
- assert_eq!(result, event_ts);
- }
-
- #[test]
- fn normalize_disable_skew_correction_skips_adjustment() {
+ #[rstest]
+ #[case::clamps_far_future(false, Duration::zero(), Duration::days(2), Duration::zero())]
+ #[case::allows_near_future(false, Duration::zero(), Duration::hours(22), Duration::hours(22))]
+ #[case::clamps_skew_adjusted_value(
+ false,
+ Duration::hours(-1),
+ Duration::minutes(22 * 60 + 30),
+ Duration::zero()
+ )]
+ #[case::disabled_skips_skew_for_past(
+ true,
+ Duration::seconds(10),
+ Duration::hours(-1),
+ Duration::hours(-1)
+ )]
+ #[case::disabled_skips_skew_for_near_future(
+ true,
+ Duration::seconds(10),
+ Duration::hours(22),
+ Duration::hours(22)
+ )]
+ #[case::disabled_compares_raw_value_to_cutoff(
+ true,
+ Duration::hours(-1),
+ Duration::minutes(22 * 60 + 30),
+ Duration::minutes(22 * 60 + 30)
+ )]
+ #[case::disabled_clamps_far_future(
+ true,
+ Duration::zero(),
+ Duration::days(30),
+ Duration::zero()
+ )]
+ #[case::disabled_allows_exact_cutoff(
+ true,
+ Duration::zero(),
+ Duration::hours(23),
+ Duration::hours(23)
+ )]
+ #[case::disabled_clamps_just_past_cutoff(
+ true,
+ Duration::zero(),
+ Duration::hours(23) + Duration::milliseconds(1),
+ Duration::zero()
+ )]
+ fn normalize_future_clamp(
+ #[case] disable_skew_correction: bool,
+ #[case] skew: Duration,
+ #[case] event_offset: Duration,
+ #[case] expected_offset: Duration,
+ ) {
let now = dt("2026-03-19T12:00:00Z");
- let ctx = ctx_with_skew(now, Duration::seconds(10));
- let event_ts = dt("2026-03-19T11:00:00Z");
- let result = normalize_timestamp(&ctx, true, event_ts);
- assert_eq!(result, event_ts);
+ let ctx = ctx_with_skew(now, skew);
+ let result = normalize_timestamp(&ctx, disable_skew_correction, now + event_offset);
+ assert_eq!(result, now + expected_offset);
}
#[test]