: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(apm): scaffold apm product behind a feature flag (#75547)
Jon McCallum77e248d986ea
diff --git a/frontend/package.json b/frontend/package.jsonindex 96d46b66ef2a..c36f5041bbac 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -84,6 +84,7 @@"@posthog/products-ai-observability": "workspace:*","@posthog/products-alerts": "workspace:*","@posthog/products-annotations": "workspace:*",+ "@posthog/products-apm": "workspace:*","@posthog/products-batch-exports": "workspace:*","@posthog/products-business-knowledge": "workspace:*","@posthog/products-cohorts": "workspace:*",diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 28661c820931..1658387ecf7e 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -514,6 +514,7 @@ export const FEATURE_FLAGS = {TRACING_SAVED_VIEWS: 'tracing-saved-views', // owner: #team-apm — gates saved views (saved filter sets) in tracingTRAFFIC_TYPE_VIRTUAL_PROPERTIES: 'traffic-type-virtual-properties', // owner: #team-web-analyticsUI_CUSTOMIZATION: 'ui-customization-enabled', // owner: @fercgomes #team-self-driving, gates sidebar/homepage UI customization+ UNIFIED_APM_PRODUCT: 'unified-apm-product', // owner: #team-apm (@jonmcwest, @frankh), gates the shared APM product backing logs, tracing, and metricsUSER_INTERVIEWS: 'user-interviews', // owner: @Twixes @jurajmajerikUX_REMOVE_SIDEPANEL: 'ux-remove-sidepanel', // owner: #team-surveysVISUAL_REVIEW: 'visual-review', // owner: #team-devexdiff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c7f60bd22f2d..d855e3c22ad9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -644,6 +644,9 @@ importers:'@posthog/products-annotations':specifier: workspace:*version: link:../products/annotations+ '@posthog/products-apm':+ specifier: workspace:*+ version: link:../products/apm'@posthog/products-batch-exports':specifier: workspace:*version: link:../products/batch_exports@@ -2479,6 +2482,8 @@ importers:products/annotations: {}+ products/apm: {}+products/approvals: {}products/batch_exports: {}diff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex 0e1e70e71df9..5fee3c070657 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -79,6 +79,7 @@ PRODUCTS_APPS = ["products.logs.backend.apps.LogsConfig","products.tracing.backend.apps.TracingConfig","products.metrics.backend.apps.MetricsConfig",+ "products.apm.backend.apps.ApmConfig","products.notifications.backend.apps.NotificationsConfig","products.dashboards.backend.apps.DashboardsConfig","products.messaging.backend.apps.MessagingConfig",diff --git a/products/apm/__init__.py b/products/apm/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/apm/backend/apps.py b/products/apm/backend/apps.pynew file mode 100644index 000000000000..cea18abd0673--- /dev/null+++ b/products/apm/backend/apps.py@@ -0,0 +1,8 @@+"""Django app configuration for apm."""++from django.apps import AppConfig+++class ApmConfig(AppConfig):+ name = "products.apm.backend"+ label = "apm"diff --git a/products/apm/backend/facade/api.py b/products/apm/backend/facade/api.pynew file mode 100644index 000000000000..22f368d8b4bc--- /dev/null+++ b/products/apm/backend/facade/api.py@@ -0,0 +1,14 @@+"""+Facade for apm.++The ONLY module other products are allowed to import.+Accept frozen dataclasses, call logic/, return frozen+dataclasses. Never return ORM instances or import DRF.++So far the only thing consuming products need from APM is whether it is switched+on for a given team, so that is all this exposes for now.+"""++from products.apm.backend.feature_flags import is_apm_enabled++__all__ = ["is_apm_enabled"]diff --git a/products/apm/backend/facade/contracts.py b/products/apm/backend/facade/contracts.pynew file mode 100644index 000000000000..827ae7204fea--- /dev/null+++ b/products/apm/backend/facade/contracts.py@@ -0,0 +1,11 @@+"""+Contract types for apm.++Frozen dataclasses that define what this product exposes.+No Django imports. Used by facade as inputs/outputs.++Uses ``pydantic.dataclasses.dataclass`` rather than the stdlib+variant — same syntax, same ``is_dataclass()`` compatibility (so+``DataclassSerializer`` keeps working), but with runtime type+validation on construction.+"""diff --git a/products/apm/backend/facade/enums.py b/products/apm/backend/facade/enums.pynew file mode 100644index 000000000000..fe0c85f7a46e--- /dev/null+++ b/products/apm/backend/facade/enums.py@@ -0,0 +1,7 @@+"""+Exported enums for apm.++If an enum appears in a contract dataclass field, it belongs here.+Internal-only constants (DB magic values, feature flags) stay in+the implementation (logic/, models.py).+"""diff --git a/products/apm/backend/feature_flags.py b/products/apm/backend/feature_flags.pynew file mode 100644index 000000000000..2175ae1a01b1--- /dev/null+++ b/products/apm/backend/feature_flags.py@@ -0,0 +1,41 @@+"""Feature-flag gate for the APM product.++APM is unreleased, so nothing that lands here runs for a team until+``unified-apm-product`` is switched on for that team. This gates the product as+a whole. A capability that needs its own rollout, like anomaly detection, gets+its own flag layered on top of this one rather than reusing it.++The gate evaluates against the ``project`` group keyed on team ID alone, so it+needs no ``Team`` row and no database read. That matters because work landing+here is expected to run per team on a schedule, where a fetch per team per tick+would be pure overhead.+"""++from __future__ import annotations++import structlog++from posthog.ph_client import feature_enabled_or_false++logger = structlog.get_logger(__name__)++UNIFIED_APM_PRODUCT_FLAG = "unified-apm-product"+++def is_apm_enabled(team_id: int) -> bool:+ try:+ return feature_enabled_or_false(+ UNIFIED_APM_PRODUCT_FLAG,+ str(team_id),+ groups={"project": str(team_id)},+ group_properties={"project": {"id": str(team_id)}},+ only_evaluate_locally=False,+ # Work here is scheduled per team, so leaving this on would emit a+ # $feature_flag_called event for every team on every tick.+ send_feature_flag_events=False,+ )+ except Exception:+ # An unreleased product stays off when the flags service is unreachable,+ # rather than defaulting on or failing the caller's run.+ logger.exception("apm_feature_flag_check_failed", team_id=team_id)+ return Falsediff --git a/products/apm/backend/logic/__init__.py b/products/apm/backend/logic/__init__.pynew file mode 100644index 000000000000..c73307edc95e--- /dev/null+++ b/products/apm/backend/logic/__init__.py@@ -0,0 +1 @@+"""Business logic for apm."""diff --git a/products/apm/backend/models.py b/products/apm/backend/models.pynew file mode 100644index 000000000000..159dd3e87e2a--- /dev/null+++ b/products/apm/backend/models.py@@ -0,0 +1,14 @@+"""+Django models for apm.++Keep models thin — business logic belongs in logic/.+Use types from facade/enums.py where applicable.+Avoid ForeignKeys to models outside this app; if needed,+disallow reverse relations with related_name='+'.++Models must inherit TeamScopedRootMixin to opt into fail-closed team+scoping — queries without team context raise TeamScopeError instead of+silently returning every team's rows. Main-DB products add+`team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE)`.+See posthog/models/scoping/README.md.+"""diff --git a/products/apm/backend/presentation/serializers.py b/products/apm/backend/presentation/serializers.pynew file mode 100644index 000000000000..eca8a315ca54--- /dev/null+++ b/products/apm/backend/presentation/serializers.py@@ -0,0 +1 @@+"""DRF serializers for apm."""diff --git a/products/apm/backend/presentation/urls.py b/products/apm/backend/presentation/urls.pynew file mode 100644index 000000000000..5ca313268ca3--- /dev/null+++ b/products/apm/backend/presentation/urls.py@@ -0,0 +1,3 @@+"""URL routes for apm."""++urlpatterns: list = []diff --git a/products/apm/backend/presentation/views.py b/products/apm/backend/presentation/views.pynew file mode 100644index 000000000000..21a80e30fffb--- /dev/null+++ b/products/apm/backend/presentation/views.py@@ -0,0 +1,6 @@+"""+DRF views for apm.++Validate JSON via serializers, call facade methods,+return serialized responses. No business logic here.+"""diff --git a/products/apm/backend/tasks/schedules.py b/products/apm/backend/tasks/schedules.pynew file mode 100644index 000000000000..d8a53c5e0d26--- /dev/null+++ b/products/apm/backend/tasks/schedules.py@@ -0,0 +1,2 @@+"""Celery beat schedules for apm."""+# Define periodic task schedules herediff --git a/products/apm/backend/tasks/tasks.py b/products/apm/backend/tasks/tasks.pynew file mode 100644index 000000000000..ef3690704be1--- /dev/null+++ b/products/apm/backend/tasks/tasks.py@@ -0,0 +1,8 @@+"""+Celery tasks for apm.++Async entrypoints that call the facade (facade/api.py).+Keep task functions thin - only call facade methods.+"""+# from celery import shared_task+# from ..facade import apidiff --git a/products/apm/backend/tests/__init__.py b/products/apm/backend/tests/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/apm/backend/tests/conftest.py b/products/apm/backend/tests/conftest.pynew file mode 100644index 000000000000..ae3f00200968--- /dev/null+++ b/products/apm/backend/tests/conftest.py@@ -0,0 +1,13 @@+import pytest++from posthog.models.scoping import team_scope++[email protected](autouse=True)+def _set_team_scope(request):+ if request.node.get_closest_marker("django_db") is None:+ yield+ return+ team = request.getfixturevalue("team")+ with team_scope(team.id):+ yielddiff --git a/products/apm/backend/tests/test_feature_flags.py b/products/apm/backend/tests/test_feature_flags.pynew file mode 100644index 000000000000..ecadaf0f7d39--- /dev/null+++ b/products/apm/backend/tests/test_feature_flags.py@@ -0,0 +1,23 @@+from unittest import TestCase+from unittest.mock import patch++from parameterized import parameterized++from products.apm.backend.feature_flags import is_apm_enabled+++class TestApmFeatureFlag(TestCase):+ @parameterized.expand(+ [+ (True, True),+ (False, False),+ (None, False),+ ]+ )+ def test_gate_reflects_flag_state(self, flag_result: bool | None, expected: bool) -> None:+ with patch("posthoganalytics.feature_enabled", return_value=flag_result):+ assert is_apm_enabled(team_id=1) is expected++ def test_gate_stays_closed_when_flag_service_raises(self) -> None:+ with patch("posthoganalytics.feature_enabled", side_effect=RuntimeError("flags unreachable")):+ assert is_apm_enabled(team_id=1) is Falsediff --git a/products/apm/manifest.tsx b/products/apm/manifest.tsxnew file mode 100644index 000000000000..d99a1b093820--- /dev/null+++ b/products/apm/manifest.tsx@@ -0,0 +1,20 @@+/**+ * Product manifest for APM.+ *+ * No scenes or nav entries yet. APM's first deliverable is a backend detector+ * that the logs, tracing, and metrics products consume, so today its only+ * surfaces are theirs. An empty manifest is valid in the meantime; see+ * products/notifications for the same shape.+ */+import { ProductManifest } from '../../frontend/src/types'++export const manifest: ProductManifest = {+ name: 'APM',+ scenes: {},+ routes: {},+ redirects: {},+ urls: {},+ fileSystemTypes: {},+ treeItemsNew: [],+ treeItemsProducts: [],+}diff --git a/products/apm/package.json b/products/apm/package.jsonnew file mode 100644index 000000000000..c4fa98752f67--- /dev/null+++ b/products/apm/package.json@@ -0,0 +1,6 @@+{+ "name": "@posthog/products-apm",+ "scripts": {+ "backend:test": "pytest -c ../../pytest.ini --rootdir ../.. backend/tests -v --tb=short"+ }+}diff --git a/products/apm/product.yaml b/products/apm/product.yamlnew file mode 100644index 000000000000..186b61d2d73b--- /dev/null+++ b/products/apm/product.yaml@@ -0,0 +1,3 @@+name: APM+owners:+ - logsdiff --git a/products/apm/tsconfig.json b/products/apm/tsconfig.jsonnew file mode 100644index 000000000000..fe425d6fcba8--- /dev/null+++ b/products/apm/tsconfig.json@@ -0,0 +1,6 @@+{+ "extends": "../../tsconfig.json",+ "compilerOptions": {+ "composite": true+ }+}diff --git a/tach.toml b/tach.tomlindex 16e1a310c0da..a42fbc901208 100644--- a/tach.toml+++ b/tach.toml@@ -1165,3 +1165,17 @@ expose = [from = ["products.data_catalog",]++[[modules]]+path = "products.apm"+depends_on = ["posthog"]+layer = "modules"++[[interfaces]]+expose = [+ "backend\\.facade.*",+ "backend\\.presentation\\.views.*",+]+from = [+ "products.apm",+]