11import { SENTRY_LINK_TYPE } from '@sentry/conventions/attributes' ;
2- import type { Span } from '@sentry/core' ;
2+ import type { Span , SpanLink , StartSpanOptions } from '@sentry/core' ;
33import { extractTraceparentData , GLOBAL_OBJ } from '@sentry/core' ;
44import { SHELL_META_TAG_NAME } from '../../common/traceMetaTags' ;
55import { WINDOW } from '@sentry/react' ;
@@ -132,24 +132,31 @@ export function waitForTraceMetaTag(
132132 return { giveUp : ( ) => settle ( onGiveUp ) } ;
133133}
134134
135- /**
136- * Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
137- * the pageload span exists, which can only link the server request, not continue it.
138- */
139- export function addTraceMetaTagLink ( span : Span , { sentryTrace } : TraceMetaTagValues , linkType ?: string ) : void {
135+ /** The span link to the span a `sentry-trace` value describes, if the value is one. */
136+ function traceMetaTagLink ( sentryTrace : string , linkType ?: string ) : SpanLink | undefined {
140137 const traceparent = extractTraceparentData ( sentryTrace ) ;
141138 if ( ! traceparent ?. traceId || ! traceparent . parentSpanId ) {
142- return ;
139+ return undefined ;
143140 }
144-
145- span . addLink ( {
141+ return {
146142 context : {
147143 traceId : traceparent . traceId ,
148144 spanId : traceparent . parentSpanId ,
149145 traceFlags : traceparent . parentSampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE ,
150146 } ,
151147 ...( linkType && { attributes : { [ SENTRY_LINK_TYPE ] : linkType } } ) ,
152- } ) ;
148+ } ;
149+ }
150+
151+ /**
152+ * Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
153+ * the pageload span exists, which can only link the server request, not continue it.
154+ */
155+ export function addTraceMetaTagLink ( span : Span , { sentryTrace } : TraceMetaTagValues ) : void {
156+ const link = traceMetaTagLink ( sentryTrace ) ;
157+ if ( link ) {
158+ span . addLink ( link ) ;
159+ }
153160}
154161
155162/** Reads the marker the server writes into a shell while it prerenders it, see `nextSentryPropagator.ts`. */
@@ -159,11 +166,12 @@ export function readShellMarker(): string | undefined {
159166}
160167
161168/**
162- * Marks a pageload whose document is a cached shell and links the request that prerendered the shell.
169+ * The start options that mark a pageload whose document is a cached shell and link the request that
170+ * prerendered the shell. Applied at span start, so `tracesSampler` and `ignoreSpans` see the attribute.
163171 * The marker is never continued: the shell is served to every later visitor, and the request behind it
164172 * handled none of their visits.
165173 */
166- export function markCachedShell ( span : Span , marker : string ) : void {
167- span . setAttribute ( CACHED_SHELL_ATTRIBUTE , true ) ;
168- addTraceMetaTagLink ( span , { sentryTrace : marker , baggage : undefined } , 'cache_origin' ) ;
174+ export function cachedShellSpanOptions ( marker : string ) : Pick < StartSpanOptions , 'attributes' | 'links' > {
175+ const link = traceMetaTagLink ( marker , 'cache_origin' ) ;
176+ return { attributes : { [ CACHED_SHELL_ATTRIBUTE ] : true } , ... ( link && { links : [ link ] } ) } ;
169177}
0 commit comments