Skip to content

Trace-native CI/CD for AI agents — production failures become regression tests that block the PR. Auto-detect, cluster, freeze into hermetic cases, replay in CI for $0.

feat: expire raw OTLP bodies after 90 days

Wuthrich Julien2c5177fbd54b
diff --git a/backend/tests/test_purge_service.py b/backend/tests/test_purge_service.py
index 6fbaa78..63a164a 100644
--- a/backend/tests/test_purge_service.py
+++ b/backend/tests/test_purge_service.py
@@ -237,3 +237,37 @@ def test_sweep_handles_naive_clickhouse_timestamps(world):
"""ClickHouse hands back naive UTC; comparing one with an aware `now` raises."""
world(storage=set(), last_write={"gone": (NOW - timedelta(days=30)).replace(tzinfo=None)}, registry={"x"})
assert purge_service.purge_orphans(now=NOW)["purged"] == ["gone"]
+
+
+# ── 90-day expiry of raw OTLP bodies ──────────────────────────────────────────
+
+
+def test_expiry_takes_only_old_otlp_bodies(bucket):
+ """Past the 90-day horizon the tables already enforce, a raw body is bytes nothing can reach.
+ But case fixtures and artifacts must outlive their source trace — a promoted regression case
+ keeps running after the trace it came from has expired — and chat attachments aren't traces."""
+ ancient = NOW - timedelta(days=91)
+ recent = NOW - timedelta(days=89)
+ b = bucket({
+ "events/p1/otlp/old.json": ancient,
+ "events/p1/otlp/new.json": recent,
+ "events/p2/otlp/old.pb": ancient,
+ "events/fixtures/p1/bundle.json": ancient,
+ "events/cases/p1/artifact.json": ancient,
+ "events/p1/assistant/chat.png": ancient,
+ })
+ out = purge_service.expire_otlp_blobs(now=NOW)
+ assert out["expired"] == 2
+ assert set(b.objects) == {
+ "events/p1/otlp/new.json",
+ "events/fixtures/p1/bundle.json",
+ "events/cases/p1/artifact.json",
+ "events/p1/assistant/chat.png",
+ }
+
+
+def test_expiry_is_scheduled_nightly():
+ from tracely.infrastructure.queue.celery_app import celery_app
+
+ entry = celery_app.conf.beat_schedule["tracely.expire_otlp_blobs-nightly"]
+ assert entry["task"] == "tracely.expire_otlp_blobs"
diff --git a/backend/tracely/infrastructure/blob/s3.py b/backend/tracely/infrastructure/blob/s3.py
index 5436624..4a76392 100644
--- a/backend/tracely/infrastructure/blob/s3.py
+++ b/backend/tracely/infrastructure/blob/s3.py
@@ -126,6 +126,13 @@ def project_ids_in_storage() -> set[str]:
return found
+def delete_otlp_blobs(project_id: str, before: datetime) -> int:
+ """Delete one project's raw OTLP bodies written before `before` — and ONLY those. Not
+ `delete_project_blobs`: that also takes `fixtures/` and `cases/`, which a promoted regression
+ case needs long after its source trace has expired."""
+ return _delete_prefix(f"{settings.s3_event_prefix}{project_id}/otlp/", before)
+
+
def delete_project_blobs(
project_id: str, *, traces_only: bool = False, before: datetime | None = None
) -> int:
diff --git a/backend/tracely/infrastructure/queue/celery_app.py b/backend/tracely/infrastructure/queue/celery_app.py
index 691c267..768467c 100644
--- a/backend/tracely/infrastructure/queue/celery_app.py
+++ b/backend/tracely/infrastructure/queue/celery_app.py
@@ -77,6 +77,12 @@ celery_app.conf.update(
"task": "tracely.purge_orphans",
"schedule": crontab(hour="5", minute="47"),
},
+ # Raw OTLP bodies past the 90-day horizon the tables already enforce. Last in the window:
+ # it lists every body, so it holds the solo worker longest.
+ "tracely.expire_otlp_blobs-nightly": {
+ "task": "tracely.expire_otlp_blobs",
+ "schedule": crontab(hour="6", minute="7"),
+ },
},
timezone="UTC",
)
diff --git a/backend/tracely/services/purge_service.py b/backend/tracely/services/purge_service.py
index a83fba8..41f30d9 100644
--- a/backend/tracely/services/purge_service.py
+++ b/backend/tracely/services/purge_service.py
@@ -36,6 +36,11 @@ log = structlog.get_logger()
ORPHAN_QUIET_DAYS = 7
+# Raw OTLP bodies used to outlive the traces they carry: `events` drops a span 90 days after it
+# happened (ddl/0003_events_ttl), but nothing ever expired the body it was ingested from.
+OTLP_RETENTION_DAYS = 90
+
+
def schedule_purge(project_id: str, before: datetime | None = None) -> None:
"""Queue the physical purge of one workspace's deleted data.
@@ -100,3 +105,23 @@ def purge_orphans(now: datetime | None = None) -> dict:
def _aware(ts: datetime) -> datetime:
"""ClickHouse hands back naive UTC datetimes."""
return ts if ts.tzinfo else ts.replace(tzinfo=timezone.utc)
+
+
+def expire_otlp_blobs(now: datetime | None = None) -> dict:
+ """Delete raw OTLP bodies older than OTLP_RETENTION_DAYS, in every workspace (beat, nightly).
+
+ Only `{project}/otlp/`: case fixtures and artifacts stay (a promoted case must keep running
+ after its source trace has expired), and so do chat attachments. Nothing reads a body after
+ ingest, so past the table's own horizon it is bytes no feature can reach.
+
+ ponytail: lists every body nightly (~550 objects/s on this MinIO) — minutes at today's volume.
+ When that gets slow, tag bodies on upload and let a bucket lifecycle rule expire them instead.
+ """
+ now = now or datetime.now(timezone.utc)
+ cutoff = now - timedelta(days=OTLP_RETENTION_DAYS)
+ removed = {pid: s3.delete_otlp_blobs(pid, cutoff) for pid in sorted(s3.project_ids_in_storage())}
+ removed = {pid: n for pid, n in removed.items() if n}
+ if removed:
+ log.info("otlp_blobs_expired", before=cutoff.isoformat(), by_project=removed)
+ return {"expired": sum(removed.values()), "before": cutoff.isoformat()}
+
diff --git a/backend/tracely/workers/tasks.py b/backend/tracely/workers/tasks.py
index 07a2bbf..16dc697 100644
--- a/backend/tracely/workers/tasks.py
+++ b/backend/tracely/workers/tasks.py
@@ -367,6 +367,20 @@ def purge_orphans_task(self) -> dict:
return {"error": str(exc)}
+@celery_app.task(name="tracely.expire_otlp_blobs", bind=True, max_retries=0)
+def expire_otlp_blobs_task(self) -> dict:
+ """Delete raw OTLP bodies past the 90-day horizon the ClickHouse tables already enforce
+ (beat, nightly) — see `purge_service.expire_otlp_blobs`. A run cut short by the time limit
+ just leaves the rest for tomorrow."""
+ from tracely.services.purge_service import expire_otlp_blobs
+
+ try:
+ return expire_otlp_blobs()
+ except Exception as exc: # noqa: BLE001 — a missed night costs disk, never data
+ log.warning("expire_otlp_blobs_failed", error=str(exc))
+ return {"error": str(exc)}
+
+
@celery_app.task(name="tracely.compact_tables", bind=True, max_retries=0)
def compact_tables_task(self) -> dict:
"""Physically drop masked and superseded rows from `events`/`scores` (beat, nightly) — see