Skip to content

: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.

feat(cdp): label filter errors by the code path that raised them (#102765)

Meikel Ratz2aba70137099
diff --git a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
index 1182e6349e96..a6d1da04a8d5 100644
--- a/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
+++ b/nodejs/src/cdp/hog-transformations/hog-transformer.service.ts
@@ -190,6 +190,7 @@ export class HogTransformerService implements HogTransformer {
// Check if function has filters - if not, always apply
if (hogFunction.filters?.bytecode) {
const filterResults = await filterFunctionInstrumented({
+ caller: 'transformation',
fn: hogFunction,
filters: hogFunction.filters,
filterGlobals,
diff --git a/nodejs/src/cdp/services/hog-executor.service.ts b/nodejs/src/cdp/services/hog-executor.service.ts
index 99cf467407ed..7e95f2cc3a02 100644
--- a/nodejs/src/cdp/services/hog-executor.service.ts
+++ b/nodejs/src/cdp/services/hog-executor.service.ts
@@ -125,6 +125,7 @@ export class HogExecutorService {
continue
}
const { match } = await filterFunctionInstrumented({
+ caller: 'execute_hog_function',
fn: hogFunction,
filters: mapping.filters,
filterGlobals,
diff --git a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
index a3707574e0e2..878a66f27412 100644
--- a/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/conditional_branch.ts
@@ -211,6 +211,7 @@ export async function checkConditions(
// TODO(team-workflows): Figure out error handling here - do we throw or just move on to other conditions?
const filterResults = await filterFunctionInstrumented({
+ caller: 'hogflow_conditional_branch',
fn: invocation.hogFlow,
filters: condition.filters,
filterGlobals: {
diff --git a/nodejs/src/cdp/services/hogflows/actions/trigger.handler.ts b/nodejs/src/cdp/services/hogflows/actions/trigger.handler.ts
index 7be5f04e1858..6b2a3225379f 100644
--- a/nodejs/src/cdp/services/hogflows/actions/trigger.handler.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/trigger.handler.ts
@@ -117,6 +117,7 @@ export class TriggerHandler implements ActionHandler {
fn: invocation.hogFlow,
filters: trigger.filters,
filterGlobals: invocation.filterGlobals,
+ caller: 'hogflow_trigger_action',
})
if (filterResults.error) {
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts
index 9f357a71512f..f22f6e5f3a51 100644
--- a/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts
+++ b/nodejs/src/cdp/services/hogflows/hogflow-executor.service.ts
@@ -207,6 +207,7 @@ export class HogFlowExecutorService {
fn: hogFlow,
filters: trigger.filters,
filterGlobals,
+ caller: 'build_hogflow_invocations',
})
// Add any generated metrics and logs to our collections. These are queued straight by the
@@ -342,6 +343,7 @@ export class HogFlowExecutorService {
fn: hogFlow,
filters: { bytecode: hogFlow.conversion.bytecode, properties: hogFlow.conversion.filters },
filterGlobals: invocation.filterGlobals,
+ caller: 'hogflow_conversion',
})
if (!filterResult.match) {
return null
@@ -460,12 +462,14 @@ export class HogFlowExecutorService {
fn: hogFlow,
filters: hogFlow.trigger.filters,
filterGlobals: invocation.filterGlobals,
+ caller: 'hogflow_exit_condition',
})
triggerMatch = filterResult.match
}
if (hogFlow.conversion?.filters?.length && person) {
if (hogFlow.conversion.bytecode?.length) {
const filterResult = await filterFunctionInstrumented({
+ caller: 'hogflow_exit_condition',
fn: hogFlow,
filters: {
bytecode: hogFlow.conversion.bytecode || [],
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-utils.ts b/nodejs/src/cdp/services/hogflows/hogflow-utils.ts
index 9a4b0fe40cfd..260b917b61db 100644
--- a/nodejs/src/cdp/services/hogflows/hogflow-utils.ts
+++ b/nodejs/src/cdp/services/hogflows/hogflow-utils.ts
@@ -103,6 +103,7 @@ export async function shouldSkipAction(
fn: invocation.hogFlow,
filters: action.filters,
filterGlobals: invocation.filterGlobals,
+ caller: 'hogflow_skip_action',
})
return !filterResults.match
diff --git a/nodejs/src/cdp/utils/hog-function-filtering.test.ts b/nodejs/src/cdp/utils/hog-function-filtering.test.ts
index 509d38eda867..6f225f357310 100644
--- a/nodejs/src/cdp/utils/hog-function-filtering.test.ts
+++ b/nodejs/src/cdp/utils/hog-function-filtering.test.ts
@@ -267,6 +267,7 @@ describe('hog-function-filtering', () => {
// Test with an event that doesn't match any specific event names
mockFilterGlobals.event = '$pageview'
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
@@ -292,6 +293,7 @@ describe('hog-function-filtering', () => {
// Test with an event that matches one of the specific events
mockFilterGlobals.event = 'change_order_generated'
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
@@ -316,6 +318,7 @@ describe('hog-function-filtering', () => {
mockFilterGlobals.event = 'change_order_generated'
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
@@ -330,6 +333,7 @@ describe('hog-function-filtering', () => {
}
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
@@ -349,6 +353,7 @@ describe('hog-function-filtering', () => {
}
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
@@ -390,6 +395,7 @@ describe('hog-function-filtering', () => {
mockFilterGlobals.timestamp = timestamp
const result = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: mockHogFunction,
filters: mockHogFunction.filters,
filterGlobals: mockFilterGlobals,
diff --git a/nodejs/src/cdp/utils/hog-function-filtering.ts b/nodejs/src/cdp/utils/hog-function-filtering.ts
index 0cebb18e7800..ce94fb793a07 100644
--- a/nodejs/src/cdp/utils/hog-function-filtering.ts
+++ b/nodejs/src/cdp/utils/hog-function-filtering.ts
@@ -44,6 +44,23 @@ const hogFunctionFilterOutcomes = new Counter({
labelNames: ['result', 'result_type'],
})
+/**
+ * Where a filter threw, rather than how many threw.
+ *
+ * This counts every exception the catch block below sees. The `filtering_failed` app metric is
+ * pushed onto the returned `metrics` array in that same block, but most callers discard the array.
+ * This counter therefore covers a strictly larger population than the app metric, so read its
+ * per-caller rate on its own rather than as a share of that metric.
+ *
+ * That rate is what the dead-letter work needs, because only the two `build_*` callers turn a
+ * filter error into a parked record.
+ */
+const hogFunctionFilterErrors = new Counter({
+ name: 'cdp_hog_function_filter_error',
+ help: 'A filter threw while being evaluated, by the code path that asked for it',
+ labelNames: ['caller', 'type'],
+})
+
const hogFunctionPreFilterCounter = new Counter({
name: 'cdp_hog_function_prefilter_result',
help: 'Count of pre-filter results',
@@ -347,13 +364,32 @@ function preFilterResult(filters: HogFunctionType['filters'], filterGlobals: Hog
* Shared utility to check if an event matches the filters of a HogFunction.
* Used by both the HogExecutorService (for destinations) and HogTransformerService (for transformations).
*/
+/**
+ * Named so a filter error can be attributed to the code path that asked for it.
+ *
+ * Only the two `build_*` callers run before an invocation exists, so only their errors become
+ * dead-letter records. Everything else here is either the ingestion transformer or a filter
+ * evaluated while an invocation is already running.
+ */
+export type FilterCaller =
+ | 'build_hog_function_invocations'
+ | 'build_hogflow_invocations'
+ | 'execute_hog_function'
+ | 'hogflow_conditional_branch'
+ | 'hogflow_conversion'
+ | 'hogflow_exit_condition'
+ | 'hogflow_skip_action'
+ | 'hogflow_trigger_action'
+ | 'transformation'
+
export async function filterFunctionInstrumented(options: {
fn: HogFunctionType | HogFlow
filterGlobals: HogFunctionFilterGlobals
/** Optional filters to use instead of those on the function */
filters: HogFunctionType['filters']
+ caller: FilterCaller
}): Promise<HogFilterResult> {
- const { fn, filters, filterGlobals } = options
+ const { fn, filters, filterGlobals, caller } = options
const type = 'type' in fn ? fn.type : 'hogflow'
const fnKind = 'type' in fn ? 'HogFunction' : 'HogFlow'
const logs: LogEntry[] = []
@@ -439,6 +475,8 @@ export async function filterFunctionInstrumented(options: {
})
}
} catch (error) {
+ hogFunctionFilterErrors.inc({ caller, type })
+
logger.debug('🦔', `[${fnKind}] Error filtering function`, {
functionId: fn.id,
functionName: fn.name,
diff --git a/nodejs/src/cdp/utils/invocation-utils.ts b/nodejs/src/cdp/utils/invocation-utils.ts
index 6e6d3f5bf560..e6e2c2319012 100644
--- a/nodejs/src/cdp/utils/invocation-utils.ts
+++ b/nodejs/src/cdp/utils/invocation-utils.ts
@@ -62,6 +62,7 @@ export async function buildHogFunctionInvocations(
filterGlobals: HogFunctionFilterGlobals
): Promise<boolean> => {
const filterResults = await filterFunctionInstrumented({
+ caller: 'build_hog_function_invocations',
fn: hogFunction,
filters,
filterGlobals,