@@ -24,14 +24,17 @@ import { init } from './sdk';
24
24
25
25
const UUID_REGEX = / ^ [ 0 - 9 a - f ] { 8 } - ? [ 0 - 9 a - f ] { 4 } - ? [ 0 - 9 a - f ] { 4 } - ? [ 0 - 9 a - f ] { 4 } - ? [ 0 - 9 a - f ] { 12 } $ / i;
26
26
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
+ }
32
35
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 ) ;
35
38
36
39
// Derive sampleRand from last 4 characters of the random UUID
37
40
//
@@ -60,7 +63,7 @@ async function workflowStepWithSentry<V>(
60
63
addCloudResourceContext ( isolationScope ) ;
61
64
62
65
return withScope ( async scope => {
63
- const propagationContext = propagationContextFromInstanceId ( instanceId ) ;
66
+ const propagationContext = await propagationContextFromInstanceId ( instanceId ) ;
64
67
scope . setPropagationContext ( propagationContext ) ;
65
68
66
69
// eslint-disable-next-line no-return-await
0 commit comments