Skip to content

Commit 7ee6852

Browse files
authored
ref(browser): Ensure idle span ending is consistent (#12310)
1 parent 7839b37 commit 7ee6852

File tree

3 files changed

+12
-10
lines changed
  • dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/pageloadWithChildSpanTimeout
  • packages/core/src/tracing

3 files changed

+12
-10
lines changed

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/pageloadWithChildSpanTimeout/init.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ Sentry.init({
1515
});
1616

1717
const activeSpan = Sentry.getActiveSpan();
18-
if (activeSpan) {
19-
Sentry.startInactiveSpan({ name: 'pageload-child-span' });
20-
} else {
21-
setTimeout(() => {
22-
Sentry.startInactiveSpan({ name: 'pageload-child-span' });
23-
}, 200);
24-
}
18+
Sentry.startInactiveSpan({
19+
name: 'pageload-child-span',
20+
onlyIfParent: true,
21+
// Set this to ensure we do not discard this span due to timeout
22+
startTime: activeSpan && Sentry.spanToJSON(activeSpan).start_timestamp,
23+
});

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/pageloadWithChildSpanTimeout/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ sentryTest('should send a pageload span terminated via child span timeout', asyn
2121
const eventData = envelopeRequestParser(req);
2222

2323
expect(eventData.contexts?.trace?.op).toBe('pageload');
24+
expect(eventData.contexts?.trace?.data?.['sentry.idle_span_discarded_spans']).toBeUndefined();
2425
expect(eventData.spans?.length).toBeGreaterThanOrEqual(1);
2526
const testSpan = eventData.spans?.find(span => span.description === 'pageload-child-span');
2627
expect(testSpan).toBeDefined();

packages/core/src/tracing/idleSpan.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
121121
beforeSpanEnd(span);
122122
}
123123

124-
const timestamp = args[0] || timestampInSeconds();
124+
// Just ensuring that this keeps working, even if we ever have more arguments here
125+
const [definedEndTimestamp, ...rest] = args;
126+
const timestamp = definedEndTimestamp || timestampInSeconds();
125127
const spanEndTimestamp = spanTimeInputToSeconds(timestamp);
126128

127129
// Ensure we end with the last span timestamp, if possible
@@ -130,7 +132,7 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
130132
// If we have no spans, we just end, nothing else to do here
131133
if (!spans.length) {
132134
onIdleSpanEnded(spanEndTimestamp);
133-
return Reflect.apply(target, thisArg, args);
135+
return Reflect.apply(target, thisArg, [spanEndTimestamp, ...rest]);
134136
}
135137

136138
const childEndTimestamps = spans
@@ -152,7 +154,7 @@ export function startIdleSpan(startSpanOptions: StartSpanOptions, options: Parti
152154
);
153155

154156
onIdleSpanEnded(endTimestamp);
155-
return Reflect.apply(target, thisArg, [endTimestamp]);
157+
return Reflect.apply(target, thisArg, [endTimestamp, ...rest]);
156158
},
157159
});
158160

0 commit comments

Comments
 (0)