@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
99 lines (98 loc) • 2.98 kB
JavaScript
import { CONSOLE_LEVELS, GLOBAL_OBJ, consoleIntegration as consoleIntegration$1, defineIntegration, fill, markFunctionWrapped, maybeInstrument, originalConsoleMethods, triggerHandlers } from "@sentry/core";
// src/integrations/console.ts
/**
* Node-specific console integration that captures breadcrumbs and handles
* the AWS Lambda runtime replacing console methods after our patch.
*
* In Lambda, console methods are patched via `Object.defineProperty` so that
* external replacements (by the Lambda runtime) are absorbed as the delegate
* while our wrapper stays in place. Outside Lambda, this delegates entirely
* to the core `consoleIntegration` which uses the simpler `fill`-based patch.
*/
const consoleIntegration = defineIntegration((options = {}) => {
return {
name: "Console",
setup(client) {
if (process.env.LAMBDA_TASK_ROOT) maybeInstrument("console", instrumentConsoleLambda);
consoleIntegration$1({
...options,
filter: [...options.filter || [], "[DEP0205] DeprecationWarning"]
}).setup?.(client);
}
};
});
/**
* NOTE: This currently ignores the filter option.
* We can revisit this later.
*/
function instrumentConsoleLambda() {
const consoleObj = GLOBAL_OBJ?.console;
if (!consoleObj) return;
CONSOLE_LEVELS.forEach((level) => {
if (level in consoleObj) patchWithDefineProperty(consoleObj, level);
});
}
function patchWithDefineProperty(consoleObj, level) {
const nativeMethod = consoleObj[level];
originalConsoleMethods[level] = nativeMethod;
let delegate = nativeMethod;
let savedDelegate;
let isExecuting = false;
const wrapper = function(...args) {
if (isExecuting) {
nativeMethod.apply(consoleObj, args);
return;
}
isExecuting = true;
try {
triggerHandlers("console", {
args,
level
});
delegate.apply(consoleObj, args);
} finally {
isExecuting = false;
}
};
markFunctionWrapped(wrapper, nativeMethod);
const sandboxBypass = nativeMethod.bind(consoleObj);
originalConsoleMethods[level] = sandboxBypass;
try {
let current = wrapper;
Object.defineProperty(consoleObj, level, {
configurable: true,
enumerable: true,
get() {
return current;
},
set(newValue) {
if (newValue === wrapper) {
if (savedDelegate !== void 0) {
delegate = savedDelegate;
savedDelegate = void 0;
}
current = wrapper;
} else if (newValue === sandboxBypass) {
savedDelegate = delegate;
current = sandboxBypass;
} else if (typeof newValue === "function" && !newValue.__sentry_original__) {
delegate = newValue;
current = wrapper;
} else current = newValue;
}
});
} catch {
fill(consoleObj, level, function(originalConsoleMethod) {
originalConsoleMethods[level] = originalConsoleMethod;
return function(...args) {
triggerHandlers("console", {
args,
level
});
originalConsoleMethods[level]?.apply(this, args);
};
});
}
}
export { consoleIntegration };
//# sourceMappingURL=console.js.map