@@ -283,6 +283,52 @@ export class Tracing implements Integration {
283283 } ) ;
284284 }
285285
286+ /**
287+ * Returns a new Transaction either continued from sentry-trace meta or a new one
288+ */
289+ private static _getNewTransaction ( hub : Hub , transactionContext : TransactionContext ) : Transaction {
290+ let traceId ;
291+ let parentSpanId ;
292+ let sampled ;
293+
294+ const header = Tracing . _getMeta ( 'sentry-trace' ) ;
295+ if ( header ) {
296+ const span = SpanClass . fromTraceparent ( header ) ;
297+ if ( span ) {
298+ traceId = span . traceId ;
299+ parentSpanId = span . parentSpanId ;
300+ sampled = span . sampled ;
301+ }
302+ Tracing . _log (
303+ `[Tracing] found 'sentry-meta' '<meta />' continuing trace with: trace_id: ${ traceId } span_id: ${ parentSpanId } ` ,
304+ ) ;
305+ }
306+
307+ return hub . startTransaction ( {
308+ parentSpanId,
309+ sampled,
310+ traceId,
311+ trimEnd : true ,
312+ ...transactionContext ,
313+ } ) as Transaction ;
314+ }
315+
316+ /**
317+ * Returns the value of a meta tag
318+ */
319+ private static _getMeta ( metaName : string ) : string | null {
320+ const metas = document . getElementsByTagName ( 'meta' ) ;
321+
322+ // tslint:disable-next-line: prefer-for-of
323+ for ( let i = 0 ; i < metas . length ; i ++ ) {
324+ if ( metas [ i ] . getAttribute ( 'name' ) === metaName ) {
325+ return metas [ i ] . getAttribute ( 'content' ) ;
326+ }
327+ }
328+
329+ return null ;
330+ }
331+
286332 /**
287333 * Pings the heartbeat
288334 */
@@ -454,10 +500,7 @@ export class Tracing implements Integration {
454500 return undefined ;
455501 }
456502
457- Tracing . _activeTransaction = hub . startTransaction ( {
458- trimEnd : true ,
459- ...transactionContext ,
460- } ) as Transaction ;
503+ Tracing . _activeTransaction = Tracing . _getNewTransaction ( hub , transactionContext ) ;
461504
462505 // We set the transaction here on the scope so error events pick up the trace context and attach it to the error
463506 hub . configureScope ( scope => scope . setSpan ( Tracing . _activeTransaction ) ) ;
0 commit comments