-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathasyncLocalStorageContextManager.ts
More file actions
214 lines (192 loc) · 7.79 KB
/
asyncLocalStorageContextManager.ts
File metadata and controls
214 lines (192 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* NOTICE from the Sentry authors:
* This implementation follows the behavior of OpenTelemetry’s `@opentelemetry/context-async-hooks`
* package, combining logic that upstream splits across:
* - https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-context-async-hooks/src/AbstractAsyncHooksContextManager.ts
* - https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-context-async-hooks/src/AsyncLocalStorageContextManager.ts
* It is a single-class re-implementation for Sentry (not a verbatim copy of those files).
*/
import type { Context, ContextManager } from '@opentelemetry/api';
import { ROOT_CONTEXT } from '@opentelemetry/api';
import { AsyncLocalStorage } from 'node:async_hooks';
import { EventEmitter } from 'node:events';
import { SENTRY_SCOPES_CONTEXT_KEY } from './constants';
import type { AsyncLocalStorageLookup } from './contextManager';
import { buildContextWithSentryScopes } from './utils/buildContextWithSentryScopes';
import { setIsSetup } from './utils/setupCheck';
type ListenerFn = (...args: unknown[]) => unknown;
/**
* Per-event map from user listeners to context-bound listeners.
*/
type PatchMap = Record<string, WeakMap<ListenerFn, ListenerFn>>;
const ADD_LISTENER_METHODS = ['addListener', 'on', 'once', 'prependListener', 'prependOnceListener'] as const;
/**
* OpenTelemetry-compatible context manager using Node.js `AsyncLocalStorage`.
* Semantics match `@opentelemetry/context-async-hooks` (function `bind` + `EventEmitter` patching).
*/
export class SentryAsyncLocalStorageContextManager implements ContextManager {
protected readonly _asyncLocalStorage = new AsyncLocalStorage<Context>();
private readonly _kOtListeners = Symbol('OtListeners');
private _wrapped = false;
public constructor() {
setIsSetup('SentryContextManager');
}
public active(): Context {
return this._asyncLocalStorage.getStore() ?? ROOT_CONTEXT;
}
public with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(
context: Context,
fn: F,
thisArg?: ThisParameterType<F>,
...args: A
): ReturnType<F> {
const ctx2 = buildContextWithSentryScopes(context, this.active());
const cb = thisArg == null ? fn : fn.bind(thisArg);
return this._asyncLocalStorage.run(ctx2, cb as never, ...args);
}
public enable(): this {
return this;
}
public disable(): this {
this._asyncLocalStorage.disable();
return this;
}
public bind<T>(context: Context, target: T): T {
if (target instanceof EventEmitter) {
return this._bindEventEmitter(context, target);
}
if (typeof target === 'function') {
return this._bindFunction(context, target as unknown as ListenerFn) as T;
}
return target;
}
/**
* Gets underlying AsyncLocalStorage and symbol to allow lookup of scope.
* This is Sentry-specific.
*/
public getAsyncLocalStorageLookup(): AsyncLocalStorageLookup {
return {
asyncLocalStorage: this._asyncLocalStorage,
contextSymbol: SENTRY_SCOPES_CONTEXT_KEY,
};
}
private _bindFunction(context: Context, target: ListenerFn): ListenerFn {
const managerWith = this.with.bind(this);
const contextWrapper = function (this: never, ...args: unknown[]) {
return managerWith(context, () => target.apply(this, args));
};
Object.defineProperty(contextWrapper, 'length', {
enumerable: false,
configurable: true,
writable: false,
value: target.length,
});
return contextWrapper;
}
private _bindEventEmitter<T extends EventEmitter>(context: Context, ee: T): T {
if (this._getPatchMap(ee) !== undefined) {
return ee;
}
this._createPatchMap(ee);
for (const methodName of ADD_LISTENER_METHODS) {
if (ee[methodName] === undefined) continue;
ee[methodName] = this._patchAddListener(
ee,
ee[methodName] as unknown as (...args: unknown[]) => unknown,
context,
);
}
if (typeof ee.removeListener === 'function') {
// oxlint-disable-next-line @typescript-eslint/unbound-method -- patched like upstream OTel context manager
ee.removeListener = this._patchRemoveListener(ee, ee.removeListener as (...args: unknown[]) => unknown);
}
if (typeof ee.off === 'function') {
// oxlint-disable-next-line @typescript-eslint/unbound-method
ee.off = this._patchRemoveListener(ee, ee.off as (...args: unknown[]) => unknown);
}
if (typeof ee.removeAllListeners === 'function') {
ee.removeAllListeners = this._patchRemoveAllListeners(
ee,
// oxlint-disable-next-line @typescript-eslint/unbound-method
ee.removeAllListeners as (...args: unknown[]) => unknown,
);
}
return ee;
}
private _patchRemoveListener(ee: EventEmitter, original: (...args: unknown[]) => unknown) {
// oxlint-disable-next-line @typescript-eslint/no-this-alias
const contextManager = this;
return function (this: unknown, event: string, listener: ListenerFn) {
const events = contextManager._getPatchMap(ee)?.[event];
if (events === undefined) {
return original.call(this, event, listener);
}
const patchedListener = events.get(listener);
return original.call(this, event, patchedListener || listener);
};
}
private _patchRemoveAllListeners(ee: EventEmitter, original: (...args: unknown[]) => unknown) {
// oxlint-disable-next-line @typescript-eslint/no-this-alias
const contextManager = this;
return function (this: unknown, event?: string) {
const map = contextManager._getPatchMap(ee);
if (map !== undefined) {
if (arguments.length === 0) {
contextManager._createPatchMap(ee);
} else if (event !== undefined && map[event] !== undefined) {
// oxlint-disable-next-line @typescript-eslint/no-dynamic-delete -- event-keyed listener map
delete map[event];
}
}
return original.apply(this, arguments);
};
}
private _patchAddListener(ee: EventEmitter, original: (...args: unknown[]) => unknown, context: Context) {
// oxlint-disable-next-line @typescript-eslint/no-this-alias
const contextManager = this;
return function (this: unknown, event: string, listener: ListenerFn) {
if (contextManager._wrapped) {
return original.call(this, event, listener);
}
let map = contextManager._getPatchMap(ee);
if (map === undefined) {
map = contextManager._createPatchMap(ee);
}
let listeners = map[event];
if (listeners === undefined) {
listeners = new WeakMap();
map[event] = listeners;
}
const patchedListener = contextManager.bind(context, listener);
listeners.set(listener, patchedListener);
contextManager._wrapped = true;
try {
return original.call(this, event, patchedListener);
} finally {
contextManager._wrapped = false;
}
};
}
private _createPatchMap(ee: EventEmitter): PatchMap {
const map = Object.create(null) as PatchMap;
(ee as unknown as Record<symbol, PatchMap>)[this._kOtListeners] = map;
return map;
}
private _getPatchMap(ee: EventEmitter): PatchMap | undefined {
return (ee as unknown as Record<symbol, PatchMap | undefined>)[this._kOtListeners];
}
}