Skip to content

Commit d83094c

Browse files
fix(core): Keep beforeSendTransaction and warn about ignored static lifecycle
With `__SENTRY_EXCLUDE_STATIC_LIFECYCLE__`, manually captured transaction events skipped `beforeSendTransaction`, so a PII scrubber could stop running. Only the span processing is skipped now. Setting `traceLifecycle: 'static'` under the flag also logs a non-debug warning, because the static callbacks would otherwise stop running silently.
1 parent c0ce932 commit d83094c

4 files changed

Lines changed: 47 additions & 14 deletions

File tree

‎packages/browser-utils/src/web-vitals/tracking.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export function addWebVitalsToSpan(span: Span, options: AddWebVitalsToSpanOption
182182
}
183183

184184
const { recordClsOnPageloadSpan, recordLcpOnPageloadSpan } = options;
185+
// Redundant at runtime (the constructor forces 'stream'); lets bundlers drop the static branch.
185186
const spanStreamingEnabled =
186187
(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) ||
187188
options.spanStreamingEnabled;

‎packages/core/src/client.ts‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,26 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
269269
attachStacktrace: true,
270270
...options,
271271
traceLifecycle:
272-
options.traceLifecycle === 'static' &&
273-
!(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__)
274-
? 'static'
275-
: 'stream',
272+
(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) ||
273+
options.traceLifecycle !== 'static'
274+
? 'stream'
275+
: 'static',
276276
};
277+
278+
// Not debug-gated: builds that set the flag usually strip debug logging too, and the user's
279+
// static callbacks (often PII scrubbers) would otherwise stop running without any message.
280+
if (
281+
typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' &&
282+
__SENTRY_EXCLUDE_STATIC_LIFECYCLE__ &&
283+
options.traceLifecycle === 'static'
284+
) {
285+
consoleSandbox(() => {
286+
// oxlint-disable-next-line no-console
287+
console.warn(
288+
"[Sentry] `traceLifecycle: 'static'` is not available in this build (`__SENTRY_EXCLUDE_STATIC_LIFECYCLE__`). `beforeSendTransaction`, `ignoreTransactions` and `withStaticSpan` callbacks are ignored.",
289+
);
290+
});
291+
}
277292
this._integrations = {};
278293
this._numProcessing = 0;
279294
this._outcomes = {};
@@ -615,6 +630,7 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
615630

616631
// Extract gen_ai spans from transaction and convert to span v2 format.
617632
// This mutates event.spans to remove the extracted spans.
633+
// Manually captured transaction events keep their gen_ai spans inline under the flag.
618634
const genAiSpanItem =
619635
typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__
620636
? undefined
@@ -1809,12 +1825,13 @@ function processBeforeSend(
18091825
);
18101826
}
18111827

1812-
if (
1813-
!(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) &&
1814-
isTransactionEvent(processedEvent)
1815-
) {
1816-
// Avoid processing if we don't have to
1817-
if (beforeSendSpan || ignoreSpans) {
1828+
if (isTransactionEvent(processedEvent)) {
1829+
// Only manually captured transaction events get here under the flag. They skip span processing,
1830+
// but still run `beforeSendTransaction`, which users often rely on to scrub PII.
1831+
if (
1832+
!(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) &&
1833+
(beforeSendSpan || ignoreSpans)
1834+
) {
18181835
// 1. Process root span
18191836
const rootSpanJson = convertTransactionEventToSpanJson(processedEvent);
18201837

‎packages/core/src/tracing/sentrySpan.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ export class SentrySpan implements Span {
413413
return;
414414
}
415415

416+
// Redundant at runtime (the constructor forces 'stream'); lets bundlers drop the static branch.
416417
if (
417418
(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) ||
418419
(client && hasSpanStreamingEnabled(client))
@@ -590,6 +591,7 @@ function sendStandaloneSpan(span: SentrySpan, client: Client): void {
590591
// callbacks), so scrub the span in its native v1 shape and convert it forward to v2, mirroring the
591592
// gen_ai extraction path.
592593
// TODO(standalone): remove this branch once the static trace lifecycle is dropped.
594+
// Redundant at runtime (the constructor forces 'stream'); lets bundlers drop the static branch.
593595
if (
594596
!(typeof __SENTRY_EXCLUDE_STATIC_LIFECYCLE__ === 'boolean' && __SENTRY_EXCLUDE_STATIC_LIFECYCLE__) &&
595597
traceLifecycle === 'static' &&

‎packages/core/test/lib/tracing/excludeStaticLifecycle.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ describe('__SENTRY_EXCLUDE_STATIC_LIFECYCLE__', () => {
1717
vi.restoreAllMocks();
1818
});
1919

20-
it('ignores `traceLifecycle: "static"`', () => {
20+
it('ignores `traceLifecycle: "static"` and warns about it', () => {
21+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
22+
2123
const client = new TestClient(getDefaultTestClientOptions({ traceLifecycle: 'static' }));
2224

2325
expect(client.getOptions().traceLifecycle).toBe('stream');
26+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("`traceLifecycle: 'static'` is not available"));
27+
});
28+
29+
it('does not warn with `traceLifecycle: "stream"`', () => {
30+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
31+
32+
new TestClient(getDefaultTestClientOptions({ traceLifecycle: 'stream' }));
33+
34+
expect(warnSpy).not.toHaveBeenCalledWith(expect.stringContaining("`traceLifecycle: 'static'` is not available"));
2435
});
2536

2637
it('hands segment spans to span streaming instead of capturing a transaction', () => {
@@ -37,17 +48,19 @@ describe('__SENTRY_EXCLUDE_STATIC_LIFECYCLE__', () => {
3748
expect(captureEventSpy).not.toHaveBeenCalled();
3849
});
3950

40-
it('does not run transaction processing on transaction events', async () => {
41-
const beforeSendTransaction = vi.fn(event => event);
51+
it('still runs `beforeSendTransaction` on manually captured transaction events', async () => {
52+
const beforeSendTransaction = vi.fn(event => ({ ...event, transaction: 'scrubbed' }));
4253
const client = new TestClient(
4354
getDefaultTestClientOptions({ dsn: 'https://username@domain/123', beforeSendTransaction }),
4455
);
56+
const sendEventSpy = vi.spyOn(client, 'sendEvent');
4557
const event: TransactionEvent = { type: 'transaction', transaction: 'test', spans: [] };
4658

4759
client.captureEvent(event);
4860
await client.flush();
4961

50-
expect(beforeSendTransaction).not.toHaveBeenCalled();
62+
expect(beforeSendTransaction).toHaveBeenCalledTimes(1);
63+
expect(sendEventSpy).toHaveBeenCalledWith(expect.objectContaining({ transaction: 'scrubbed' }), expect.anything());
5164
});
5265

5366
it('adds the span streaming integration to server clients with `traceLifecycle: "static"`', () => {

0 commit comments

Comments
 (0)