1+ import { SPAN_STATUS_OK } from '@sentry/core' ;
12import type { SpanAttributeValue , StartSpanOptions } from '@sentry/core' ;
23import * as Sentry from '@sentry/react-native' ;
34import { AppState } from 'react-native' ;
45import CONST from '@src/CONST' ;
56
67type ActiveSpanEntry = {
78 span : ReturnType < typeof Sentry . startInactiveSpan > ;
8- startTime : number ;
9+ startTimeForLog : number ;
910} ;
1011
1112const activeSpans = new Map < string , ActiveSpanEntry > ( ) ;
@@ -35,7 +36,15 @@ function startSpan(spanId: string, options: StartSpanOptions, extraOptions: Star
3536 if ( extraOptions . minDuration ) {
3637 span . setAttribute ( CONST . TELEMETRY . ATTRIBUTE_MIN_DURATION , extraOptions . minDuration ) ;
3738 }
38- activeSpans . set ( spanId , { span, startTime : performance . now ( ) } ) ;
39+
40+ let startTimeForLog : number ;
41+ if ( typeof options . startTime === 'number' ) {
42+ startTimeForLog = options . startTime ;
43+ } else {
44+ startTimeForLog = performance . now ( ) ;
45+ }
46+
47+ activeSpans . set ( spanId , { span, startTimeForLog} ) ;
3948
4049 return span ;
4150}
@@ -46,11 +55,12 @@ function endSpan(spanId: string) {
4655 if ( ! entry ) {
4756 return ;
4857 }
49- const { span, startTime } = entry ;
58+ const { span, startTimeForLog } = entry ;
5059 const now = performance . now ( ) ;
51- const durationMs = Math . round ( now - startTime ) ;
60+ const durationMs = Math . round ( now - startTimeForLog ) ;
5261 console . debug ( `[Sentry][${ spanId } ] Ending span (${ durationMs } ms)` , { spanId, durationMs, timestamp : now , attributes : Sentry . spanToJSON ( span ) . data } ) ;
53- span . setStatus ( { code : 1 } ) ;
62+ span . setStatus ( { code : SPAN_STATUS_OK } ) ;
63+
5464 span . setAttribute ( CONST . TELEMETRY . ATTRIBUTE_FINISHED_MANUALLY , true ) ;
5565 span . end ( ) ;
5666 activeSpans . delete ( spanId ) ;
@@ -64,7 +74,7 @@ function cancelSpan(spanId: string) {
6474 entry . span . setAttribute ( CONST . TELEMETRY . ATTRIBUTE_CANCELED , true ) ;
6575 // In Sentry there are only OK or ERROR status codes.
6676 // We treat canceled spans as OK, so we can properly track spans that are not finished at all (their status would be different)
67- entry . span . setStatus ( { code : 1 } ) ;
77+ entry . span . setStatus ( { code : SPAN_STATUS_OK } ) ;
6878 endSpan ( spanId ) ;
6979}
7080
@@ -86,7 +96,7 @@ function getSpan(spanId: string) {
8696 return activeSpans . get ( spanId ) ?. span ;
8797}
8898
89- function endSpanWithAttributes ( spanId : string , attributes : Record < string , SpanAttributeValue > ) {
99+ function endSpanWithAttributes ( spanId : string , attributes : Record < string , SpanAttributeValue | undefined > ) {
90100 const span = getSpan ( spanId ) ;
91101 span ?. setAttributes ( attributes ) ;
92102 endSpan ( spanId ) ;
0 commit comments