Skip to content

Commit aecd1b2

Browse files
committed
fix(cloudflare): Allow non uuid workflow instance IDs
1 parent 726c868 commit aecd1b2

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/cloudflare/src/workflows.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ import { init } from './sdk';
2424

2525
const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/i;
2626

27-
function propagationContextFromInstanceId(instanceId: string): PropagationContext {
28-
// Validate and normalize traceId - should be a valid UUID with or without hyphens
29-
if (!UUID_REGEX.test(instanceId)) {
30-
throw new Error("Invalid 'instanceId' for workflow: Sentry requires random UUIDs for instanceId.");
31-
}
27+
async function hashStringToUuid(input: string): Promise<string> {
28+
const buf = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(input));
29+
return Array.from(new Uint8Array(buf))
30+
// We only need the first 16 bytes for the 32 characters
31+
.slice(0, 16)
32+
.map(b => b.toString(16).padStart(2, '0'))
33+
.join('');
34+
}
3235

33-
// Remove hyphens to get UUID without hyphens
34-
const traceId = instanceId.replace(/-/g, '');
36+
async function propagationContextFromInstanceId(instanceId: string): Promise<PropagationContext> {
37+
const traceId = UUID_REGEX.test(instanceId) ? instanceId.replace(/-/g, '') : await hashStringToUuid(instanceId);
3538

3639
// Derive sampleRand from last 4 characters of the random UUID
3740
//
@@ -60,7 +63,7 @@ async function workflowStepWithSentry<V>(
6063
addCloudResourceContext(isolationScope);
6164

6265
return withScope(async scope => {
63-
const propagationContext = propagationContextFromInstanceId(instanceId);
66+
const propagationContext = await propagationContextFromInstanceId(instanceId);
6467
scope.setPropagationContext(propagationContext);
6568

6669
// eslint-disable-next-line no-return-await

0 commit comments

Comments
 (0)