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): record how long an awaited step waited (#100805)

Harley Alexanderec60ec72f4cc
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
index f689d8c46ff3..fd5f7e28b69c 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -783,6 +783,7 @@ describe('HogFunctionHandler', () => {
deadlineAt: handlerResult.scheduledAt!.toISO(),
dispatch: { id: 't1', run_id: 'r1' },
label: 'task',
+ parkedAt: expect.any(String),
})
expect(invocationResult.metrics.map((m) => m.metric_name)).toContain('billable_invocation')
expect(invocationResult.logs.map((l) => l.message)).toContainEqual(
@@ -861,6 +862,7 @@ describe('HogFunctionHandler', () => {
deadlineAt,
dispatch: { id: 't1', run_id: 'r1' },
label: 'task',
+ parkedAt: DateTime.now().minus({ minutes: 5 }).toISO()!,
}
})
@@ -912,6 +914,13 @@ describe('HogFunctionHandler', () => {
expect(invocationResult.invocation.state.currentAction?.awaitingResume).toBeUndefined()
expect(invocationResult.invocation.state.currentAction?.resumeResult).toBeUndefined()
expect(await finishedCount('completed')).toBe(1)
+ const waited = await register.getSingleMetric('cdp_hogflow_awaited_step_wait_seconds')!.get()
+ // prom-client types histogram values without metricName, but the runtime sets it.
+ const sum = waited.values.find(
+ (v) =>
+ (v as { metricName?: string }).metricName?.endsWith('_sum') && v.labels.outcome === 'completed'
+ )
+ expect(sum?.value).toBeGreaterThanOrEqual(300)
})
it.each([4000, 4700])('fits the resumed result with %s bytes of existing variables', async (usedBytes) => {
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
index cfcc3269f0b8..575f32d8110b 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
@@ -1,5 +1,5 @@
import { DateTime, Duration } from 'luxon'
-import { Counter } from 'prom-client'
+import { Counter, Histogram } from 'prom-client'
import { HogFlowAction } from '~/cdp/schema/hogflow'
import {
@@ -93,6 +93,21 @@ const counterAwaitedStepFinished = new Counter({
labelNames: ['outcome'],
})
+const histogramAwaitedStepWaitSeconds = new Histogram({
+ name: 'cdp_hogflow_awaited_step_wait_seconds',
+ help: 'How long a parked step waited before it stopped, by outcome.',
+ labelNames: ['outcome'],
+ buckets: [30, 60, 120, 300, 600, 1200, 1800, 3600, 7200, 10800],
+})
+
+const observeAwaitedStepFinished = (outcome: string, awaiting: AwaitingResume): void => {
+ counterAwaitedStepFinished.labels({ outcome }).inc()
+ if (awaiting.parkedAt) {
+ const waited = DateTime.now().diff(DateTime.fromISO(awaiting.parkedAt), 'seconds').seconds
+ histogramAwaitedStepWaitSeconds.labels({ outcome }).observe(Math.max(0, waited))
+ }
+}
+
export class HogFunctionHandler implements ActionHandler {
constructor(
private hogFlowFunctionsService: HogFlowFunctionsService,
@@ -250,6 +265,7 @@ export class HogFunctionHandler implements ActionHandler {
deadlineAt: deadline.toISO()!,
dispatch,
label: awaitRequest.label,
+ parkedAt: DateTime.now().toISO()!,
}
result.logs.push({
level: 'info',
@@ -272,7 +288,7 @@ export class HogFunctionHandler implements ActionHandler {
if (resume?.key === awaiting.key) {
delete currentAction.awaitingResume
delete currentAction.resumeResult
- counterAwaitedStepFinished.labels({ outcome: resume.status }).inc()
+ observeAwaitedStepFinished(resume.status, awaiting)
const payload = capWorkflowStepResult(
{ ...awaiting.dispatch, status: resume.status },
resume.result ?? {},
@@ -313,7 +329,7 @@ export class HogFunctionHandler implements ActionHandler {
}
const deadline = DateTime.fromISO(awaiting.deadlineAt)
if (DateTime.now() >= deadline) {
- counterAwaitedStepFinished.labels({ outcome: 'timed_out' }).inc()
+ observeAwaitedStepFinished('timed_out', awaiting)
throw new Error(`Timed out waiting for the ${label} to finish`)
}
// Woken early with nothing (clock skew): park again.
diff --git a/nodejs/src/cdp/types.ts b/nodejs/src/cdp/types.ts
index b58bb38d4d26..b1c7c3e95365 100644
--- a/nodejs/src/cdp/types.ts
+++ b/nodejs/src/cdp/types.ts
@@ -485,7 +485,13 @@ export type HogFlowInvocationContext = {
// ever catches a wake the subscription streams missed, gating its eventual removal.
pollReparked?: boolean
// A step parked on an external run: cleared when the matcher writes a matching `resumeResult`.
- awaitingResume?: { key: string; deadlineAt: string; dispatch: Record<string, unknown>; label?: string }
+ awaitingResume?: {
+ key: string
+ deadlineAt: string
+ dispatch: Record<string, unknown>
+ label?: string
+ parkedAt?: string
+ }
resumeResult?: { key: string; status: WorkflowStepResumeStatus; result?: Record<string, unknown> }
}
// Set by the subscription matcher consumer when an incoming event matched the