Skip to content

Commit 41cae18

Browse files
committed
span link
1 parent c8e9045 commit 41cae18

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

packages/browser-utils/src/metrics/browserMetrics.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
browserPerformanceTimeOrigin,
55
getActiveSpan,
66
getComponentName,
7+
getCurrentScope,
78
htmlTreeAsString,
89
isPrimitive,
910
parseUrl,
1011
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
12+
SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE,
1113
setMeasurement,
1214
spanToJSON,
1315
stringMatchesSomePattern,
@@ -32,6 +34,7 @@ import {
3234
import { getActivationStart } from './web-vitals/lib/getActivationStart';
3335
import { getNavigationEntry } from './web-vitals/lib/getNavigationEntry';
3436
import { getVisibilityWatcher } from './web-vitals/lib/getVisibilityWatcher';
37+
import { TRACE_FLAG_NONE, TRACE_FLAG_SAMPLED } from '@sentry/core/build/types/utils/spanUtils';
3538

3639
interface NavigatorNetworkInformation {
3740
readonly connection?: NetworkInformation;
@@ -604,6 +607,7 @@ function _addRequest(span: Span, entry: PerformanceNavigationTiming, timeOrigin:
604607
const responseEndTimestamp = timeOrigin + msToSec(entry.responseEnd as number);
605608
const responseStartTimestamp = timeOrigin + msToSec(entry.responseStart as number);
606609
if (entry.responseEnd) {
610+
const propagationContext = getCurrentScope().getPropagationContext();
607611
// It is possible that we are collecting these metrics when the page hasn't finished loading yet, for example when the HTML slowly streams in.
608612
// In this case, ie. when the document request hasn't finished yet, `entry.responseEnd` will be 0.
609613
// In order not to produce faulty spans, where the end timestamp is before the start timestamp, we will only collect
@@ -614,6 +618,20 @@ function _addRequest(span: Span, entry: PerformanceNavigationTiming, timeOrigin:
614618
attributes: {
615619
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
616620
},
621+
...(propagationContext.ssrSpanId && {
622+
links: [
623+
{
624+
context: {
625+
traceId: propagationContext.traceId,
626+
spanId: propagationContext.ssrSpanId,
627+
traceFlags: propagationContext.sampled ? TRACE_FLAG_SAMPLED : TRACE_FLAG_NONE,
628+
},
629+
attributes: {
630+
[SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE]: 'ssr_span',
631+
},
632+
},
633+
],
634+
}),
617635
});
618636

619637
startAndEndSpan(span, responseStartTimestamp, responseEndTimestamp, {

packages/browser/src/tracing/browserTracingIntegration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ export const browserTracingIntegration = ((_options: Partial<BrowserTracingOptio
547547
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
548548

549549
if (parentlessRootSpans) {
550-
propagationContext.parentSpanId = undefined;
550+
const ssrSpan = propagationContext.parentSpanId;
551+
delete propagationContext.parentSpanId;
552+
propagationContext.ssrSpanId = ssrSpan;
551553
}
552554

553555
const scope = getCurrentScope();

packages/core/src/types-hoist/link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type SpanLinkAttributes = {
44
/**
55
* Setting the link type to 'previous_trace' helps the Sentry product linking to the previous trace
66
*/
7-
'sentry.link.type'?: string | 'previous_trace';
7+
'sentry.link.type'?: string | 'previous_trace' | 'ssr_span';
88
} & Record<string, SpanAttributeValue | undefined>;
99

1010
export interface SpanLink {

packages/core/src/types-hoist/tracing.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export interface PropagationContext {
5050
* The current SDK should not modify this value!
5151
*/
5252
dsc?: Partial<DynamicSamplingContext>;
53+
54+
/**
55+
* The id of a server-side span that describe the SSR request handling lifecycle (most likely the root `http.server` span).
56+
* To be used on the client for additional annotation if present.
57+
*/
58+
ssrSpanId?: string;
5359
}
5460

5561
/**

0 commit comments

Comments
 (0)