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 how awaited steps finish (#100315)

Harley Alexanderad8b3284340e
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 929f5c8d2d98..f689d8c46ff3 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.test.ts
@@ -1,6 +1,7 @@
import { mockFetch } from '~/tests/helpers/mocks/request.mock'
import { DateTime } from 'luxon'
+import { register } from 'prom-client'
import { FixtureHogFlowBuilder } from '~/cdp/_tests/builders/hogflow.builder'
import { insertHogFunctionTemplate, insertIntegration } from '~/cdp/_tests/fixtures'
@@ -847,7 +848,13 @@ describe('HogFunctionHandler', () => {
let executeSpy: jest.SpyInstance
const deadlineAt = DateTime.now().plus({ hours: 1 }).toISO()!
+ const finishedCount = async (outcome: string): Promise<number> => {
+ const metric = await register.getSingleMetric('cdp_hogflow_awaited_step_finished')!.get()
+ return metric.values.find((v) => v.labels.outcome === outcome)?.value ?? 0
+ }
+
beforeEach(() => {
+ register.resetMetrics()
executeSpy = jest.spyOn(mockHogFlowFunctionsService, 'executeWithAsyncFunctions')
invocation.state.currentAction!.awaitingResume = {
key: dispatchKey,
@@ -904,6 +911,7 @@ describe('HogFunctionHandler', () => {
})
expect(invocationResult.invocation.state.currentAction?.awaitingResume).toBeUndefined()
expect(invocationResult.invocation.state.currentAction?.resumeResult).toBeUndefined()
+ expect(await finishedCount('completed')).toBe(1)
})
it.each([4000, 4700])('fits the resumed result with %s bytes of existing variables', async (usedBytes) => {
@@ -960,6 +968,7 @@ describe('HogFunctionHandler', () => {
error_message: 'sandbox crashed',
})
expect(executeSpy).not.toHaveBeenCalled()
+ expect(await finishedCount('failed')).toBe(1)
})
it('ignores a wake for an earlier visit and keeps waiting', async () => {
@@ -982,6 +991,7 @@ describe('HogFunctionHandler', () => {
.toISO()!
await expect(execute()).rejects.toThrow('Timed out waiting for the task to finish')
+ expect(await finishedCount('timed_out')).toBe(1)
})
})
})
diff --git a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
index dd9f7d7ae30d..cfcc3269f0b8 100644
--- a/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
+++ b/nodejs/src/cdp/services/hogflows/actions/hog_function.ts
@@ -87,6 +87,12 @@ const counterAwaitedStepStaleResume = new Counter({
help: 'A parked step received a wake keyed to an earlier visit of the same step and kept waiting.',
})
+const counterAwaitedStepFinished = new Counter({
+ name: 'cdp_hogflow_awaited_step_finished',
+ help: 'A parked step stopped waiting, by how: the job completed, failed or was cancelled, or the wait timed out.',
+ labelNames: ['outcome'],
+})
+
export class HogFunctionHandler implements ActionHandler {
constructor(
private hogFlowFunctionsService: HogFlowFunctionsService,
@@ -266,6 +272,7 @@ export class HogFunctionHandler implements ActionHandler {
if (resume?.key === awaiting.key) {
delete currentAction.awaitingResume
delete currentAction.resumeResult
+ counterAwaitedStepFinished.labels({ outcome: resume.status }).inc()
const payload = capWorkflowStepResult(
{ ...awaiting.dispatch, status: resume.status },
resume.result ?? {},
@@ -306,6 +313,7 @@ export class HogFunctionHandler implements ActionHandler {
}
const deadline = DateTime.fromISO(awaiting.deadlineAt)
if (DateTime.now() >= deadline) {
+ counterAwaitedStepFinished.labels({ outcome: 'timed_out' }).inc()
throw new Error(`Timed out waiting for the ${label} to finish`)
}
// Woken early with nothing (clock skew): park again.