Skip to content

Commit fec78dd

Browse files
committed
revert centralizing to client
1 parent 0e4761a commit fec78dd

File tree

4 files changed

+80
-64
lines changed

4 files changed

+80
-64
lines changed

packages/core/src/client.ts

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { DEBUG_BUILD } from './debug-build';
66
import { createEventEnvelope, createSessionEnvelope } from './envelope';
77
import type { IntegrationIndex } from './integration';
88
import { afterSetupIntegrations, setupIntegration, setupIntegrations } from './integration';
9-
import { stripMetadataFromStackFrames } from './metadata';
109
import type { Scope } from './scope';
1110
import { updateSession } from './session';
1211
import {
@@ -45,7 +44,7 @@ import { parseSampleRate } from './utils/parseSampleRate';
4544
import { prepareEvent } from './utils/prepareEvent';
4645
import { reparentChildSpans, shouldIgnoreSpan } from './utils/should-ignore-span';
4746
import { getActiveSpan, showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
48-
import { rejectedSyncPromise } from './utils/syncpromise';
47+
import { rejectedSyncPromise, resolvedSyncPromise, SyncPromise } from './utils/syncpromise';
4948
import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent';
5049

5150
const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
@@ -317,19 +316,16 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
317316
* @returns A promise that will resolve with `true` if all events are sent before the timeout, or `false` if there are
318317
* still events in the queue when the timeout is reached.
319318
*/
320-
// @ts-expect-error - PromiseLike is a subset of Promise
321-
public async flush(timeout?: number): PromiseLike<boolean> {
319+
public flush(timeout?: number): PromiseLike<boolean> {
322320
const transport = this._transport;
323-
if (!transport) {
324-
return true;
321+
if (transport) {
322+
this.emit('flush');
323+
return this._isClientDoneProcessing(timeout).then(clientFinished => {
324+
return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed);
325+
});
326+
} else {
327+
return resolvedSyncPromise(true);
325328
}
326-
327-
this.emit('flush');
328-
329-
const clientFinished = await this._isClientDoneProcessing(timeout);
330-
const transportFlushed = await transport.flush(timeout);
331-
332-
return clientFinished && transportFlushed;
333329
}
334330

335331
/**
@@ -340,12 +336,12 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
340336
* @returns {Promise<boolean>} A promise which resolves to `true` if the flush completes successfully before the timeout, or `false` if
341337
* it doesn't.
342338
*/
343-
// @ts-expect-error - PromiseLike is a subset of Promise
344-
public async close(timeout?: number): PromiseLike<boolean> {
345-
const result = await this.flush(timeout);
346-
this.getOptions().enabled = false;
347-
this.emit('close');
348-
return result;
339+
public close(timeout?: number): PromiseLike<boolean> {
340+
return this.flush(timeout).then(result => {
341+
this.getOptions().enabled = false;
342+
this.emit('close');
343+
return result;
344+
});
349345
}
350346

351347
/**
@@ -876,21 +872,18 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
876872
/**
877873
* Send an envelope to Sentry.
878874
*/
879-
// @ts-expect-error - PromiseLike is a subset of Promise
880-
public async sendEnvelope(envelope: Envelope): PromiseLike<TransportMakeRequestResponse> {
875+
public sendEnvelope(envelope: Envelope): PromiseLike<TransportMakeRequestResponse> {
881876
this.emit('beforeEnvelope', envelope);
882877

883878
if (this._isEnabled() && this._transport) {
884-
try {
885-
return await this._transport.send(envelope);
886-
} catch (reason) {
879+
return this._transport.send(envelope).then(null, reason => {
887880
DEBUG_BUILD && debug.error('Error while sending envelope:', reason);
888881
return {};
889-
}
882+
});
890883
}
891884

892885
DEBUG_BUILD && debug.error('Transport disabled');
893-
return {};
886+
return resolvedSyncPromise({});
894887
}
895888

896889
/* eslint-enable @typescript-eslint/unified-signatures */
@@ -945,20 +938,24 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
945938
* @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and
946939
* `false` otherwise
947940
*/
948-
protected async _isClientDoneProcessing(timeout?: number): Promise<boolean> {
949-
let ticked = 0;
950-
951-
// if no timeout is provided, we wait "forever" until everything is processed
952-
while (!timeout || ticked < timeout) {
953-
await new Promise(resolve => setTimeout(resolve, 1));
941+
protected _isClientDoneProcessing(timeout?: number): PromiseLike<boolean> {
942+
return new SyncPromise(resolve => {
943+
let ticked: number = 0;
944+
const tick: number = 1;
954945

955-
if (!this._numProcessing) {
956-
return true;
957-
}
958-
ticked++;
959-
}
960-
961-
return false;
946+
const interval = setInterval(() => {
947+
if (this._numProcessing == 0) {
948+
clearInterval(interval);
949+
resolve(true);
950+
} else {
951+
ticked += tick;
952+
if (timeout && ticked >= timeout) {
953+
clearInterval(interval);
954+
resolve(false);
955+
}
956+
}
957+
}, tick);
958+
});
962959
}
963960

964961
/** Determines whether this SDK is enabled and a transport is present. */
@@ -1125,13 +1122,9 @@ export abstract class Client<O extends ClientOptions = ClientOptions> {
11251122
throw _makeDoNotSendEventError(`${beforeSendLabel} returned \`null\`, will not send event.`);
11261123
}
11271124

1128-
if (isError) {
1129-
const session = currentScope.getSession() || isolationScope.getSession();
1130-
if (session) {
1131-
this._updateSessionFromEvent(session, processedEvent);
1132-
}
1133-
1134-
stripMetadataFromStackFrames(processedEvent);
1125+
const session = currentScope.getSession() || isolationScope.getSession();
1126+
if (isError && session) {
1127+
this._updateSessionFromEvent(session, processedEvent);
11351128
}
11361129

11371130
if (isTransaction) {

packages/core/src/integrations/moduleMetadata.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineIntegration } from '../integration';
2-
import { addMetadataToStackFrames } from '../metadata';
2+
import { addMetadataToStackFrames, stripMetadataFromStackFrames } from '../metadata';
3+
import type { EventItem } from '../types-hoist/envelope';
4+
import { forEachEnvelopeItem } from '../utils/envelope';
35

46
/**
57
* Adds module metadata to stack frames.
@@ -14,6 +16,20 @@ export const moduleMetadataIntegration = defineIntegration(() => {
1416
return {
1517
name: 'ModuleMetadata',
1618
setup(client) {
19+
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only.
20+
client.on('beforeEnvelope', envelope => {
21+
forEachEnvelopeItem(envelope, (item, type) => {
22+
if (type === 'event') {
23+
const event = Array.isArray(item) ? (item as EventItem)[1] : undefined;
24+
25+
if (event) {
26+
stripMetadataFromStackFrames(event);
27+
item[1] = event;
28+
}
29+
}
30+
});
31+
});
32+
1733
client.on('applyFrameMetadata', event => {
1834
// Only apply stack frame metadata to error events
1935
if (event.type) {

packages/core/src/integrations/third-party-errors-filter.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { defineIntegration } from '../integration';
2-
import { addMetadataToStackFrames } from '../metadata';
2+
import { addMetadataToStackFrames, stripMetadataFromStackFrames } from '../metadata';
3+
import type { EventItem } from '../types-hoist/envelope';
34
import type { Event } from '../types-hoist/event';
5+
import { forEachEnvelopeItem } from '../utils/envelope';
46
import { getFramesFromEvent } from '../utils/stacktrace';
57

68
interface Options {
@@ -39,6 +41,20 @@ export const thirdPartyErrorFilterIntegration = defineIntegration((options: Opti
3941
return {
4042
name: 'ThirdPartyErrorsFilter',
4143
setup(client) {
44+
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only.
45+
client.on('beforeEnvelope', envelope => {
46+
forEachEnvelopeItem(envelope, (item, type) => {
47+
if (type === 'event') {
48+
const event = Array.isArray(item) ? (item as EventItem)[1] : undefined;
49+
50+
if (event) {
51+
stripMetadataFromStackFrames(event);
52+
item[1] = event;
53+
}
54+
}
55+
});
56+
});
57+
4258
client.on('applyFrameMetadata', event => {
4359
// Only apply stack frame metadata to error events
4460
if (event.type) {

packages/core/src/metadata.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,19 @@ export function getMetadataForUrl(parser: StackParser, filename: string): any |
5353
* Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option.
5454
*/
5555
export function addMetadataToStackFrames(parser: StackParser, event: Event): void {
56-
try {
57-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
58-
event.exception!.values!.forEach(exception => {
59-
if (!exception.stacktrace) {
56+
event.exception?.values?.forEach(exception => {
57+
exception.stacktrace?.frames?.forEach(frame => {
58+
if (!frame.filename || frame.module_metadata) {
6059
return;
6160
}
6261

63-
for (const frame of exception.stacktrace.frames || []) {
64-
if (!frame.filename || frame.module_metadata) {
65-
continue;
66-
}
67-
68-
const metadata = getMetadataForUrl(parser, frame.filename);
62+
const metadata = getMetadataForUrl(parser, frame.filename);
6963

70-
if (metadata) {
71-
frame.module_metadata = metadata;
72-
}
64+
if (metadata) {
65+
frame.module_metadata = metadata;
7366
}
7467
});
75-
} catch {
76-
// To save bundle size we're just try catching here instead of checking for the existence of all the different objects.
77-
}
68+
});
7869
}
7970

8071
/**

0 commit comments

Comments
 (0)