Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/runtime/step-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from '../serialization.js';
import { contextStorage } from '../step/context-storage.js';
import * as Attribute from '../telemetry/semantic-conventions.js';
import { trace } from '../telemetry.js';
import { recordStepExecutionDuration, trace } from '../telemetry.js';
import {
getErrorName,
getErrorStack,
Expand Down Expand Up @@ -967,6 +967,8 @@ export async function executeStep(
span?.setAttributes(attributes);
};

let stepExecutionStatus: 'ok' | 'error' = 'ok';
const stepExecutionStartTime = performance.now();
try {
result = await trace('step.execute', {}, async () => {
return await contextStorage.run(
Expand Down Expand Up @@ -1006,8 +1008,14 @@ export async function executeStep(
);
});
} catch (err) {
stepExecutionStatus = 'error';
userCodeError = err;
userCodeFailed = true;
} finally {
void recordStepExecutionDuration(
performance.now() - stepExecutionStartTime,
stepExecutionStatus
);
}
const executionTimeMs = Date.now() - executionStartTime;

Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/telemetry-metrics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { metrics as otelMetrics } from '@opentelemetry/api';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { recordStepExecutionDuration } from './telemetry.js';

const histogram = { record: vi.fn() };
const meter = { createHistogram: vi.fn(() => histogram) };
const provider = { getMeter: vi.fn(() => meter) };

otelMetrics.setGlobalMeterProvider(provider as any);

afterAll(() => {
otelMetrics.disable();
});

describe('recordStepExecutionDuration', () => {
it('records a millisecond histogram with only a bounded status dimension', async () => {
await recordStepExecutionDuration(125, 'ok');

expect(meter.createHistogram).toHaveBeenCalledWith(
'workflow.step.execute.duration',
{
description: 'Duration of user step execution',
unit: 'ms',
}
);
expect(histogram.record).toHaveBeenCalledWith(125, {
'workflow.step.status': 'ok',
});
});
});
25 changes: 25 additions & 0 deletions packages/core/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ const Tracer = once(async () => {
return tracer;
});

const StepExecutionDurationHistogram = once(async () => {
const otel = await OtelApi.value;
if (!otel) return null;
// service.name is a resource attribute, applied by the configured provider.
return otel.metrics
.getMeter('workflow')
.createHistogram('workflow.step.execute.duration', {
description: 'Duration of user step execution',
unit: 'ms',
});
});

/**
* One-shot runtime diagnostic (DEBUG=workflow:* only), same shape as the one
* world-vercel emits tagged `world-vercel`: prints how this module instance
Expand Down Expand Up @@ -278,6 +290,19 @@ export async function recordElapsedSpan(
tracer.startSpan(spanName, { ...opts, startTime: startEpochMs }).end();
}

/**
* Records the same user-code interval as the inner `step.execute` span. The
* configured OpenTelemetry meter provider attaches resource dimensions such as
* service.name, so this avoids accepting a caller-controlled service tag.
*/
export async function recordStepExecutionDuration(
durationMs: number,
status: 'ok' | 'error'
): Promise<void> {
const histogram = await StepExecutionDurationHistogram.value;
histogram?.record(durationMs, { 'workflow.step.status': status });
}

/**
* Applies the workflow suspension algebraic effect to an active span.
*/
Expand Down
Loading