: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(activity-logs): emit required severity_id and map client to the actor (#78846)
Yasen95292d0b8467
diff --git a/posthog/api/advanced_activity_logs/ocsf.py b/posthog/api/advanced_activity_logs/ocsf.pyindex 18122710751c..acef512342c2 100644--- a/posthog/api/advanced_activity_logs/ocsf.py+++ b/posthog/api/advanced_activity_logs/ocsf.py@@ -24,6 +24,10 @@ ACTIVITY_ID_OTHER = 99STATUS_ID_SUCCESS = 1STATUS_ID_FAILURE = 2+# Required on every OCSF event. An audit trail records what happened rather than scoring it, so+# every entry is Informational; consumers derive severity from their own detection rules.+SEVERITY_ID_INFORMATIONAL = 1+# `activity` is an unconstrained CharField with no enum and ~57 distinct values in production, so# only the ones carrying real volume are mapped. Everything else falls through to activity_id 99# plus activity_name, which is OCSF's documented mechanism for an unmapped source activity.@@ -100,12 +104,16 @@ class ActivityLogOCSFSerializer(serializers.ModelSerializer):"category_uid": CATEGORY_UID_IAM,"type_uid": class_uid * 100 + activity_id,"activity_id": activity_id,+ "severity_id": SEVERITY_ID_INFORMATIONAL,"status_id": STATUS_ID_FAILURE if instance.activity in _FAILURE_ACTIVITIES else STATUS_ID_SUCCESS,"entity": {"type": instance.scope,"uid": instance.item_id,"name": detail.get("name"),},+ # Everything here is PostHog-specific with no OCSF equivalent, which is what `unmapped`+ # is for. Entity Management has no attribute for which fields changed, and the class has+ # no top-level `org`, so tenancy identifiers have nowhere else to go either."unmapped": {"activity": instance.activity,"changed_fields": changed_fields,@@ -113,21 +121,27 @@ class ActivityLogOCSFSerializer(serializers.ModelSerializer):"organization_id": str(instance.organization_id) if instance.organization_id else None,"was_impersonated": instance.was_impersonated,"is_system": instance.is_system,- "client": instance.client,},}if activity_id == ACTIVITY_ID_OTHER:event["activity_name"] = instance.activity+ # OCSF requires an actor to carry at least one of app_name, app_uid, invoked_by, process,+ # session or user, so it is only emitted when there is something to put in it.+ actor: dict[str, Any] = {}if instance.user is not None:- event["actor"] = {- "user": {- "uid": str(instance.user.uuid),- "email_addr": instance.user.email,- "name": instance.user.first_name,- }+ actor["user"] = {+ "uid": str(instance.user.uuid),+ "email_addr": instance.user.email,+ "name": instance.user.first_name,}+ if instance.client:+ # `app_name` is "the client application or service that initiated the activity", which is+ # what the x-posthog-client header records.+ actor["app_name"] = instance.client+ if actor:+ event["actor"] = actorif instance.ip_address:event["src_endpoint"] = {"ip": instance.ip_address}diff --git a/posthog/api/advanced_activity_logs/test_ocsf.py b/posthog/api/advanced_activity_logs/test_ocsf.pyindex 85f2b671e9dd..6e594baf6c8b 100644--- a/posthog/api/advanced_activity_logs/test_ocsf.py+++ b/posthog/api/advanced_activity_logs/test_ocsf.py@@ -18,6 +18,7 @@ class TestActivityLogOCSF(APIBaseTest):item_id="flag-1",user=self.user,ip_address="203.0.113.4",+ client="posthog-python",detail=detail,)# Read it back so `detail` is the JSON dict the list endpoint serializes, not the dataclass@@ -68,6 +69,19 @@ class TestActivityLogOCSF(APIBaseTest):assert event["entity"]["data"] == {"filters": {"a": 1}}assert event["entity_result"]["data"] == {"filters": {"a": 2}}+ def test_every_required_ocsf_attribute_is_present(self):+ # A strict OCSF consumer rejects an event missing any of these.+ event = ActivityLogOCSFSerializer(self._log()).data++ for attribute in ("metadata", "time", "class_uid", "category_uid", "type_uid", "activity_id", "severity_id"):+ assert attribute in event, f"missing required OCSF attribute: {attribute}"++ def test_client_is_reported_as_the_actor_app_rather_than_unmapped(self):+ event = ActivityLogOCSFSerializer(self._log()).data++ assert event["actor"]["app_name"] == "posthog-python"+ assert "client" not in event["unmapped"]+def test_time_is_epoch_milliseconds_and_time_dt_is_rfc3339(self):log = self._log()