1- import type { Span , TransactionContext } from '@sentry/types' ;
1+ import type { Span , SpanTimeInput , TransactionContext } from '@sentry/types' ;
22import { dropUndefinedKeys , logger , tracingContextFromHeaders } from '@sentry/utils' ;
33
44import { DEBUG_BUILD } from '../debug-build' ;
@@ -7,6 +7,12 @@ import type { Hub } from '../hub';
77import { getCurrentHub } from '../hub' ;
88import { handleCallbackErrors } from '../utils/handleCallbackErrors' ;
99import { hasTracingEnabled } from '../utils/hasTracingEnabled' ;
10+ import { spanTimeInputToSeconds } from '../utils/spanUtils' ;
11+
12+ interface StartSpanOptions extends TransactionContext {
13+ /** A manually specified start time for the created `Span` object. */
14+ startTime ?: SpanTimeInput ;
15+ }
1016
1117/**
1218 * Wraps a function with a transaction/span and finishes the span after the function is done.
@@ -65,7 +71,7 @@ export function trace<T>(
6571 * or you didn't set `tracesSampleRate`, this function will not generate spans
6672 * and the `span` returned from the callback will be undefined.
6773 */
68- export function startSpan < T > ( context : TransactionContext , callback : ( span : Span | undefined ) => T ) : T {
74+ export function startSpan < T > ( context : StartSpanOptions , callback : ( span : Span | undefined ) => T ) : T {
6975 const ctx = normalizeContext ( context ) ;
7076
7177 return withScope ( scope => {
@@ -105,7 +111,7 @@ export const startActiveSpan = startSpan;
105111 * and the `span` returned from the callback will be undefined.
106112 */
107113export function startSpanManual < T > (
108- context : TransactionContext ,
114+ context : StartSpanOptions ,
109115 callback : ( span : Span | undefined , finish : ( ) => void ) => T ,
110116) : T {
111117 const ctx = normalizeContext ( context ) ;
@@ -143,17 +149,12 @@ export function startSpanManual<T>(
143149 * or you didn't set `tracesSampleRate` or `tracesSampler`, this function will not generate spans
144150 * and the `span` returned from the callback will be undefined.
145151 */
146- export function startInactiveSpan ( context : TransactionContext ) : Span | undefined {
152+ export function startInactiveSpan ( context : StartSpanOptions ) : Span | undefined {
147153 if ( ! hasTracingEnabled ( ) ) {
148154 return undefined ;
149155 }
150156
151- const ctx = { ...context } ;
152- // If a name is set and a description is not, set the description to the name.
153- if ( ctx . name !== undefined && ctx . description === undefined ) {
154- ctx . description = ctx . name ;
155- }
156-
157+ const ctx = normalizeContext ( context ) ;
157158 const hub = getCurrentHub ( ) ;
158159 const parentSpan = getActiveSpan ( ) ;
159160 return parentSpan ? parentSpan . startChild ( ctx ) : hub . startTransaction ( ctx ) ;
@@ -238,12 +239,24 @@ function createChildSpanOrTransaction(
238239 return parentSpan ? parentSpan . startChild ( ctx ) : hub . startTransaction ( ctx ) ;
239240}
240241
241- function normalizeContext ( context : TransactionContext ) : TransactionContext {
242+ /**
243+ * This converts StartSpanOptions to TransactionContext.
244+ * For the most part (for now) we accept the same options,
245+ * but some of them need to be transformed.
246+ *
247+ * Eventually the StartSpanOptions will be more aligned with OpenTelemetry.
248+ */
249+ function normalizeContext ( context : StartSpanOptions ) : TransactionContext {
242250 const ctx = { ...context } ;
243251 // If a name is set and a description is not, set the description to the name.
244252 if ( ctx . name !== undefined && ctx . description === undefined ) {
245253 ctx . description = ctx . name ;
246254 }
247255
256+ if ( context . startTime ) {
257+ ctx . startTimestamp = spanTimeInputToSeconds ( context . startTime ) ;
258+ delete ctx . startTime ;
259+ }
260+
248261 return ctx ;
249262}
0 commit comments