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(logs): add source column to logs metric rule (migration) (#94352)

Daniel Visca837e043d6c0a
diff --git a/products/logs/backend/migrations/0024_logsmetricrule_source.py b/products/logs/backend/migrations/0024_logsmetricrule_source.py
new file mode 100644
index 000000000000..c867b14dc3b7
--- /dev/null
+++ b/products/logs/backend/migrations/0024_logsmetricrule_source.py
@@ -0,0 +1,31 @@
+# Generated by hand for span-based metrics (adds LogsMetricRule.source + per-source uniqueness).
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("logs", "0023_teamlogsconfig_logs_pattern_message_keys"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="logsmetricrule",
+ name="source",
+ field=models.CharField(
+ choices=[("logs", "Logs"), ("spans", "Spans")],
+ default="logs",
+ max_length=16,
+ ),
+ ),
+ migrations.RemoveConstraint(
+ model_name="logsmetricrule",
+ name="logs_metric_rule_team_metric_uniq",
+ ),
+ migrations.AddConstraint(
+ model_name="logsmetricrule",
+ constraint=models.UniqueConstraint(
+ fields=("team", "metric_name", "source"), name="logs_metric_rule_team_metric_uniq"
+ ),
+ ),
+ ]
diff --git a/products/logs/backend/migrations/max_migration.txt b/products/logs/backend/migrations/max_migration.txt
index c869227648e7..85b529697da6 100644
--- a/products/logs/backend/migrations/max_migration.txt
+++ b/products/logs/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0023_teamlogsconfig_logs_pattern_message_keys
+0024_logsmetricrule_source
diff --git a/products/logs/backend/models.py b/products/logs/backend/models.py
index b893b2f62fb7..3d0c3dedc240 100644
--- a/products/logs/backend/models.py
+++ b/products/logs/backend/models.py
@@ -373,13 +373,22 @@ MAX_METRIC_RULE_GROUP_BY_KEYS = 5
# addressed through the `attributes.` / `resource_attributes.` map prefixes.
METRIC_RULE_GROUP_BY_TOP_LEVEL_KEYS = ("service_name", "severity_text", "event_name")
+# Top-level span fields allowed as group-by dimensions for `source=spans` rules. Spans
+# carry no severity/event columns, so the log-only keys are excluded; `name` and
+# `status_code` are span columns.
+METRIC_RULE_GROUP_BY_SPAN_TOP_LEVEL_KEYS = ("service_name", "name", "status_code")
+
class LogsMetricRule(ModelActivityMixin, TeamScopedRootMixin, CreatedMetaFields, UpdatedMetaFields, UUIDModel):
- """Generates a metric from ingested logs: records matching `filter_group` are tallied
- at ingest time (before drop rules) and emitted into the Metrics product under
+ """Generates a metric from ingested logs or spans: records matching `filter_group` are
+ tallied at ingest time (before drop rules) and emitted into the Metrics product under
`metric_name`. With `value_attribute` unset the rule counts matching records; when set,
the numeric value of that attribute is aggregated into a distribution (count + sum)."""
+ class RecordSource(models.TextChoices):
+ LOGS = "logs", "Logs"
+ SPANS = "spans", "Spans"
+
# db_constraint=False on the team/user FKs: posthog_team and posthog_user are hot tables,
# and creating an FK constraint against them locks the parent — see the hot-table section
# of safe-django-migrations.md. Enforcement is app-level (Django still cascades in the ORM).
@@ -401,12 +410,16 @@ class LogsMetricRule(ModelActivityMixin, TeamScopedRootMixin, CreatedMetaFields,
value_attribute = models.CharField(max_length=512, null=True, blank=True)
# Group-by dimension keys; each distinct value combination becomes its own series.
group_by = ArrayField(models.CharField(max_length=512), default=list, blank=True)
+ # Record source the rule tallies. Immutable after create — it decides which consumer
+ # (logs vs traces) evaluates the rule and which keys are valid. Default keeps
+ # pre-existing rows as log rules.
+ source = models.CharField(max_length=16, choices=RecordSource.choices, default=RecordSource.LOGS)
version = models.PositiveIntegerField(default=1)
class Meta:
db_table = "logs_logsmetricrule"
constraints = [
- models.UniqueConstraint(fields=["team", "metric_name"], name="logs_metric_rule_team_metric_uniq"),
+ models.UniqueConstraint(fields=["team", "metric_name", "source"], name="logs_metric_rule_team_metric_uniq"),
]
indexes = [
models.Index(fields=["team_id", "enabled"], name="logs_metric_team_enabled_idx"),