@sentry/node
Version:
Sentry Node SDK using OpenTelemetry for performance instrumentation
246 lines (245 loc) • 8.28 kB
JavaScript
import { CALLBACK_FUNCTIONS, PROMISE_FUNCTIONS, SYNC_FUNCTIONS } from "./constants.js";
import { indexFs } from "./utils.js";
import { ERROR_TYPE, SENTRY_OP, SENTRY_ORIGIN } from "@sentry/conventions/attributes";
import { SPAN_STATUS_ERROR, getActiveSpan, startInactiveSpan, startSpan, suppressTracing, withActiveSpan } from "@sentry/core";
import { FILE } from "@sentry/conventions/op";
import fs from "fs";
import * as moduleNamespace from "module";
import { promisify } from "util";
// src/integrations/fs/vendored/instrumentation.ts
const moduleExports = moduleNamespace;
const syncBuiltinESMExports = moduleExports.default?.syncBuiltinESMExports ?? moduleExports.syncBuiltinESMExports;
const SPAN_ORIGIN = "auto.file.fs";
const SPAN_OP = FILE;
const FS_OPERATIONS_WITH_OLD_PATH_NEW_PATH = ["rename", "renameSync"];
const FS_OPERATIONS_WITH_SRC_DEST = [
"copyFile",
"cp",
"copyFileSync",
"cpSync"
];
const FS_OPERATIONS_WITH_EXISTING_PATH_NEW_PATH = ["link", "linkSync"];
const FS_OPERATIONS_WITH_PREFIX = ["mkdtemp", "mkdtempSync"];
const FS_OPERATIONS_WITH_TARGET_PATH = ["symlink", "symlinkSync"];
const FS_OPERATIONS_WITH_PATH_ARG = [
"access",
"appendFile",
"chmod",
"chown",
"exists",
"mkdir",
"lchown",
"lstat",
"lutimes",
"open",
"opendir",
"readdir",
"readFile",
"readlink",
"realpath",
"realpath.native",
"rm",
"rmdir",
"stat",
"truncate",
"unlink",
"utimes",
"writeFile",
"accessSync",
"appendFileSync",
"chmodSync",
"chownSync",
"existsSync",
"lchownSync",
"lstatSync",
"lutimesSync",
"opendirSync",
"mkdirSync",
"openSync",
"readdirSync",
"readFileSync",
"readlinkSync",
"realpathSync",
"realpathSync.native",
"rmdirSync",
"rmSync",
"statSync",
"truncateSync",
"unlinkSync",
"utimesSync",
"writeFileSync"
];
/**
* Builds the span attributes for a given `fs` operation, optionally including the file path arguments.
*/
function getSpanAttributes(functionName, args, config) {
const attributes = {
[SENTRY_OP]: SPAN_OP,
[SENTRY_ORIGIN]: SPAN_ORIGIN
};
if (!config.recordFilePaths) return attributes;
if (typeof args[0] === "string" && FS_OPERATIONS_WITH_PATH_ARG.includes(functionName)) attributes["path_argument"] = args[0];
else if (typeof args[0] === "string" && typeof args[1] === "string") {
if (FS_OPERATIONS_WITH_TARGET_PATH.includes(functionName)) {
attributes["target_argument"] = args[0];
attributes["path_argument"] = args[1];
} else if (FS_OPERATIONS_WITH_EXISTING_PATH_NEW_PATH.includes(functionName)) {
attributes["existing_path_argument"] = args[0];
attributes["new_path_argument"] = args[1];
} else if (FS_OPERATIONS_WITH_SRC_DEST.includes(functionName)) {
attributes["src_argument"] = args[0];
attributes["dest_argument"] = args[1];
} else if (FS_OPERATIONS_WITH_OLD_PATH_NEW_PATH.includes(functionName)) {
attributes["old_path_argument"] = args[0];
attributes["new_path_argument"] = args[1];
}
} else if (typeof args[0] === "string" && FS_OPERATIONS_WITH_PREFIX.includes(functionName)) attributes["prefix_argument"] = args[0];
return attributes;
}
/**
* This is important for 2-level functions like `realpath.native` to retain the 2nd-level
* when patching the 1st-level.
*/
function patchedFunctionWithOriginalProperties(patchedFunction, original) {
return Object.assign(patchedFunction, original);
}
const _patched = /* @__PURE__ */ new WeakMap();
function _patchMethod(obj, name, wrapper) {
const original = obj[name];
if (typeof original !== "function") return;
let patched = _patched.get(obj);
if (!patched) {
patched = /* @__PURE__ */ new Set();
_patched.set(obj, patched);
}
if (patched.has(name)) return;
patched.add(name);
obj[name] = wrapper(original);
}
function _patchSyncFunction(functionName, original, config) {
const patchedFunction = function(...args) {
const attributes = getSpanAttributes(functionName, args, config);
return startSpan({
name: `fs.${functionName}`,
onlyIfParent: true,
attributes
}, (span) => {
try {
return suppressTracing(() => original.apply(this, args));
} catch (error) {
recordError(span, error, config);
throw error;
}
});
};
return patchedFunctionWithOriginalProperties(patchedFunction, original);
}
function _patchCallbackFunction(functionName, original, config) {
const patchedFunction = function(...args) {
const lastIdx = args.length - 1;
const cb = args[lastIdx];
if (typeof cb !== "function") return original.apply(this, args);
const attributes = getSpanAttributes(functionName, args, config);
const span = startInactiveSpan({
name: `fs.${functionName}`,
onlyIfParent: true,
attributes
});
const parentSpan = getActiveSpan();
args[lastIdx] = function(...cbArgs) {
const error = cbArgs[0];
if (error) recordError(span, error, config);
span.end();
if (parentSpan) return withActiveSpan(parentSpan, () => cb.apply(this, cbArgs));
return cb.apply(this, cbArgs);
};
try {
return suppressTracing(() => original.apply(this, args));
} catch (error) {
recordError(span, error, config);
span.end();
throw error;
}
};
return patchedFunctionWithOriginalProperties(patchedFunction, original);
}
function _patchExistsCallbackFunction(original, config) {
const functionName = "exists";
const patchedFunction = function(...args) {
const lastIdx = args.length - 1;
const cb = args[lastIdx];
if (typeof cb !== "function") return original.apply(this, args);
const attributes = getSpanAttributes(functionName, args, config);
const span = startInactiveSpan({
name: `fs.${functionName}`,
onlyIfParent: true,
attributes
});
const parentSpan = getActiveSpan();
args[lastIdx] = function(...cbArgs) {
span.end();
if (parentSpan) return withActiveSpan(parentSpan, () => cb.apply(this, cbArgs));
return cb.apply(this, cbArgs);
};
try {
return suppressTracing(() => original.apply(this, args));
} catch (error) {
recordError(span, error, config);
span.end();
throw error;
}
};
const functionWithOriginalProperties = patchedFunctionWithOriginalProperties(patchedFunction, original);
const promisified = function(path) {
return new Promise((resolve) => functionWithOriginalProperties(path, resolve));
};
Object.defineProperty(promisified, "name", { value: functionName });
Object.defineProperty(functionWithOriginalProperties, promisify.custom, { value: promisified });
return functionWithOriginalProperties;
}
function _patchPromiseFunction(functionName, original, config) {
const patchedFunction = async function(...args) {
const attributes = getSpanAttributes(functionName, args, config);
return startSpan({
name: `fs.${functionName}`,
onlyIfParent: true,
attributes
}, async (span) => {
try {
return await suppressTracing(() => original.apply(this, args));
} catch (error) {
recordError(span, error, config);
throw error;
}
});
};
return patchedFunctionWithOriginalProperties(patchedFunction, original);
}
function enableFsInstrumentation(config = {}) {
for (const fName of SYNC_FUNCTIONS) {
const { objectToPatch, functionNameToPatch } = indexFs(fs, fName);
_patchMethod(objectToPatch, functionNameToPatch, (original) => _patchSyncFunction(fName, original, config));
}
for (const fName of CALLBACK_FUNCTIONS) {
const { objectToPatch, functionNameToPatch } = indexFs(fs, fName);
if (fName === "exists") _patchMethod(objectToPatch, functionNameToPatch, (original) => _patchExistsCallbackFunction(original, config));
else _patchMethod(objectToPatch, functionNameToPatch, (original) => _patchCallbackFunction(fName, original, config));
}
const fsPromises = fs.promises;
for (const fName of PROMISE_FUNCTIONS) _patchMethod(fsPromises, fName, (original) => _patchPromiseFunction(fName, original, config));
try {
syncBuiltinESMExports?.();
} catch {}
}
/**
* Sets the error status on the span and, if configured, records the error type as a span attribute.
*/
function recordError(span, error, config) {
span.setStatus({
code: SPAN_STATUS_ERROR,
message: "internal_error"
});
if (config.recordErrorMessagesAsSpanAttributes && error instanceof Error) span.setAttribute(ERROR_TYPE, error.code ?? error.name);
}
export { enableFsInstrumentation };
//# sourceMappingURL=instrumentation.js.map