Skip to content

feat: start time travel at inbound request entry via x-td-root-timestamp header#62

Closed
jy-tan wants to merge 1 commit intomainfrom
inbound-request-time-travel
Closed

feat: start time travel at inbound request entry via x-td-root-timestamp header#62
jy-tan wants to merge 1 commit intomainfrom
inbound-request-time-travel

Conversation

@jy-tan
Copy link
Contributor

@jy-tan jy-tan commented Feb 5, 2026

Summary

Enable deterministic time travel at the very start of an inbound replay request by reading a new x-td-root-timestamp HTTP header sent by the CLI. This fixes replay deviations caused by time-sensitive code executing before the first outbound span is matched - most notably django-cachalot, which incorporates timezone.now() into Redis cache keys.

Previously, time travel was only activated per-outbound-span (when a mock was matched). This meant any time-dependent logic that ran before the first outbound call (e.g., cache key generation, session expiry checks) would use wall-clock time instead of the recorded time, causing cache key mismatches and divergent code paths during replay.

Changes

  • drift/instrumentation/django/middleware.py: Read HTTP_X_TD_ROOT_TIMESTAMP from Django's META headers and call start_time_travel() immediately in _handle_replay_request, before any downstream middleware or view code runs.
  • drift/instrumentation/wsgi/handler.py: Read x-td-root-timestamp from WSGI environ headers and call start_time_travel() at the start of _handle_replay_request.
  • drift/instrumentation/fastapi/instrumentation.py: Read x-td-root-timestamp from ASGI headers and call start_time_travel() at the start of _handle_replay_request.

Design notes

  • The header is optional and best-effort; if the CLI does not send it (older CLI versions), time travel falls back to the existing per-outbound-span mechanism with no behavioral change.
  • Django mangles headers into HTTP_X_TD_ROOT_TIMESTAMP format, so the Django middleware reads from http_x_td_root_timestamp in the lowercased headers dict, while WSGI/FastAPI use the standard x-td-root-timestamp.
  • This is a companion change to the CLI-side PR that adds the x-td-root-timestamp header to replay requests.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

@jy-tan
Copy link
Contributor Author

jy-tan commented Feb 6, 2026

Not needed anymore since we use root span timestamp for SendSetTimeTravel gRPC call on the CLI side. See Use-Tusk/tusk-drift-cli#154.

@jy-tan jy-tan closed this Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants