: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(hog-functions): load real issue into error tracking alert test payload (#72220)
Aleksander Błaszkiewicza3ad95741222
diff --git a/frontend/src/lib/components/Alerting/AlertWizard/alertWizardLogic.ts b/frontend/src/lib/components/Alerting/AlertWizard/alertWizardLogic.tsindex ddda17de02be..66e62a8f0f31 100644--- a/frontend/src/lib/components/Alerting/AlertWizard/alertWizardLogic.ts+++ b/frontend/src/lib/components/Alerting/AlertWizard/alertWizardLogic.ts@@ -7,6 +7,7 @@ import { lemonToast } from '@posthog/lemon-ui'import api from 'lib/api'import { HealthIssueKind, KIND_LABELS } from 'scenes/health/healthCategories'+import { SAMPLE_GLOBALS_CONTEXTS } from 'scenes/hog-functions/configuration/sampleGlobalsContexts'import {HOG_FUNCTION_SUB_TEMPLATES,HOG_FUNCTION_SUB_TEMPLATE_COMMON_PROPERTIES,@@ -15,6 +16,8 @@ import {import {CyclotronJobFiltersType,CyclotronJobInputType,+ CyclotronJobInvocationGlobals,+ HogFunctionConfigurationContextId,HogFunctionSubTemplateIdType,HogFunctionTemplateType,HogFunctionType,@@ -63,6 +66,9 @@ export interface AlertWizardLogicProps {// to one or more health-check kinds. Pass an empty array to mean "all kinds"// explicitly; omit the prop to leave filters untouched.presetTriggerKinds?: string[]+ // When set, the "Test" button populates the test event via the matching+ // SAMPLE_GLOBALS_CONTEXTS loader (real product data) instead of a stub event.+ contextId?: HogFunctionConfigurationContextIdonAlertCreated?: () => void}@@ -636,12 +642,14 @@ export const alertWizardLogic = kea<alertWizardLogicType>([hog: selectedTemplate.code,}- const globals = {+ let globals: CyclotronJobInvocationGlobals = {event: {uuid: 'test-event-uuid',distinct_id: 'test-distinct-id',timestamp: new Date().toISOString(),event: subTemplate.filters?.events?.[0]?.id || triggerKey,+ elements_chain: '',+ url: '',properties: {name: 'Test issue',description: 'This is a test alert from PostHog',@@ -658,6 +666,15 @@ export const alertWizardLogic = kea<alertWizardLogicType>([},}+ const sampleGlobalsLoader = logicProps.contextId ? SAMPLE_GLOBALS_CONTEXTS[logicProps.contextId] : undefined+ if (sampleGlobalsLoader) {+ try {+ globals = await sampleGlobalsLoader(globals)+ } catch {+ // Fall back to the stub test event+ }+ }+try {await api.hogFunctions.createTestInvocation('new', {configuration,diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsxindex 4056f3e69ca6..16e7d000314f 100644--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx@@ -85,6 +85,7 @@ import type { GroupType, GroupTypeIndex, HogFunctionMappingTemplateType, Projectimport type { TeamPublicType, TeamType } from '../../../types'import { performWideEventsQueryInTwoPhases } from '../sampleEventsQuery'import { eventToHogFunctionContextId } from '../sub-templates/sub-templates'+import { SAMPLE_GLOBALS_CONTEXTS } from './sampleGlobalsContexts'export interface HogFunctionConfigurationLogicProps {logicKey?: string@@ -778,7 +779,10 @@ export interface hogFunctionConfigurationLogicMeta {newInputs: CyclotronJobInputSchemaType[]oldInputs: CyclotronJobInputSchemaType[]} | null- canLoadSampleGlobals: (lastEventQuery: EventsQuery | null) => boolean+ canLoadSampleGlobals: (+ lastEventQuery: EventsQuery | null,+ contextId: HogFunctionConfigurationContextId+ ) => booleanshowFilters: (type: HogFunctionTypeType) => booleanshowExpectedVolume: (type: HogFunctionTypeType, sourceUsesEvents: boolean) => booleancanEditSource: (@@ -1105,6 +1109,21 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTynull as CyclotronJobInvocationGlobals | null,{loadSampleGlobals: async ({ eventId }, breakpoint) => {+ const sampleGlobalsLoader = SAMPLE_GLOBALS_CONTEXTS[values.contextId]+ if (sampleGlobalsLoader) {+ try {+ const globals = await sampleGlobalsLoader(values.exampleInvocationGlobals)+ breakpoint()+ return globals+ } catch (e: any) {+ if (isBreakpoint(e)) {+ // Superseded by a newer load — abort without dispatching a result+ throw e+ }+ actions.setSampleGlobalsError(e.message)+ return values.exampleInvocationGlobals+ }+ }if (!values.lastEventQuery) {return values.sampleGlobals}@@ -1791,9 +1810,9 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy],canLoadSampleGlobals: [- (s) => [s.lastEventQuery],- (lastEventQuery: EventsQuery | null) => {- return !!lastEventQuery+ (s) => [s.lastEventQuery, s.contextId],+ (lastEventQuery: EventsQuery | null, contextId: HogFunctionConfigurationContextId) => {+ return !!lastEventQuery || !!SAMPLE_GLOBALS_CONTEXTS[contextId]},],diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsxindex 9a44f22baf5b..6eae7b215a0b 100644--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionTestLogic.tsx@@ -12,12 +12,19 @@ import { tryJsonParse } from 'lib/utils/json'import { groupsModel } from '~/models/groupsModel'import { CyclotronJobInvocationGlobals, CyclotronJobTestInvocationResult } from '~/types'-import type { GroupType, GroupTypeIndex, HogFunctionConfigurationType, HogFunctionTypeType } from '../../../types'+import type {+ GroupType,+ GroupTypeIndex,+ HogFunctionConfigurationContextId,+ HogFunctionConfigurationType,+ HogFunctionTypeType,+} from '../../../types'import {HogFunctionConfigurationLogicProps,hogFunctionConfigurationLogic,sanitizeConfiguration,} from './hogFunctionConfigurationLogic'+import { SAMPLE_GLOBALS_CONTEXTS } from './sampleGlobalsContexts'export type HogFunctionTestInvocationForm = {globals: string // CyclotronJobInvocationGlobals@@ -71,6 +78,7 @@ export interface hogFunctionTestLogicValues {groupTypes: Map<GroupTypeIndex, GroupType> // groupsModelconfiguration: HogFunctionConfigurationType // hogFunctionConfigurationLogicconfigurationHasErrors: boolean // hogFunctionConfigurationLogic+ contextId: HogFunctionConfigurationContextId // hogFunctionConfigurationLogiccurrentHogCode: string // hogFunctionConfigurationLogicexampleInvocationGlobals: CyclotronJobInvocationGlobals // hogFunctionConfigurationLogicsampleGlobals: CyclotronJobInvocationGlobals | null // hogFunctionConfigurationLogic@@ -245,6 +253,7 @@ export const hogFunctionTestLogic = kea<hogFunctionTestLogicType>(['configuration','templateId','configurationHasErrors',+ 'contextId','sampleGlobals','sampleGlobalsLoading','exampleInvocationGlobals',@@ -333,6 +342,13 @@ export const hogFunctionTestLogic = kea<hogFunctionTestLogicType>([],}),listeners(({ values, actions }) => ({+ toggleExpanded: () => {+ // Contexts with a sample globals loader fetch real product data as soon as the+ // test panel opens, instead of waiting for a "Load new event" click.+ if (values.expanded && !values.sampleGlobals && SAMPLE_GLOBALS_CONTEXTS[values.contextId]) {+ actions.loadSampleGlobals()+ }+ },loadSampleGlobalsSuccess: () => {if (values.expanded && !values.fetchCancelled && values.sampleGlobals) {actions.receiveExampleGlobals(values.sampleGlobals)diff --git a/frontend/src/scenes/hog-functions/configuration/sampleGlobalsContexts.ts b/frontend/src/scenes/hog-functions/configuration/sampleGlobalsContexts.tsnew file mode 100644index 000000000000..6c2bd09d0c84--- /dev/null+++ b/frontend/src/scenes/hog-functions/configuration/sampleGlobalsContexts.ts@@ -0,0 +1,50 @@+import { ApiConfig } from 'lib/api'++import { CyclotronJobInvocationGlobals, HogFunctionConfigurationContextId } from '~/types'++import {+ errorTrackingFingerprintsList,+ errorTrackingIssuesRetrieve,+} from 'products/error_tracking/frontend/generated/api'++export type SampleGlobalsLoader = (+ exampleGlobals: CyclotronJobInvocationGlobals+) => Promise<CyclotronJobInvocationGlobals>++/**+ * Per-context overrides for the "load sample globals" flow in the hog function test panel.+ * Contexts without an entry load the last event matching the configured filters.+ */+export const SAMPLE_GLOBALS_CONTEXTS: Partial<Record<HogFunctionConfigurationContextId, SampleGlobalsLoader>> = {+ 'error-tracking': async (exampleGlobals) => {+ const projectId = String(ApiConfig.getCurrentProjectId())+ // The issues list API doesn't expose fingerprints, so start from a fingerprint+ // record (which alert templates rely on) and resolve its issue.+ const response = await errorTrackingFingerprintsList(projectId, { limit: 20 })+ const fingerprintRecord = response.results[Math.floor(Math.random() * response.results.length)]+ if (!fingerprintRecord) {+ return exampleGlobals+ }+ const issue = await errorTrackingIssuesRetrieve(projectId, fingerprintRecord.issue_id)+ const properties: Record<string, any> = {+ name: issue.name ?? 'Unnamed issue',+ description: 'PostHog test alert',+ status: issue.status,+ fingerprint: fingerprintRecord.fingerprint,+ }+ if (issue.assignee) {+ // Real issue lifecycle events stringify the assignee as {"type":...,"id":...},+ // and omit the property entirely when the issue is unassigned+ properties.assignee = JSON.stringify({ type: issue.assignee.type, id: issue.assignee.id })+ }+ return {+ ...exampleGlobals,+ event: {+ ...exampleGlobals.event,+ // Real issue lifecycle events use the issue id as the distinct_id+ distinct_id: issue.id,+ properties,+ },+ }+ },+}diff --git a/products/error_tracking/frontend/scenes/ErrorTrackingConfigurationScene/alerting/ErrorTrackingAlerting.tsx b/products/error_tracking/frontend/scenes/ErrorTrackingConfigurationScene/alerting/ErrorTrackingAlerting.tsxindex 6b26cbb0470b..a7b5bcc06f3d 100644--- a/products/error_tracking/frontend/scenes/ErrorTrackingConfigurationScene/alerting/ErrorTrackingAlerting.tsx+++ b/products/error_tracking/frontend/scenes/ErrorTrackingConfigurationScene/alerting/ErrorTrackingAlerting.tsx@@ -31,6 +31,7 @@ export function ErrorTrackingAlerting(): JSX.Element {subTemplateIds: ERROR_TRACKING_SUB_TEMPLATE_IDS,triggers: ERROR_TRACKING_TRIGGERS,destinations: ERROR_TRACKING_DESTINATIONS,+ contextId: 'error-tracking',}return (diff --git a/products/error_tracking/frontend/scenes/ErrorTrackingScene/tabs/recommendations/AlertsRecommendationCard.tsx b/products/error_tracking/frontend/scenes/ErrorTrackingScene/tabs/recommendations/AlertsRecommendationCard.tsxindex 763e2e697fb4..d2f03c226f3b 100644--- a/products/error_tracking/frontend/scenes/ErrorTrackingScene/tabs/recommendations/AlertsRecommendationCard.tsx+++ b/products/error_tracking/frontend/scenes/ErrorTrackingScene/tabs/recommendations/AlertsRecommendationCard.tsx@@ -117,6 +117,7 @@ function AlertsRecommendationWizardModal({destinations: ERROR_TRACKING_DESTINATIONS,disableUrlSync: true,presetTriggerKey: triggerKey,+ contextId: 'error-tracking',onAlertCreated: onClose,}