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(hogbox-preview): let feature-flags read warm caches instead of falling back (#113896)

Frank Hamand2502eaa4661f
diff --git a/tools/hogbox-preview/README.md b/tools/hogbox-preview/README.md
index a1a4f9920227..8e129471cfa5 100644
--- a/tools/hogbox-preview/README.md
+++ b/tools/hogbox-preview/README.md
@@ -50,6 +50,7 @@ print(url) # https://pen-….boxes.hogland.prod-us.posthog.dev/ (stable across
5. **Sync HogFunction templates** - start the CDP service and load destination
templates into the restored database before the preview becomes available.
6. **Sync feature flags** - run `sync_feature_flags` so each flag in the PR's `constants.tsx` that the golden lacks exists and is on.
+ Then `warm_team_metadata_cache` and `warm_flags_cache` fill the flags Redis (`redis7` database 1), which `feature-flags` reads. Django and `feature-flags` share `FLAGS_REDIS_URL`, and `feature-flags` uses the stack's object storage for cache misses.
A celery `worker` runs in the preview, without the scheduler, so tasks such as the flag cache refresh that a flag save queues actually run.
7. **Serve + report** — the box is HTTP-exposed; the URL is posted to the PR.
A Caddy container (`static-proxy`) takes the exposed port.
diff --git a/tools/hogbox-preview/hogbox_preview/stack.py b/tools/hogbox-preview/hogbox_preview/stack.py
index e27762c7ec37..b55cb007254a 100644
--- a/tools/hogbox-preview/hogbox_preview/stack.py
+++ b/tools/hogbox-preview/hogbox_preview/stack.py
@@ -216,6 +216,7 @@ class PostHogPreviewStack:
TELEMETRY_SERVICES = ["capture-logs", "ingestion-logs", "ingestion-traces", "ingestion-metrics", "otel-collector"]
CELERY_SERVICES = ["worker"]
CELERY_CONCURRENCY = 2
+ FLAGS_REDIS_URL = "redis://redis7:6379/1"
REPO_DIR = "/home/hog/posthog"
COMPOSE = "docker-compose.dev-full.yml"
OVERRIDE = "docker-compose.preview.yml"
@@ -335,6 +336,10 @@ class PostHogPreviewStack:
self.generate_demo_data()
except Exception as e: # noqa: BLE001
sys.stderr.write(f"[hogbox-preview] demo-data seeding skipped (preview still usable): {e}\n")
+ try:
+ self.warm_flag_caches()
+ except Exception as e: # noqa: BLE001
+ sys.stderr.write(f"[hogbox-preview] flag cache warm skipped (preview still usable): {e}\n")
if self.frontend_dist_tar:
# Serve the PR's frontend (else it's the golden's :master SPA). Must
# run before up_web so the fresh web container reads the new index
@@ -545,6 +550,7 @@ class PostHogPreviewStack:
" - ADMIN_PORTAL_ENABLED=1",
" - SELF_CAPTURE=1",
" - SELF_CAPTURE_HOST=http://static-proxy:8000",
+ f" - FLAGS_REDIS_URL={self.FLAGS_REDIS_URL}",
" - OTEL_SERVICE_NAME=posthog-web",
*_OTEL_ENV,
]
@@ -697,6 +703,7 @@ class PostHogPreviewStack:
# doesn't start. Values mirror the bake.
" - TEMPORAL_HEALTH_PORT=7999",
" - TEMPORAL_HEALTH_MAX_IDLE_SECONDS=86400",
+ f" - FLAGS_REDIS_URL={self.FLAGS_REDIS_URL}",
" - OTEL_SERVICE_NAME=posthog-temporal-worker",
" - TEMPORAL_OTEL_PLUGIN_ENABLED=true",
*_OTEL_ENV,
@@ -723,12 +730,20 @@ class PostHogPreviewStack:
" - SELF_CAPTURE=1",
" - SELF_CAPTURE_HOST=http://static-proxy:8000",
f" - WEB_CONCURRENCY={self.CELERY_CONCURRENCY}",
+ f" - FLAGS_REDIS_URL={self.FLAGS_REDIS_URL}",
" - OTEL_SERVICE_NAME=posthog-celery-worker",
*_OTEL_ENV,
]
def _self_capture_services(self) -> list[str]:
return [
+ " feature-flags:",
+ " environment:",
+ f" - FLAGS_REDIS_URL={self.FLAGS_REDIS_URL}",
+ " - REDIS_COMPRESSION_ENABLED=true",
+ " - OBJECT_STORAGE_ENDPOINT=http://objectstorage:19000",
+ " - AWS_ACCESS_KEY_ID=object_storage_root_user",
+ " - AWS_SECRET_ACCESS_KEY=object_storage_root_password",
" ingestion-general:",
" extends:",
" file: docker-compose.base.yml",
@@ -977,6 +992,15 @@ class PostHogPreviewStack:
timeout=600,
)
+ def warm_flag_caches(self) -> None:
+ timing.stage("warm team and flag caches")
+ commands = ("warm_team_metadata_cache", "warm_flags_cache")
+ self.backend.run_long(
+ " && ".join(self._compose(f"run --rm -T web python manage.py {command}") for command in commands),
+ name="warm-flag-caches",
+ timeout=600,
+ )
+
def generate_demo_data(self) -> None:
# Same command hobby-ci uses (bin/hobby-ci.py). Seeds a demo org + the
# [email protected] / 12345678 login so the preview opens populated.
diff --git a/tools/hogbox-preview/tests/test_deep_health.py b/tools/hogbox-preview/tests/test_deep_health.py
index 7705b4a4a451..df09786bdd79 100644
--- a/tools/hogbox-preview/tests/test_deep_health.py
+++ b/tools/hogbox-preview/tests/test_deep_health.py
@@ -143,6 +143,13 @@ class OverrideTemporalParityTest(unittest.TestCase):
override = backend.files[f"{stack.repo_dir}/{stack.OVERRIDE}"]
self.assertEqual(override.count(f"SECRET_KEY={stack.secret_key}"), 3)
+ def test_feature_flags_reads_the_caches_django_writes(self):
+ override = self._override()
+ flags_block = override.split(" feature-flags:", 1)[1].split(" ingestion-general:", 1)[0]
+ self.assertIn(f"FLAGS_REDIS_URL={PostHogPreviewStack.FLAGS_REDIS_URL}", flags_block)
+ self.assertIn("OBJECT_STORAGE_ENDPOINT=http://objectstorage:19000", flags_block)
+ self.assertEqual(override.count(f"FLAGS_REDIS_URL={PostHogPreviewStack.FLAGS_REDIS_URL}"), 4)
+
def test_worker_pins_the_image(self):
# dev-full's worker carries `build: .` — an unpinned image turns
# `up --no-build` into the 20-min build.
@@ -255,6 +262,7 @@ class TemplateSyncTest(unittest.TestCase):
"start_cdp_service",
"sync_hog_function_templates",
"sync_feature_flags",
+ "warm_flag_caches",
"up_web",
"wait_for_health",
"deep_health",
@@ -270,7 +278,8 @@ class TemplateSyncTest(unittest.TestCase):
self.assertLess(events.index("migrate"), events.index("start_cdp_service"))
self.assertLess(events.index("start_cdp_service"), events.index("sync_hog_function_templates"))
self.assertLess(events.index("sync_hog_function_templates"), events.index("up_web"))
- self.assertLess(events.index("sync_feature_flags"), events.index("up_web"))
+ self.assertLess(events.index("sync_feature_flags"), events.index("warm_flag_caches"))
+ self.assertLess(events.index("warm_flag_caches"), events.index("up_web"))
def test_cdp_service_uses_the_published_image_configuration(self):
backend = _RecordingBackend()