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.

feat(error_tracking): add native alert, destination, and thread models (#90533)

Catalin58fb02ca6677
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index 5d76420fd6fb..a8b274231231 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -160,6 +160,9 @@ rules:
|EventFilterConfig
|EventStream
|EventStreamMember
+ |ErrorTrackingAlert
+ |ErrorTrackingAlertDestination
+ |ErrorTrackingAlertThread
|ErrorTrackingAssignmentRule
|ErrorTrackingBypassRule
|ErrorTrackingGroupingRule
@@ -547,6 +550,9 @@ rules:
|EventFilterConfig
|EventStream
|EventStreamMember
+ |ErrorTrackingAlert
+ |ErrorTrackingAlertDestination
+ |ErrorTrackingAlertThread
|ErrorTrackingAssignmentRule
|ErrorTrackingBypassRule
|ErrorTrackingGroupingRule
diff --git a/products/error_tracking/backend/migrations/0031_errortrackingalert_errortrackingalertdestination_and_more.py b/products/error_tracking/backend/migrations/0031_errortrackingalert_errortrackingalertdestination_and_more.py
new file mode 100644
index 000000000000..c914599d5640
--- /dev/null
+++ b/products/error_tracking/backend/migrations/0031_errortrackingalert_errortrackingalertdestination_and_more.py
@@ -0,0 +1,186 @@
+# Generated by Django 5.2.17 on 2026-08-27 17:36
+
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.uuidt
+
+import products.error_tracking.backend.models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("error_tracking", "0030_add_stackframe_team_created_at_index"),
+ ("posthog", "1323_proxyrecord_root_redirect_url"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="ErrorTrackingAlert",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.UUIDT,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("name", models.TextField()),
+ ("enabled", models.BooleanField(default=True)),
+ ("triggers", models.JSONField(default=list)),
+ ("filters", models.JSONField(blank=True, default=dict)),
+ ("throttle_seconds", models.PositiveIntegerField(default=0)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "created_by",
+ models.ForeignKey(
+ related_name="+",
+ blank=True,
+ db_constraint=False,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ related_name="+",
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_errortrackingalert",
+ },
+ ),
+ migrations.CreateModel(
+ name="ErrorTrackingAlertDestination",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.UUIDT,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ (
+ "channel_type",
+ models.TextField(
+ choices=products.error_tracking.backend.models.error_tracking_alert_channel_type_choices
+ ),
+ ),
+ ("config", models.JSONField(blank=True, default=dict)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "alert",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="destinations",
+ to="error_tracking.errortrackingalert",
+ ),
+ ),
+ (
+ "integration",
+ models.ForeignKey(
+ related_name="+",
+ blank=True,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ to="posthog.integration",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ related_name="+",
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_errortrackingalertdestination",
+ },
+ ),
+ migrations.CreateModel(
+ name="ErrorTrackingAlertThread",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.uuidt.UUIDT,
+ editable=False,
+ primary_key=True,
+ serialize=False,
+ ),
+ ),
+ ("external_ref", models.JSONField(blank=True, default=dict)),
+ ("root_headline", models.TextField(blank=True, default="")),
+ (
+ "delivered_notification_ids",
+ models.JSONField(blank=True, default=list),
+ ),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("updated_at", models.DateTimeField(auto_now=True)),
+ (
+ "alert",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="threads",
+ to="error_tracking.errortrackingalert",
+ ),
+ ),
+ (
+ "destination",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="threads",
+ to="error_tracking.errortrackingalertdestination",
+ ),
+ ),
+ (
+ "issue",
+ models.ForeignKey(
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="alert_threads",
+ to="error_tracking.errortrackingissue",
+ ),
+ ),
+ (
+ "team",
+ models.ForeignKey(
+ related_name="+",
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ to="posthog.team",
+ ),
+ ),
+ ],
+ options={
+ "db_table": "posthog_errortrackingalertthread",
+ },
+ ),
+ migrations.AddIndex(
+ model_name="errortrackingalert",
+ index=models.Index(fields=["team", "enabled"], name="idx_et_alert_team_enabled"),
+ ),
+ migrations.AddConstraint(
+ model_name="errortrackingalertthread",
+ constraint=models.UniqueConstraint(
+ fields=("alert", "issue", "destination"),
+ name="unique_error_tracking_alert_thread",
+ ),
+ ),
+ ]
diff --git a/products/error_tracking/backend/migrations/max_migration.txt b/products/error_tracking/backend/migrations/max_migration.txt
index b7dd3d4d8c62..ff779ffb15bc 100644
--- a/products/error_tracking/backend/migrations/max_migration.txt
+++ b/products/error_tracking/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0030_add_stackframe_team_created_at_index
+0031_errortrackingalert_errortrackingalertdestination_and_more
diff --git a/products/error_tracking/backend/models.py b/products/error_tracking/backend/models.py
index 263bc73a974d..43a880668152 100644
--- a/products/error_tracking/backend/models.py
+++ b/products/error_tracking/backend/models.py
@@ -885,3 +885,115 @@ class ErrorTrackingRecommendation(UUIDTModel):
constraints = [
models.UniqueConstraint(fields=["team", "type"], name="unique_error_tracking_recommendation_per_team_type"),
]
+
+
+class ErrorTrackingAlert(TeamScopedRootMixin, UUIDTModel):
+ """Alert configuration for issue lifecycle notifications.
+
+ Triggers are opt-in thread openers: they decide which lifecycle transitions start
+ a notification thread for an issue. Once a thread exists, every later lifecycle
+ update for that issue is delivered into it as a reply without further trigger or
+ filter evaluation, so new reply kinds are enabled for existing alerts with no
+ migration (see ErrorTrackingAlertThread).
+ """
+
+ class Trigger(models.TextChoices):
+ ISSUE_CREATED = "issue_created", "Issue created"
+ ISSUE_REOPENED = "issue_reopened", "Issue reopened"
+ ISSUE_SPIKING = "issue_spiking", "Issue spiking"
+ ISSUE_ASSIGNED = "issue_assigned", "Issue assigned"
+
+ # db_constraint=False keeps creates lock-free on hot tables (posthog_team / posthog_user);
+ # team scoping is enforced at the ORM layer via TeamScopedRootMixin.
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ name = models.TextField()
+ enabled = models.BooleanField(default=True)
+ triggers = models.JSONField(default=list) # list of Trigger values
+ # Same property-filter shape hog functions store, with the compiled HogQL bytecode
+ # embedded under "bytecode" (see posthog.cdp.filters.compile_filters_bytecode).
+ filters = models.JSONField(default=dict, blank=True)
+ # Minimum seconds between thread-opening notifications per (alert, issue).
+ # Replies into an existing thread are never throttled. 0 disables the throttle.
+ throttle_seconds = models.PositiveIntegerField(default=0)
+ created_by = models.ForeignKey(
+ settings.AUTH_USER_MODEL,
+ on_delete=models.SET_NULL,
+ related_name="+",
+ null=True,
+ blank=True,
+ db_constraint=False,
+ )
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ db_table = "posthog_errortrackingalert"
+ indexes = [
+ models.Index(fields=["team", "enabled"], name="idx_et_alert_team_enabled"),
+ ]
+
+
+# nosemgrep: tuple-return-prefer-dataclass -- Django's `choices` contract is (value, label) pairs.
+def error_tracking_alert_channel_type_choices() -> list[tuple[str, str]]:
+ # Callable choices: adding a channel later must not generate an AlterField migration.
+ return [(str(value), str(label)) for value, label in ErrorTrackingAlertDestination.ChannelType.choices]
+
+
+class ErrorTrackingAlertDestination(TeamScopedRootMixin, UUIDTModel):
+ """One delivery target of an alert.
+
+ The alert's triggers and filters are evaluated once per lifecycle transition and
+ the outcome fans out to every destination, so per-destination state (threads,
+ delivery results) hangs off this row rather than the alert.
+ """
+
+ class ChannelType(models.TextChoices):
+ # Additional channels (email, in-app, webhook) are planned; only rows with
+ # currently supported channel types are ever created through the API.
+ SLACK = "slack", "Slack"
+
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ alert = models.ForeignKey(ErrorTrackingAlert, on_delete=models.CASCADE, related_name="destinations")
+ channel_type = models.TextField(choices=error_tracking_alert_channel_type_choices)
+ integration = models.ForeignKey(Integration, on_delete=models.SET_NULL, related_name="+", null=True, blank=True)
+ # Channel-specific delivery settings, e.g. {"channel": "C0123", "channel_name": "#alerts"} for Slack
+ config = models.JSONField(default=dict, blank=True)
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ db_table = "posthog_errortrackingalertdestination"
+
+
+class ErrorTrackingAlertThread(TeamScopedRootMixin, UUIDTModel):
+ """Delivery state for one (alert, issue, destination) conversation.
+
+ Maps an issue to the externally posted notification (e.g. a Slack message) so
+ lifecycle updates can be delivered as replies to the original message instead
+ of new fire-and-forget notifications. The unique constraint is the concurrency
+ primitive: concurrent deliveries race on the insert and the loser reuses the
+ winner's thread.
+ """
+
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ alert = models.ForeignKey(ErrorTrackingAlert, on_delete=models.CASCADE, related_name="threads")
+ issue = models.ForeignKey(ErrorTrackingIssue, on_delete=models.CASCADE, related_name="alert_threads")
+ destination = models.ForeignKey(ErrorTrackingAlertDestination, on_delete=models.CASCADE, related_name="threads")
+ # External message reference, e.g. {"channel": "C0123", "ts": "1234.5678"} for Slack
+ external_ref = models.JSONField(default=dict, blank=True)
+ # The root message's headline. It is the thread's identity: status transitions
+ # edit the root in place but never change the headline.
+ root_headline = models.TextField(blank=True, default="")
+ # UUIDs of recently delivered lifecycle notifications (newest last, capped by the
+ # delivery activity) so Temporal retries don't duplicate notifications.
+ delivered_notification_ids = models.JSONField(default=list, blank=True)
+ created_at = models.DateTimeField(auto_now_add=True)
+ updated_at = models.DateTimeField(auto_now=True)
+
+ class Meta:
+ db_table = "posthog_errortrackingalertthread"
+ constraints = [
+ models.UniqueConstraint(
+ fields=["alert", "issue", "destination"], name="unique_error_tracking_alert_thread"
+ ),
+ ]