getsentry / getsentry/sentry-javascript

Setup in shared environments creates global `__SENTRY__` carrier (logging, scopes)

Đang mở
#19,324 1 bình luận 0 reaction 1 người được giao Được @s1gr1d nhận Xem trên GitHub
Bug
Ngôn ngữ chính
TypeScript
Star
8.7k
Fork
1.8k
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
515

Mô tả

### Description

It's recommended to set up Sentry in shared environments like shown below, and only use methods on the `scope`:

```js
const client = new BrowserClient({ /* options */ });

const scope = new Scope();
scope.setClient(client);
client.init();

// No usage of global methods like `Sentry.captureException`, only on scope
scope.captureException(new Error("example"));
```

## Problem

### Logger

On SDK initialization, the logger is set up and if you didn't set `__SENTRY_DEBUG__: false` for your build, the SDK will always act on the global singleton (the `__SENTRY__` carrier).

https://github.com/getsentry/sentry-javascript/blob/337f53839102275cba0e511408fb055429715b21/packages/core/src/utils/debug-logger.ts#L112-L118

### Scopes

Calling `scope.captureException()` will also initialize the scopes on the global `__SENTRY__` carrier, which is a bigger problem as it can potentially leak scope data if there are multiple Sentry instances.

`scope.captureException()` calls the global `getIsolationScope()`. This was introduced in the following PR, in order to use the session from the isolation scope: https://github.com/getsentry/sentry-javascript/pull/14860

What happens, is that `scope.captureException()` calls `captureException` with 3 paramters:
https://github.com/getsentry/sentry-javascript/blob/337f53839102275cba0e511408fb055429715b21/packages/core/src/scope.ts#L708-L717

Eventually, this is forwarded until `_captureEvent`, where by default, the 4th parameter calls `getIsolationScope()`:
https://github.com/getsentry/sentry-javascript/blob/eb4382c82f4bc621953d9d9ae6acc97341ce37f2/packages/core/src/client.ts#L1235-L1240

## Reproduction

https://github.com/Lms24/repro-js-shared-environments

## Ideas

- `scope.captureException` could receive an optional `scope` as a third parameter which will be used as the `isolationScope`. But then, the usage would be `scope.captureException(new Error(), {}, scope)`.
- Adding a `sharedEnvSetup: boolean` to the initialization options which would allow us to conditionally use certain APIs (or for example, store the logger settings in the module scope, instead of a global scope)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.