: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
feat(signals): flatten emit extra onto signal_emitted analytics events (#62022)
Andrew Maguire40e4195fb469
diff --git a/products/signals/backend/facade/api.py b/products/signals/backend/facade/api.pyindex aff9f358b4cb..d8cabfc1e1a7 100644--- a/products/signals/backend/facade/api.py+++ b/products/signals/backend/facade/api.py@@ -58,6 +58,27 @@ for _variant_type in get_args(SignalInput.model_fields["root"].annotation):_SIGNAL_VARIANT_LOOKUP[(_product, _source_type)] = _variant_type+# Telemetry only forwards top-level *scalar* `extra` values, each truncated — never nested+# lists/dicts. Source `extra` payloads nest customer-derived content (pganalyze+# `references[].queryText` raw SQL, session-replay `event_history`, scout `evidence`+# summaries) that must not leak into product analytics; scalars are the cheap-to-query+# attribution we actually want (`scout_run_id`, `task_run_id`, `skill_name`, …). The cap+# bounds top-level strings that could still be large (e.g. an `error_message`).+_MAX_TELEMETRY_STR_LEN = 256+++def _telemetry_props_from_extra(extra: dict | None) -> dict:+ if not extra:+ return {}+ props: dict = {}+ for key, value in extra.items():+ if isinstance(value, str):+ props[key] = value[:_MAX_TELEMETRY_STR_LEN]+ elif isinstance(value, (bool, int, float)):+ props[key] = value+ return props++async def emit_signal(team: Team,source_product: str,@@ -80,7 +101,12 @@ async def emit_signal(source_id: Unique identifier within the source (e.g., experiment UUID)description: Human-readable description that will be embeddedweight: Importance/confidence of signal (0.0-1.0). Weight of 1.0 triggers summary.- extra: Optional product-specific metadata+ extra: Optional product-specific metadata. Its top-level scalar values (truncated) are+ flattened onto the `signal_emission_started` and `signal_emitted` analytics events+ alongside the core `source_*` keys (which win on conflict) — see+ `_telemetry_props_from_extra` — so per-source attribution (e.g. the scout harness's+ `scout_run_id` / `skill_name`) is queryable downstream without a schema change.+ Nested lists/dicts are never forwarded.Example:await emit_signal(@@ -150,6 +176,7 @@ async def emit_signal(event="signal_emission_started",distinct_id=str(team.uuid),properties={+ **_telemetry_props_from_extra(extra),"source_product": source_product,"source_type": source_type,"source_id": source_id,@@ -206,6 +233,7 @@ async def emit_signal(event="signal_emitted",distinct_id=str(team.uuid),properties={+ **_telemetry_props_from_extra(extra),"source_product": source_product,"source_type": source_type,"source_id": source_id,diff --git a/products/signals/backend/test/test_api.py b/products/signals/backend/test/test_api.pyindex 7204ef3d0f85..4188d8b113d9 100644--- a/products/signals/backend/test/test_api.py+++ b/products/signals/backend/test/test_api.py@@ -6,7 +6,7 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pydanticimport temporalio.exceptions-from products.signals.backend.facade.api import emit_signal+from products.signals.backend.facade.api import _MAX_TELEMETRY_STR_LEN, _telemetry_props_from_extra, emit_signalfrom products.signals.backend.models import SignalSourceConfigfrom products.signals.backend.temporal.buffer import BufferSignalsWorkflowfrom products.signals.backend.temporal.emitter import SignalEmitterWorkflow@@ -159,7 +159,15 @@ class TestEmitSignalAnalytics:# Both the "started" marker and the final "emitted" event fireevents = [call.kwargs["event"] for call in capture.call_args_list]assert events == ["signal_emission_started", "signal_emitted"]+ # Only top-level scalar `extra` values are flattened onto the event (the `labels`+ # list is dropped); the core `source_*` keys win on conflict.expected_properties = {+ "html_url": "https://github.com/org/repo/issues/42",+ "number": 42,+ "created_at": "2025-06-01T12:00:00Z",+ "updated_at": "2025-06-02T08:00:00Z",+ "locked": False,+ "state": "open","source_product": "github","source_type": "issue","source_id": "posthog/posthog#42",@@ -167,4 +175,29 @@ class TestEmitSignalAnalytics:for call in capture.call_args_list:assert call.kwargs["distinct_id"] == str(team_stub.uuid)assert call.kwargs["properties"] == expected_properties+ assert "labels" not in call.kwargs["properties"]assert "project" in call.kwargs["groups"]+++class TestTelemetryPropsFromExtra:+ def test_none_and_empty(self) -> None:+ assert _telemetry_props_from_extra(None) == {}+ assert _telemetry_props_from_extra({}) == {}++ def test_keeps_scalars_drops_nested(self) -> None:+ props = _telemetry_props_from_extra(+ {+ "scout_run_id": "run-1",+ "number": 42,+ "confidence": 0.9,+ "locked": False,+ "labels": ["bug", "p1"], # list — dropped+ "references": [{"queryText": "SELECT * FROM customers"}], # nested — dropped+ "time_range": {"date_from": "a", "date_to": "b"}, # dict — dropped+ }+ )+ assert props == {"scout_run_id": "run-1", "number": 42, "confidence": 0.9, "locked": False}++ def test_truncates_long_strings(self) -> None:+ props = _telemetry_props_from_extra({"error_message": "x" * 1000})+ assert len(props["error_message"]) == _MAX_TELEMETRY_STR_LEN