: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(cdp): stop dropping legacy insight alert destinations (#86202)
posthog[bot]eff9b14efc9b
diff --git a/nodejs/src/cdp/consumers/cdp-internal-event.consumer.ts b/nodejs/src/cdp/consumers/cdp-internal-event.consumer.tsindex 514ef730ea1f..3c795c884dea 100644--- a/nodejs/src/cdp/consumers/cdp-internal-event.consumer.ts+++ b/nodejs/src/cdp/consumers/cdp-internal-event.consumer.ts@@ -8,6 +8,7 @@ import { logger } from '~/common/utils/logger'import { captureException } from '~/common/utils/posthog'import { HealthCheckResult, PluginsServerConfig } from '../../types'+import { isManagedAlertInternalEvent } from '../managed-alert-events'import { CdpInternalEventSchema } from '../schema'import { HogFunctionInvocationPipeline } from '../services/hog-function-invocation-pipeline.service'import { JobQueue } from '../services/job-queue/job-queue.interface'@@ -57,7 +58,7 @@ export class CdpInternalEventsConsumer extends CdpConsumerBase {hogTypes: this.hogTypes,filterFn: () => true,invocationFilterFn: (fn, globals) => {- if (!/^\$[a-z0-9_]+_alert_(firing|resolved|errored|auto_disabled)$/.test(globals.event.event)) {+ if (!isManagedAlertInternalEvent(globals.event.event)) {return true}const alertId = globals.event.properties?.alert_iddiff --git a/nodejs/src/cdp/consumers/cdp-internal-events-consumer.test.ts b/nodejs/src/cdp/consumers/cdp-internal-events-consumer.test.tsindex 963e727c0609..5b32f5c56fc4 100644--- a/nodejs/src/cdp/consumers/cdp-internal-events-consumer.test.ts+++ b/nodejs/src/cdp/consumers/cdp-internal-events-consumer.test.ts@@ -222,23 +222,39 @@ describe('CDP Internal Events Consumer', () => {expect(invocations[0].functionId).toBe(internalFn.id)})- it('routes managed alert events only to the destination matching the event and alert id', async () => {- const filters = (alertId: string) => ({+ it.each([+ '$billing_alert_firing',+ '$billing_alert_resolved',+ '$billing_alert_errored',+ '$billing_alert_auto_disabled',+ '$logs_alert_firing',+ '$logs_alert_resolved',+ '$logs_alert_errored',+ '$logs_alert_auto_disabled',+ ])('routes %s only to the destination matching the event and alert id', async (managedAlertEvent) => {+ const filters = (eventId: string, alertId: string) => ({filters: {...HOG_FILTERS_EXAMPLES.no_filters.filters,- events: [{ id: '$billing_alert_firing', type: 'events' as const }],+ events: [{ id: eventId, type: 'events' as const }],properties: [{ key: 'alert_id', value: alertId, operator: 'exact', type: 'event' }],},})const matching = await insertHogFunction({...HOG_EXAMPLES.simple_fetch,...HOG_INPUTS_EXAMPLES.simple_fetch,- ...filters('alert-1'),+ ...filters(managedAlertEvent, 'alert-1'),})await insertHogFunction({...HOG_EXAMPLES.simple_fetch,...HOG_INPUTS_EXAMPLES.simple_fetch,- ...filters('alert-2'),+ ...filters(managedAlertEvent, 'alert-2'),+ })+ const otherManagedAlertEvent =+ managedAlertEvent === '$billing_alert_firing' ? '$logs_alert_firing' : '$billing_alert_firing'+ await insertHogFunction({+ ...HOG_EXAMPLES.simple_fetch,+ ...HOG_INPUTS_EXAMPLES.simple_fetch,+ ...filters(otherManagedAlertEvent, 'alert-1'),})await insertHogFunction({...HOG_EXAMPLES.simple_fetch,@@ -246,7 +262,7 @@ describe('CDP Internal Events Consumer', () => {...HOG_FILTERS_EXAMPLES.no_filters,})const event = createInternalEvent(team.id, {})- event.event.event = '$billing_alert_firing'+ event.event.event = managedAlertEventevent.event.properties = { alert_id: 'alert-1', current_value: '100' }const globals = await processor._parseKafkaBatch([createKafkaMessage(event)])@@ -254,5 +270,23 @@ describe('CDP Internal Events Consumer', () => {expect(invocations.map((invocation) => invocation.functionId)).toEqual([matching.id])})++ it('invokes a legacy $insight_alert_firing destination that has no alert_id filter', async () => {+ const fn = await insertHogFunction({+ ...HOG_EXAMPLES.simple_fetch,+ ...HOG_INPUTS_EXAMPLES.simple_fetch,+ filters: {+ ...HOG_FILTERS_EXAMPLES.no_filters.filters,+ events: [{ id: '$insight_alert_firing', type: 'events' as const }],+ },+ })+ const event = createInternalEvent(team.id, {})+ event.event.event = '$insight_alert_firing'++ const globals = await processor._parseKafkaBatch([createKafkaMessage(event)])+ const { invocations } = await processor.processBatch(globals)++ expect(invocations.map((invocation) => invocation.functionId)).toEqual([fn.id])+ })})})diff --git a/nodejs/src/cdp/managed-alert-events.ts b/nodejs/src/cdp/managed-alert-events.tsnew file mode 100644index 000000000000..6875776a9c1e--- /dev/null+++ b/nodejs/src/cdp/managed-alert-events.ts@@ -0,0 +1,13 @@+// Mirror of `posthog/cdp/internal_events.py`. The managed-alert event boundary and the legacy+// insight-alert exemption must match the Python side exactly, so keep the two in sync.+export const MANAGED_ALERT_EVENT_PATTERN = /^\$[a-z0-9_]+_alert_(firing|resolved|errored|auto_disabled)$/+export const LEGACY_INSIGHT_ALERT_EVENT = '$insight_alert_firing'++/** Return whether an internal event is reserved for an alert-owned destination. */+export function isManagedAlertInternalEvent(eventName: unknown): boolean {+ return (+ typeof eventName === 'string' &&+ eventName !== LEGACY_INSIGHT_ALERT_EVENT &&+ MANAGED_ALERT_EVENT_PATTERN.test(eventName)+ )+}