1212 * so we just hand the resulting header dict back to `tracer.extract` here.
1313 */
1414
15- import { randomBytes } from "crypto" ;
1615import { logDebug } from "../../../utils" ;
1716import { SpanContextWrapper } from "../../span-context-wrapper" ;
1817import { TracerWrapper } from "../../tracer-wrapper" ;
@@ -107,21 +106,6 @@ export function getCheckpointToken(event: unknown): string | undefined {
107106 return event . CheckpointToken ;
108107}
109108
110- function generateRandomPositiveId ( ) : string {
111- const bytes = randomBytes ( 8 ) ;
112- bytes [ 0 ] = bytes [ 0 ] & 0x7f ; // keep it positive int64
113- const value = bufferToBigInt ( bytes ) ;
114- return value === 0n ? "1" : value . toString ( 10 ) ;
115- }
116-
117- function bufferToBigInt ( buf : Buffer ) : bigint {
118- let result = 0n ;
119- for ( let i = 0 ; i < buf . length ; i ++ ) {
120- result = ( result << 8n ) | BigInt ( buf [ i ] ) ;
121- }
122- return result ;
123- }
124-
125109// Terminal operation statuses that indicate an operation has completed
126110const TERMINAL_STATUSES = new Set ( [ "SUCCEEDED" , "FAILED" , "CANCELLED" , "STOPPED" , "TIMED_OUT" ] ) ;
127111
@@ -362,25 +346,21 @@ export function createDurableExecutionRootSpan(
362346 return null ;
363347 }
364348
365- const rootSpanId = extractedRootContext ?. toSpanId ( ) || generateRandomPositiveId ( ) ;
366-
367- // Determine consistent start_time from the first operation's StartTimestamp
368- // StartTimestamp is unix milliseconds from the durable execution SDK
349+ // Use the first operation's StartTimestamp (unix milliseconds) so the root
350+ // span's start time matches the actual start of the durable execution.
369351 let startTime : number | undefined ;
370- const replayOperations = event . InitialExecutionState ?. Operations ;
371- if ( replayOperations && replayOperations . length > 0 ) {
372- const firstStartTs = replayOperations [ 0 ] . StartTimestamp ;
352+ if ( operations && operations . length > 0 ) {
353+ const firstStartTs = operations [ 0 ] . StartTimestamp ;
373354 if ( firstStartTs != null ) {
374355 const parsed = Number ( firstStartTs ) ;
375356 if ( ! isNaN ( parsed ) ) {
376- startTime = parsed ; // already in millis, dd-trace startSpan expects millis
357+ startTime = parsed ;
377358 }
378359 }
379360 }
380361
381362 try {
382363 const tracer = require ( "dd-trace" ) ;
383- const id = require ( "dd-trace/packages/dd-trace/src/id" ) ;
384364
385365 const serviceName = process . env . DD_DURABLE_EXECUTION_SERVICE || "aws.durable-execution" ;
386366 const resourceName = executionArn . includes ( ":" ) ? executionArn . split ( ":" ) . pop ( ) : executionArn ;
@@ -392,48 +372,22 @@ export function createDurableExecutionRootSpan(
392372 "resource.name" : resourceName ,
393373 "durable.execution_arn" : executionArn ,
394374 "durable.is_root_span" : true ,
395- "durable.invocation_count" : replayOperations ?. length ?? 0 ,
375+ "durable.invocation_count" : operations ?. length ?? 0 ,
396376 } ,
397377 } ;
398378
399379 if ( startTime !== undefined ) {
400380 spanOptions . startTime = startTime ;
401381 }
402382 if ( extractedRootContext ?. spanContext ) {
403- // Ensure the durable root span stays in the same trace as the extracted
404- // durable invocation context even when there is no active scope .
383+ // Stay in the same trace as the upstream caller even when there is no
384+ // active scope yet. aws.lambda will be parented to this span downstream .
405385 spanOptions . childOf = extractedRootContext . spanContext ;
406386 }
407387
408388 const span = tracer . startSpan ( "aws.durable-execution" , spanOptions ) ;
409389
410- // Use the extracted durable root span_id when available to keep the
411- // durable root identity stable with propagated checkpoint context.
412- try {
413- if ( rootSpanId ) {
414- span . context ( ) . _spanId = id ( rootSpanId , 10 ) ;
415- }
416- } catch ( e ) {
417- logDebug ( `Failed to set durable root span_id: ${ e } ` ) ;
418- }
419-
420- // Fix parent_id: when an extracted span context exists, tracer.startSpan()
421- // inherits its span_id as parent_id and we just overwrote our own span_id
422- // to match — that would self-parent. The root span's parent should be the
423- // upstream caller (if any) or 0 (true root).
424- try {
425- const upstreamHeaders = findUpstreamHeaders ( event as DurableExecutionEvent ) ;
426- const upstreamParentId = upstreamHeaders ?. [ "x-datadog-parent-id" ] ;
427- if ( upstreamParentId ) {
428- span . context ( ) . _parentId = id ( String ( upstreamParentId ) , 10 ) ;
429- } else {
430- span . context ( ) . _parentId = id ( "0" , 10 ) ;
431- }
432- } catch ( e ) {
433- logDebug ( `Failed to set root span parent_id: ${ e } ` ) ;
434- }
435-
436- logDebug ( `Created root execution span: span_id=${ rootSpanId ?? "auto" } , start_time=${ startTime } ` ) ;
390+ logDebug ( `Created root execution span: start_time=${ startTime } ` ) ;
437391
438392 return {
439393 span,
0 commit comments