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(data-warehouse): add observability logs to repartition activity (#67900)

Tom Owersb413b4c2a573
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition_controller.py
index 29e83004339d..eb60ef2476df 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition_controller.py
@@ -124,12 +124,18 @@ async def maybe_flag_for_repartition(
job: ExternalDataJob,
delta_table: deltalake.DeltaTable,
logger: FilteringBoundLogger,
+ *,
+ enabled: bool | None = None,
) -> None:
"""Measure partition sizes and, if over budget, record a `repartition_pending` target.
Always records `max_partition_bytes` for observability (even when the controller is disabled or in
cooldown). Setting the pending target is gated by the feature flag; the rewrite itself happens on
the next run. Never raises — detection must not break post-load.
+
+ Pass `enabled` when the caller has already evaluated the rollout flag for this schema (each
+ evaluation is a `Team.objects.get()` plus a PostHog API call) to avoid re-evaluating it here; when
+ omitted it is evaluated lazily, only once the table is confirmed over budget.
"""
try:
partition_bytes = await asyncio.to_thread(measure_partition_bytes, delta_table)
@@ -153,7 +159,9 @@ async def maybe_flag_for_repartition(
)
return
- if not await asyncio.to_thread(is_auto_repartition_enabled, schema):
+ if enabled is None:
+ enabled = await asyncio.to_thread(is_auto_repartition_enabled, schema)
+ if not enabled:
await logger.adebug(
"repartition: over budget but skipped, controller disabled by feature flag",
schema_id=str(schema.id),
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition_controller.py
index b70a856ef915..63cda2422e5e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition_controller.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition_controller.py
@@ -213,7 +213,9 @@ class TestRepartitionActivity:
patch.object(repartition_table, "capture_repartition_event") as capture,
patch.object(repartition_table.DeltaTableHelper, "get_delta_table", new=AsyncMock(return_value=delta)),
patch.object(ctrl, "target_partition_bytes", return_value=1),
- patch.object(ctrl, "is_auto_repartition_enabled", return_value=True),
+ # The activity evaluates the rollout flag once and threads the verdict into detection,
+ # so patch the binding the activity reads from (not the controller's).
+ patch.object(repartition_table, "is_auto_repartition_enabled", return_value=True),
patch.object(ctrl, "capture_repartition_event"),
):
ActivityEnvironment().run(maybe_repartition_table_activity, self._inputs(team, schema))
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
index eebc644626a1..c2b6d1812592 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
@@ -13,7 +13,6 @@ from typing import Any
from django.db import close_old_connections
-import structlog
from asgiref.sync import async_to_sync
from structlog.contextvars import bind_contextvars
from structlog.types import FilteringBoundLogger
@@ -21,6 +20,7 @@ from temporalio import activity
from posthog.exceptions_capture import capture_exception
from posthog.temporal.common.heartbeat_sync import HeartbeaterSync
+from posthog.temporal.common.logger import get_logger
from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob
from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema
@@ -34,8 +34,10 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.repartition_controller import (
MAX_REPARTITION_ATTEMPTS,
+ WAREHOUSE_AUTO_REPARTITION_FLAG,
base_event_props,
capture_repartition_event,
+ is_auto_repartition_enabled,
maybe_flag_for_repartition,
target_partition_bytes,
)
@@ -44,7 +46,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
DELTA_REPARTITION_TOTAL,
)
-LOGGER = structlog.get_logger(__name__)
+LOGGER = get_logger(__name__)
@dataclasses.dataclass
@@ -87,6 +89,7 @@ def _maybe_flag_pre_extraction(
job: ExternalDataJob,
helper: DeltaTableHelper,
logger: FilteringBoundLogger,
+ enabled: bool,
) -> dict[str, Any] | None:
"""Measure the on-disk table before extraction and flag a repartition if it's over budget.
@@ -96,13 +99,16 @@ def _maybe_flag_pre_extraction(
that gap: the on-disk table already reflects the over-budget layout, so we can flag and — in this
same run — rewrite it before the merge that would OOM. Returns the pending target set by detection,
or None if nothing was flagged (or the table couldn't be measured). Never raises.
+
+ `enabled` is the already-evaluated rollout-flag verdict, threaded through so detection reuses it
+ instead of paying for a second flag evaluation.
"""
try:
delta_table = async_to_sync(helper.get_delta_table)()
if delta_table is None:
logger.debug("repartition: no delta table on disk, cannot measure for repartition")
return None
- async_to_sync(maybe_flag_for_repartition)(schema, schema.source, job, delta_table, logger)
+ async_to_sync(maybe_flag_for_repartition)(schema, schema.source, job, delta_table, logger, enabled=enabled)
except Exception as e:
# Detection is best-effort; a failure here must not block the sync.
logger.warning("repartition: pre-extraction detection failed", exc_info=True)
@@ -119,19 +125,44 @@ def maybe_repartition_table_activity(inputs: RepartitionActivityInputs) -> None:
logger = LOGGER.bind()
close_old_connections()
+ # Always bracket the run with start/finish INFO lines so the Syncs UI shows the activity ran even
+ # on the healthy no-op path (which otherwise only logs at DEBUG). The finally guarantees the finish
+ # line regardless of which branch returns, including swallowed failures.
+ logger.info("repartition: activity started", job_id=inputs.job_id, source_id=inputs.source_id)
+ try:
+ _maybe_repartition_table(inputs, logger)
+ finally:
+ logger.info("repartition: activity finished")
+
+
+def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: FilteringBoundLogger) -> None:
try:
schema = ExternalDataSchema.objects.select_related("source").get(id=inputs.schema_id)
except ExternalDataSchema.DoesNotExist:
logger.warning("repartition: schema not found, skipping activity", schema_id=inputs.schema_id)
return
+ # Log the rollout-flag verdict (and the sizes that gate detection) so it's clear from the Syncs UI
+ # why a table does or doesn't repartition — a disabled flag is the most common reason for a no-op.
+ # Evaluate once here and thread the result into the pre-extraction detection path so the flag
+ # (a Team DB query plus a PostHog API call) isn't re-evaluated inside maybe_flag_for_repartition.
+ enabled = is_auto_repartition_enabled(schema)
+ logger.info(
+ "repartition: feature flag evaluated",
+ flag=WAREHOUSE_AUTO_REPARTITION_FLAG,
+ enabled=enabled,
+ max_partition_bytes=schema.max_partition_bytes,
+ target_partition_bytes=target_partition_bytes(),
+ )
+
pending = schema.repartition_pending
swap = schema.repartition_swap
# Fast no-op path: nothing queued and the in-memory gate says no on-disk measurement is needed.
- # Return here — before fetching the job — so the common healthy invocation stays a single DB query.
+ # Return here — before fetching the job and reading the delta log — so the common healthy
+ # invocation avoids all on-disk I/O.
if pending is None and swap is None and not _needs_pre_extraction_detection(schema):
- logger.debug("repartition: nothing queued and no detection needed, nothing to do")
+ logger.info("repartition: nothing queued and no detection needed, nothing to do")
return
try:
@@ -146,7 +177,7 @@ def maybe_repartition_table_activity(inputs: RepartitionActivityInputs) -> None:
# Nothing was queued by a prior run's post-load detection, but the gate flagged the table for an
# on-disk measurement. Measure now and self-flag if it's over budget — the only path that can
# rescue a table which OOMs its merge every run (and so never reaches post-load detection).
- pending = _maybe_flag_pre_extraction(schema, job, helper, logger)
+ pending = _maybe_flag_pre_extraction(schema, job, helper, logger, enabled)
if pending is None:
logger.debug("repartition: pre-extraction measurement found no repartition needed")
return