@@ -119,19 +119,22 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
119
119
return ;
120
120
}
121
121
122
- const latestEndTimestamp = Math . max ( ...spans . map ( span => spanToJSON ( span ) . timestamp || 0 ) ) ;
122
+ const childEndTimestamps = spans
123
+ . map ( span => spanToJSON ( span ) . timestamp )
124
+ . filter ( timestamp => ! ! timestamp ) as number [ ] ;
125
+ const latestSpanEndTimestamp = childEndTimestamps . length ? Math . max ( ...childEndTimestamps ) : undefined ;
123
126
124
- if ( latestEndTimestamp <= 0 ) {
125
- span . end ( timestamp ) ;
126
- return ;
127
- }
128
-
129
- // If the timestamp is smaller than the latest end timestamp of a span, we still want to use it
130
- // Need to convert the timestamp to seconds to ensure Math.min() works as expected
131
- // Note that other than this, we can just pass the timestamp as-is to `span.end()`
132
- // because that will convert to seconds internally anyhow
133
127
const spanEndTimestamp = spanTimeInputToSeconds ( timestamp ) ;
134
- const endTimestamp = Math . min ( spanEndTimestamp , latestEndTimestamp ) ;
128
+ const spanStartTimestamp = spanToJSON ( span ) . start_timestamp ;
129
+
130
+ // The final endTimestamp should:
131
+ // * Never be before the span start timestamp
132
+ // * Be the latestSpanEndTimestamp, if there is one, and it is smaller than the passed span end timestamp
133
+ // * Otherwise be the passed end timestamp
134
+ const endTimestamp = Math . max (
135
+ spanStartTimestamp || - Infinity ,
136
+ Math . min ( spanEndTimestamp , latestSpanEndTimestamp || Infinity ) ,
137
+ ) ;
135
138
136
139
span . end ( endTimestamp ) ;
137
140
}
0 commit comments