-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathfs.ts
More file actions
149 lines (142 loc) · 4.86 KB
/
fs.ts
File metadata and controls
149 lines (142 loc) · 4.86 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
import { FsInstrumentation } from '@opentelemetry/instrumentation-fs';
import { defineIntegration, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { generateInstrumentOnce } from '@sentry/node-core';
const INTEGRATION_NAME = 'FileSystem';
/**
* This integration will create spans for `fs` API operations, like reading and writing files.
*
* **WARNING:** This integration may add significant overhead to your application. Especially in scenarios with a lot of
* file I/O, like for example when running a framework dev server, including this integration can massively slow down
* your application.
*
* @param options Configuration for this integration.
*/
export const fsIntegration = defineIntegration(
(
options: {
/**
* Setting this option to `true` will include any filepath arguments from your `fs` API calls as span attributes.
*
* Defaults to `false`.
*/
recordFilePaths?: boolean;
/**
* Setting this option to `true` will include the error messages of failed `fs` API calls as a span attribute.
*
* Defaults to `false`.
*/
recordErrorMessagesAsSpanAttributes?: boolean;
} = {},
) => {
return {
name: INTEGRATION_NAME,
setupOnce() {
generateInstrumentOnce(
INTEGRATION_NAME,
() =>
new FsInstrumentation({
requireParentSpan: true,
endHook(functionName, { args, span, error }) {
span.updateName(`fs.${functionName}`);
span.setAttributes({
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'file',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.file.fs',
});
if (options.recordErrorMessagesAsSpanAttributes) {
if (typeof args[0] === 'string' && FS_OPERATIONS_WITH_PATH_ARG.includes(functionName)) {
span.setAttribute('path_argument', args[0]);
} else if (
typeof args[0] === 'string' &&
typeof args[1] === 'string' &&
FS_OPERATIONS_WITH_TARGET_PATH.includes(functionName)
) {
span.setAttribute('target_argument', args[0]);
span.setAttribute('path_argument', args[1]);
} else if (typeof args[0] === 'string' && FS_OPERATIONS_WITH_PREFIX.includes(functionName)) {
span.setAttribute('prefix_argument', args[0]);
} else if (
typeof args[0] === 'string' &&
typeof args[1] === 'string' &&
FS_OPERATIONS_WITH_EXISTING_PATH_NEW_PATH.includes(functionName)
) {
span.setAttribute('existing_path_argument', args[0]);
span.setAttribute('new_path_argument', args[1]);
} else if (
typeof args[0] === 'string' &&
typeof args[1] === 'string' &&
FS_OPERATIONS_WITH_SRC_DEST.includes(functionName)
) {
span.setAttribute('src_argument', args[0]);
span.setAttribute('dest_argument', args[1]);
} else if (
typeof args[0] === 'string' &&
typeof args[1] === 'string' &&
FS_OPERATIONS_WITH_OLD_PATH_NEW_PATH.includes(functionName)
) {
span.setAttribute('old_path_argument', args[0]);
span.setAttribute('new_path_argument', args[1]);
}
}
if (error && options.recordErrorMessagesAsSpanAttributes) {
span.setAttribute('fs_error', error.message);
}
},
}),
)();
},
};
},
);
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',
];