Skip to content

Commit b0c3f5f

Browse files
authored
feat(core): Log warnings when returning null in beforeSendSpan (#14433)
1 parent ce1df3e commit b0c3f5f

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.size-limit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ module.exports = [
187187
path: createCDNPath('bundle.tracing.min.js'),
188188
gzip: false,
189189
brotli: false,
190-
limit: '113 KB',
190+
limit: '120 KB',
191191
},
192192
{
193193
name: 'CDN Bundle (incl. Tracing, Replay) - uncompressed',

packages/core/src/baseclient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { dropUndefinedKeys } from './utils-hoist/object';
5252
import { SyncPromise, rejectedSyncPromise, resolvedSyncPromise } from './utils-hoist/syncpromise';
5353
import { parseSampleRate } from './utils/parseSampleRate';
5454
import { prepareEvent } from './utils/prepareEvent';
55+
import { showSpanDropWarning } from './utils/spanUtils';
5556

5657
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
5758

@@ -977,6 +978,7 @@ function processBeforeSend(
977978
if (processedSpan) {
978979
processedSpans.push(processedSpan);
979980
} else {
981+
showSpanDropWarning();
980982
client.recordDroppedEvent('before_send', 'span');
981983
}
982984
}

packages/core/src/envelope.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
createSpanEnvelopeItem,
2525
getSdkMetadataForEnvelopeHeader,
2626
} from './utils-hoist/envelope';
27-
import { spanToJSON } from './utils/spanUtils';
27+
import { showSpanDropWarning, spanToJSON } from './utils/spanUtils';
2828

2929
/**
3030
* Apply SdkInfo (name, version, packages, integrations) to the corresponding event key.
@@ -122,7 +122,13 @@ export function createSpanEnvelope(spans: [SentrySpan, ...SentrySpan[]], client?
122122

123123
const beforeSendSpan = client && client.getOptions().beforeSendSpan;
124124
const convertToSpanJSON = beforeSendSpan
125-
? (span: SentrySpan) => beforeSendSpan(spanToJSON(span) as SpanJSON)
125+
? (span: SentrySpan) => {
126+
const spanJson = beforeSendSpan(spanToJSON(span) as SpanJSON);
127+
if (!spanJson) {
128+
showSpanDropWarning();
129+
}
130+
return spanJson;
131+
}
126132
: (span: SentrySpan) => spanToJSON(span);
127133

128134
const items: SpanItem[] = [];

packages/core/src/utils/spanUtils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { MetricType } from '../metrics/types';
1717
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../semanticAttributes';
1818
import type { SentrySpan } from '../tracing/sentrySpan';
1919
import { SPAN_STATUS_OK, SPAN_STATUS_UNSET } from '../tracing/spanstatus';
20+
import { consoleSandbox } from '../utils-hoist/logger';
2021
import { addNonEnumerableProperty, dropUndefinedKeys } from '../utils-hoist/object';
2122
import { timestampInSeconds } from '../utils-hoist/time';
2223
import { generateSentryTraceHeader } from '../utils-hoist/tracing';
@@ -26,6 +27,9 @@ import { _getSpanForScope } from './spanOnScope';
2627
export const TRACE_FLAG_NONE = 0x0;
2728
export const TRACE_FLAG_SAMPLED = 0x1;
2829

30+
// todo(v9): Remove this once we've stopped dropping spans via `beforeSendSpan`
31+
let hasShownSpanDropWarning = false;
32+
2933
/**
3034
* Convert a span to a trace context, which can be sent as the `trace` context in an event.
3135
* By default, this will only include trace_id, span_id & parent_span_id.
@@ -280,3 +284,20 @@ export function updateMetricSummaryOnActiveSpan(
280284
updateMetricSummaryOnSpan(span, metricType, sanitizedName, value, unit, tags, bucketKey);
281285
}
282286
}
287+
288+
/**
289+
* Logs a warning once if `beforeSendSpan` is used to drop spans.
290+
*
291+
* todo(v9): Remove this once we've stopped dropping spans via `beforeSendSpan`.
292+
*/
293+
export function showSpanDropWarning(): void {
294+
if (!hasShownSpanDropWarning) {
295+
consoleSandbox(() => {
296+
// eslint-disable-next-line no-console
297+
console.warn(
298+
'[Sentry] Deprecation warning: Returning null from `beforeSendSpan` will be disallowed from SDK version 9.0.0 onwards. The callback will only support mutating spans. To drop certain spans, configure the respective integrations directly.',
299+
);
300+
});
301+
hasShownSpanDropWarning = true;
302+
}
303+
}

0 commit comments

Comments
 (0)