Skip to content

Commit 45a05c5

Browse files
authored
feat(core): Set default scope for BaseClient methods (#11775)
`client.captureException/captureMessage/captureEvent` do not include current scope in events by default. This is in contrast to `captureException/captureMessage/captureEvent` exported from every SDK which include the current scope by default. This PR changes the parameter names (`scope` > `currentScope`) and adds jsdocs to make this more clear. It also changes the parameters to use the scope interface instead of the scope class.
1 parent 13be067 commit 45a05c5

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

packages/core/src/baseclient.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
Integration,
1818
Outcome,
1919
ParameterizedString,
20+
Scope,
2021
SdkMetadata,
2122
Session,
2223
SessionAggregates,
@@ -52,7 +53,6 @@ import { createEventEnvelope, createSessionEnvelope } from './envelope';
5253
import type { IntegrationIndex } from './integration';
5354
import { afterSetupIntegrations } from './integration';
5455
import { setupIntegration, setupIntegrations } from './integration';
55-
import type { Scope } from './scope';
5656
import { updateSession } from './session';
5757
import { getDynamicSamplingContextFromClient } from './tracing/dynamicSamplingContext';
5858
import { parseSampleRate } from './utils/parseSampleRate';
@@ -151,7 +151,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
151151
* @inheritDoc
152152
*/
153153
// eslint-disable-next-line @typescript-eslint/no-explicit-any
154-
public captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined {
154+
public captureException(exception: any, hint?: EventHint, currentScope?: Scope): string | undefined {
155155
// ensure we haven't captured this very object before
156156
if (checkOrSetAlreadyCaught(exception)) {
157157
DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
@@ -162,7 +162,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
162162

163163
this._process(
164164
this.eventFromException(exception, hint)
165-
.then(event => this._captureEvent(event, hint, scope))
165+
.then(event => this._captureEvent(event, hint, currentScope))
166166
.then(result => {
167167
eventId = result;
168168
}),
@@ -178,7 +178,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
178178
message: ParameterizedString,
179179
level?: SeverityLevel,
180180
hint?: EventHint,
181-
scope?: Scope,
181+
currentScope?: Scope,
182182
): string | undefined {
183183
let eventId: string | undefined = hint && hint.event_id;
184184

@@ -190,7 +190,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
190190

191191
this._process(
192192
promisedEvent
193-
.then(event => this._captureEvent(event, hint, scope))
193+
.then(event => this._captureEvent(event, hint, currentScope))
194194
.then(result => {
195195
eventId = result;
196196
}),
@@ -202,7 +202,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
202202
/**
203203
* @inheritDoc
204204
*/
205-
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
205+
public captureEvent(event: Event, hint?: EventHint, currentScope?: Scope): string | undefined {
206206
// ensure we haven't captured this very object before
207207
if (hint && hint.originalException && checkOrSetAlreadyCaught(hint.originalException)) {
208208
DEBUG_BUILD && logger.log(ALREADY_SEEN_ERROR);
@@ -215,7 +215,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
215215
const capturedSpanScope: Scope | undefined = sdkProcessingMetadata.capturedSpanScope;
216216

217217
this._process(
218-
this._captureEvent(event, hint, capturedSpanScope || scope).then(result => {
218+
this._captureEvent(event, hint, capturedSpanScope || currentScope).then(result => {
219219
eventId = result;
220220
}),
221221
);
@@ -629,13 +629,13 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
629629
*
630630
* @param event The original event.
631631
* @param hint May contain additional information about the original exception.
632-
* @param scope A scope containing event metadata.
632+
* @param currentScope A scope containing event metadata.
633633
* @returns A new event with more information.
634634
*/
635635
protected _prepareEvent(
636636
event: Event,
637637
hint: EventHint,
638-
scope?: Scope,
638+
currentScope?: Scope,
639639
isolationScope = getIsolationScope(),
640640
): PromiseLike<Event | null> {
641641
const options = this.getOptions();
@@ -646,14 +646,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
646646

647647
this.emit('preprocessEvent', event, hint);
648648

649-
return prepareEvent(options, event, hint, scope, this, isolationScope).then(evt => {
649+
return prepareEvent(options, event, hint, currentScope, this, isolationScope).then(evt => {
650650
if (evt === null) {
651651
return evt;
652652
}
653653

654654
const propagationContext = {
655655
...isolationScope.getPropagationContext(),
656-
...(scope ? scope.getPropagationContext() : undefined),
656+
...(currentScope ? currentScope.getPropagationContext() : undefined),
657657
};
658658

659659
const trace = evt.contexts && evt.contexts.trace;
@@ -716,10 +716,10 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
716716
*
717717
* @param event The event to send to Sentry.
718718
* @param hint May contain additional information about the original exception.
719-
* @param scope A scope containing event metadata.
719+
* @param currentScope A scope containing event metadata.
720720
* @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send.
721721
*/
722-
protected _processEvent(event: Event, hint: EventHint, scope?: Scope): PromiseLike<Event> {
722+
protected _processEvent(event: Event, hint: EventHint, currentScope?: Scope): PromiseLike<Event> {
723723
const options = this.getOptions();
724724
const { sampleRate } = options;
725725

@@ -747,7 +747,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
747747
const sdkProcessingMetadata = event.sdkProcessingMetadata || {};
748748
const capturedSpanIsolationScope: Scope | undefined = sdkProcessingMetadata.capturedSpanIsolationScope;
749749

750-
return this._prepareEvent(event, hint, scope, capturedSpanIsolationScope)
750+
return this._prepareEvent(event, hint, currentScope, capturedSpanIsolationScope)
751751
.then(prepared => {
752752
if (prepared === null) {
753753
this.recordDroppedEvent('event_processor', dataCategory, event);
@@ -768,7 +768,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
768768
throw new SentryError(`${beforeSendLabel} returned \`null\`, will not send event.`, 'log');
769769
}
770770

771-
const session = scope && scope.getSession();
771+
const session = currentScope && currentScope.getSession();
772772
if (!isTransaction && session) {
773773
this._updateSessionFromEvent(session, processedEvent);
774774
}

packages/types/src/client.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,39 @@ export interface Client<O extends ClientOptions = ClientOptions> {
3131
/**
3232
* Captures an exception event and sends it to Sentry.
3333
*
34+
* Unlike `captureException` exported from every SDK, this method requires that you pass it the current scope.
35+
*
3436
* @param exception An exception-like object.
3537
* @param hint May contain additional information about the original exception.
36-
* @param scope An optional scope containing event metadata.
38+
* @param currentScope An optional scope containing event metadata.
3739
* @returns The event id
3840
*/
39-
captureException(exception: any, hint?: EventHint, scope?: Scope): string | undefined;
41+
captureException(exception: any, hint?: EventHint, currentScope?: Scope): string | undefined;
4042

4143
/**
4244
* Captures a message event and sends it to Sentry.
4345
*
46+
* Unlike `captureMessage` exported from every SDK, this method requires that you pass it the current scope.
47+
*
4448
* @param message The message to send to Sentry.
4549
* @param level Define the level of the message.
4650
* @param hint May contain additional information about the original exception.
47-
* @param scope An optional scope containing event metadata.
51+
* @param currentScope An optional scope containing event metadata.
4852
* @returns The event id
4953
*/
50-
captureMessage(message: string, level?: SeverityLevel, hint?: EventHint, scope?: Scope): string | undefined;
54+
captureMessage(message: string, level?: SeverityLevel, hint?: EventHint, currentScope?: Scope): string | undefined;
5155

5256
/**
5357
* Captures a manually created event and sends it to Sentry.
5458
*
59+
* Unlike `captureEvent` exported from every SDK, this method requires that you pass it the current scope.
60+
*
5561
* @param event The event to send to Sentry.
5662
* @param hint May contain additional information about the original exception.
57-
* @param scope An optional scope containing event metadata.
63+
* @param currentScope An optional scope containing event metadata.
5864
* @returns The event id
5965
*/
60-
captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined;
66+
captureEvent(event: Event, hint?: EventHint, currentScope?: Scope): string | undefined;
6167

6268
/**
6369
* Captures a session

0 commit comments

Comments
 (0)