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' ;
@@ -134,24 +134,31 @@ export function waitForTraceMetaTag(
134134 return { giveUp : ( ) => settle ( onGiveUp ) } ;
135135}
136136
137- /**
138- * Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
139- * the pageload span exists, which can only link the server request, not continue it.
140- */
141- export function addTraceMetaTagLink ( span : Span , { sentryTrace } : TraceMetaTagValues , linkType ?: string ) : void {
137+ /** The span link to the span a `sentry-trace` value describes, if the value is one. */
138+ function traceMetaTagLink ( sentryTrace : string , linkType : string ) : SpanLink | undefined {
142139 const traceparent = extractTraceparentData ( sentryTrace ) ;
143140 if ( ! traceparent ?. traceId || ! traceparent . parentSpanId ) {
144- return ;
141+ return undefined ;
145142 }
146-
147- span . addLink ( {
143+ return {
148144 context : {
149145 traceId : traceparent . traceId ,
150146 spanId : traceparent . parentSpanId ,
151147 traceFlags : traceparent . parentSampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE ,
152148 } ,
153- attributes : { [ SENTRY_LINK_TYPE ] : linkType ?? SERVER_REQUEST_LINK_TYPE } ,
154- } ) ;
149+ attributes : { [ SENTRY_LINK_TYPE ] : linkType } ,
150+ } ;
151+ }
152+
153+ /**
154+ * Adds a span link to the span described by a `sentry-trace` value. Used when the tag arrives after
155+ * the pageload span exists, which can only link the server request, not continue it.
156+ */
157+ export function addTraceMetaTagLink ( span : Span , { sentryTrace } : TraceMetaTagValues ) : void {
158+ const link = traceMetaTagLink ( sentryTrace , SERVER_REQUEST_LINK_TYPE ) ;
159+ if ( link ) {
160+ span . addLink ( link ) ;
161+ }
155162}
156163
157164/** Reads the marker the server writes into a shell while it prerenders it, see `nextSentryPropagator.ts`. */
@@ -161,11 +168,12 @@ export function readShellMarker(): string | undefined {
161168}
162169
163170/**
164- * Marks a pageload whose document is a cached shell and links the request that prerendered the shell.
171+ * The start options that mark a pageload whose document is a cached shell and link the request that
172+ * prerendered the shell. Applied at span start, so `tracesSampler` and `ignoreSpans` see the attribute.
165173 * The marker is never continued: the shell is served to every later visitor, and the request behind it
166174 * handled none of their visits.
167175 */
168- export function markCachedShell ( span : Span , marker : string ) : void {
169- span . setAttribute ( CACHED_SHELL_ATTRIBUTE , true ) ;
170- addTraceMetaTagLink ( span , { sentryTrace : marker , baggage : undefined } , 'cache_origin' ) ;
176+ export function cachedShellSpanOptions ( marker : string ) : Pick < StartSpanOptions , 'attributes' | 'links' > {
177+ const link = traceMetaTagLink ( marker , 'cache_origin' ) ;
178+ return { attributes : { [ CACHED_SHELL_ATTRIBUTE ] : true } , ... ( link && { links : [ link ] } ) } ;
171179}
0 commit comments