Skip to content

Commit b3d2fb6

Browse files
committed
remove marker type and "cheat" in withStaticSpan callback
1 parent 9176c4b commit b3d2fb6

5 files changed

Lines changed: 28 additions & 36 deletions

File tree

MIGRATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ beforeSendSpan: Sentry.withStreamedSpan(span => span);
191191
beforeSendSpan: span => span;
192192
```
193193
194-
The internal `isStreamedBeforeSendSpanCallback()` function from `@sentry/core` is no longer exported.
194+
The internal `isStreamedBeforeSendSpanCallback()` function from `@sentry/core` was removed.
195195
196196
#### Replacing `beforeSendTransaction`
197197

packages/core/src/client.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type { StartSpanOptions } from './types/startSpanOptions';
3939
import type { Transport, TransportMakeRequestResponse } from './types/transport';
4040
import type { ResolvedDataCollection } from './types/datacollection';
4141
import { createClientReportEnvelope } from './utils/clientreport';
42-
import { debug } from './utils/debug-logger';
42+
import { consoleSandbox, debug } from './utils/debug-logger';
4343
import { dsnToString, makeDsn } from './utils/dsn';
4444
import { addItemToEnvelope, createAttachmentEnvelopeItem } from './utils/envelope';
4545
import { getPossibleEventMessages } from './utils/eventUtils';
@@ -263,11 +263,14 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
263263
beforeSendSpan &&
264264
isStaticBeforeSendSpanCallback(beforeSendSpan) !== (traceLifecycle === 'static')
265265
) {
266-
debug.warn(
267-
`Ignoring \`beforeSendSpan\`: ${
268-
traceLifecycle === 'static' ? 'wrap it with' : 'remove'
269-
} \`Sentry.withStaticSpan\` to use it with \`traceLifecycle: "${traceLifecycle}"\`.`,
270-
);
266+
consoleSandbox(() => {
267+
// oxlint-disable-next-line no-console
268+
console.warn(
269+
`Ignoring \`beforeSendSpan\`: ${
270+
traceLifecycle === 'static' ? 'wrap it with' : 'remove'
271+
} \`Sentry.withStaticSpan\` to use it with \`traceLifecycle: "${traceLifecycle}"\`.`,
272+
);
273+
});
271274
}
272275

273276
if (this._dsn) {

packages/core/src/tracing/spans/beforeSendSpan.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { BeforeSendStaticSpanCallback, BeforeSendStreamedSpanCallback } from '../../types/options';
2-
import type { SpanJSON, StreamedSpanJSON } from '../../types/span';
2+
import type { StreamedSpanJSON } from '../../types/span';
33
import { addNonEnumerableProperty } from '../../utils/object';
44

55
/**
66
* A wrapper to use the static, transaction-based span format in your `beforeSendSpan` callback.
77
*
88
* When using `traceLifecycle: 'static'`, wrap your callback with this function
9-
* to receive and return {@link SpanJSON} instead of {@link StreamedSpanJSON}.
9+
* to receive and return a static span instead of a {@link StreamedSpanJSON}.
1010
*
1111
* @example
1212
*
@@ -18,12 +18,15 @@ import { addNonEnumerableProperty } from '../../utils/object';
1818
* }),
1919
* });
2020
*
21-
* @param callback - The callback function that receives and returns a {@link SpanJSON}.
21+
* @param callback - A {@link BeforeSendStaticSpanCallback} that receives and returns a static span.
2222
* @returns A callback that is compatible with the `beforeSendSpan` option when using `traceLifecycle: 'static'`.
2323
*/
24-
export function withStaticSpan(callback: (span: SpanJSON) => SpanJSON): BeforeSendStaticSpanCallback {
24+
export function withStaticSpan(callback: BeforeSendStaticSpanCallback): BeforeSendStreamedSpanCallback {
2525
addNonEnumerableProperty(callback, '_static', true);
26-
return callback as BeforeSendStaticSpanCallback;
26+
// `beforeSendSpan` is typed as a single streamed callback so that an unwrapped callback's parameter
27+
// is contextually typed as `StreamedSpanJSON`. The `_static` marker tells the SDK to hand this
28+
// callback a `SpanJSON` instead, so the declared parameter type is deliberately not what it receives.
29+
return callback as unknown as BeforeSendStreamedSpanCallback;
2730
}
2831

2932
/**
@@ -50,13 +53,3 @@ export function withStreamedSpan(
5053
export function isStaticBeforeSendSpanCallback(callback: unknown): callback is BeforeSendStaticSpanCallback {
5154
return !!callback && typeof callback === 'function' && '_static' in callback && !!callback._static;
5255
}
53-
54-
/**
55-
* Typesafe check to identify if a `beforeSendSpan` callback expects the streamed span JSON format.
56-
*
57-
* @param callback - The `beforeSendSpan` callback to check.
58-
* @returns `true` unless the callback was marked as a static span callback.
59-
*/
60-
export function isStreamedBeforeSendSpanCallback(callback: unknown): callback is BeforeSendStreamedSpanCallback {
61-
return !!callback && typeof callback === 'function' && !isStaticBeforeSendSpanCallback(callback);
62-
}

packages/core/src/tracing/spans/captureSpan.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import {
2020
streamedSpanJsonToSerializedSpan,
2121
} from '../../utils/spanUtils';
2222
import { getCapturedScopesOnSpan } from '../utils';
23-
import { isStaticBeforeSendSpanCallback, isStreamedBeforeSendSpanCallback } from './beforeSendSpan';
24-
import { hasSpanStreamingEnabled } from './hasSpanStreamingEnabled';
23+
import { isStaticBeforeSendSpanCallback } from './beforeSendSpan';
2524
import { scopeContextsToSpanAttributes } from './scopeContextAttributes';
2625
import { spanJsonToStreamedSpanJson, streamedSpanJsonToSpanJson } from './spanJsonToStreamedSpan';
2726
import { DEFAULT_ENVIRONMENT } from '../../constants';
@@ -161,14 +160,14 @@ function applyCommonSpanAttributes(
161160
* TODO(v12): Remove the v1 and static callback conversion shenanigans once we drop transactions.
162161
*/
163162
function applyBeforeSendSpan(span: StreamedSpanJSON, client: Client): StreamedSpanJSON {
164-
const { beforeSendSpan } = client.getOptions();
163+
const { beforeSendSpan, traceLifecycle } = client.getOptions();
165164

166165
if (!beforeSendSpan) {
167166
return span;
168167
}
169168

170-
if (hasSpanStreamingEnabled(client)) {
171-
return isStreamedBeforeSendSpanCallback(beforeSendSpan) ? applyBeforeSendSpanCallback(span, beforeSendSpan) : span;
169+
if (traceLifecycle === 'stream') {
170+
return !isStaticBeforeSendSpanCallback(beforeSendSpan) ? applyBeforeSendSpanCallback(span, beforeSendSpan) : span;
172171
}
173172

174173
return isStaticBeforeSendSpanCallback(beforeSendSpan)

packages/core/src/types/options.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
640640
*
641641
* @returns The modified span payload that will be sent.
642642
*/
643-
beforeSendSpan?: BeforeSendStreamedSpanCallback | BeforeSendStaticSpanCallbackMarker;
643+
beforeSendSpan?: BeforeSendStreamedSpanCallback;
644644

645645
/**
646646
* An event-processing callback for transaction events, guaranteed to be invoked after all other event
@@ -680,16 +680,13 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
680680
export type BeforeSendStreamedSpanCallback = (span: StreamedSpanJSON) => StreamedSpanJSON;
681681

682682
/**
683-
* The marker that {@link withStaticSpan} adds to a `beforeSendSpan` callback.
683+
* A callback for processing spans sent as part of transaction events.
684684
*
685-
* Deliberately not callable: `beforeSendSpan` must have exactly one call signature for TypeScript to
686-
* contextually type an unwrapped callback's parameter as {@link StreamedSpanJSON}. A union of two
687-
* callable types would infer `any` instead.
685+
* Pass this to {@link withStaticSpan} rather than to `beforeSendSpan` directly.
686+
*
687+
* @see {@link SpanJSON} for the static span format used with `traceLifecycle: 'static'`
688688
*/
689-
type BeforeSendStaticSpanCallbackMarker = { _static: true };
690-
691-
/** A callback for processing spans sent as part of transaction events. */
692-
export type BeforeSendStaticSpanCallback = ((span: SpanJSON) => SpanJSON) & BeforeSendStaticSpanCallbackMarker;
689+
export type BeforeSendStaticSpanCallback = (span: SpanJSON) => SpanJSON;
693690

694691
/** Base configuration options for every SDK. */
695692
export interface CoreOptions<TO extends BaseTransportOptions = BaseTransportOptions> extends Omit<

0 commit comments

Comments
 (0)