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(workflows): count runs whose conversion window hits the cap (#92672)

Daniel Marchuk03dc959cc622
diff --git a/nodejs/src/cdp/services/hogflows/conversion-watcher.ts b/nodejs/src/cdp/services/hogflows/conversion-watcher.ts
index 288fab48c1d7..9a2cfda744fb 100644
--- a/nodejs/src/cdp/services/hogflows/conversion-watcher.ts
+++ b/nodejs/src/cdp/services/hogflows/conversion-watcher.ts
@@ -1,3 +1,5 @@
+import { Counter } from 'prom-client'
+
import { HogFlow } from '~/cdp/schema/hogflow'
import { ConversionWatcherRow, CyclotronJobInvocationHogFlow, PinnedConversionGoal } from '../../types'
@@ -65,10 +67,21 @@ export function buildConversionWatcher(invocation: CyclotronJobInvocationHogFlow
// proportional to recent traffic rather than to all traffic ever.
export const MAX_CONVERSION_WINDOW_MINUTES = 30 * 24 * 60
+// Substituting our window for the configured one changes what the workflow's conversion rate measures,
+// so it must not be silent: a clamped run reports over the cap, not over the window it asked for.
+const counterConversionWindowClamped = new Counter({
+ name: 'cdp_conversion_window_clamped',
+ help: 'Runs enrolled with a conversion window shortened to the cap because the workflow configured a longer one. Each one measures its conversion rate over a shorter period than the workflow asked for.',
+})
+
function conversionWindowMinutes(hogFlow: HogFlow): number {
const configured = hogFlow.conversion?.window_minutes
if (!configured || configured <= 0) {
return MAX_CONVERSION_WINDOW_MINUTES
}
- return Math.min(configured, MAX_CONVERSION_WINDOW_MINUTES)
+ if (configured > MAX_CONVERSION_WINDOW_MINUTES) {
+ counterConversionWindowClamped.inc()
+ return MAX_CONVERSION_WINDOW_MINUTES
+ }
+ return configured
}
diff --git a/nodejs/src/cdp/services/hogflows/hogflow-conversion-watcher.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-conversion-watcher.test.ts
index d39a3fde255f..647f1b381d5c 100644
--- a/nodejs/src/cdp/services/hogflows/hogflow-conversion-watcher.test.ts
+++ b/nodejs/src/cdp/services/hogflows/hogflow-conversion-watcher.test.ts
@@ -1,3 +1,5 @@
+import { register } from 'prom-client'
+
import { HogFlow } from '~/cdp/schema/hogflow'
import { createExampleHogFlowInvocation } from '../../_tests/fixtures-hogflows'
@@ -85,6 +87,21 @@ describe('buildConversionWatcher', () => {
expect(minutes).toBeLessThan(expectedMinutes + 1)
})
+ it('counts a run whose window was shortened to the cap, and only then', async () => {
+ // The clamp changes what the workflow's conversion rate measures. Without this counter the
+ // substitution leaves no trace anywhere, which is how it went unnoticed in the first place.
+ const clamped = async (): Promise<number> =>
+ ((await register.getSingleMetric('cdp_conversion_window_clamped')?.get())?.values[0]?.value as number) ?? 0
+
+ const before = await clamped()
+
+ buildConversionWatcher(invocationFor({ ...propertyGoal, window_minutes: MAX_CONVERSION_WINDOW_MINUTES + 1 }))
+ expect(await clamped()).toBe(before + 1)
+
+ buildConversionWatcher(invocationFor({ ...propertyGoal, window_minutes: MAX_CONVERSION_WINDOW_MINUTES }))
+ expect(await clamped()).toBe(before + 1)
+ })
+
it('does not build a watcher for a run that has already started', () => {
// A run with several delays would otherwise enroll once per wake, inflating the denominator
// and the row count together.