diff --git a/dev-packages/node-integration-tests/suites/fs-instrumentation/test.ts b/dev-packages/node-integration-tests/suites/fs-instrumentation/test.ts index 81b366889d37..adb163e72da5 100644 --- a/dev-packages/node-integration-tests/suites/fs-instrumentation/test.ts +++ b/dev-packages/node-integration-tests/suites/fs-instrumentation/test.ts @@ -49,353 +49,345 @@ describe('fs instrumentation', () => { // On Bun and Deno the ESM `node:fs` exports are not tied to the `fs` object we patch, so only `fs.promises` is traced there. const testUnlessNonNodeEsm = test.runIf(mode === 'cjs' || supports({ runtimes: ['node'] })); + let scenario: ReturnType | undefined; + async function runScenario() { + const paths = [ + '/readFile-error', + '/mkdtemp', + ...(mode === 'cjs' || supports({ runtimes: ['node'] }) + ? ['/readFile', '/copyFile', '/link', '/symlink', '/exists'] + : []), + ...(mode === 'esm' && supports({ runtimes: ['node'] }) ? ['/named-imports'] : []), + ]; + const runner = createRunner(); + const collectors = paths.map(path => [path, runner.collectStreamedSpansUntilSegment(`GET ${path}`)] as const); + const results = new Map(); + const started = runner.start(); + for (const path of paths) { + results.set(path, await started.makeRequest('get', path)); + } + await started.completed(); + return new Map( + await Promise.all( + collectors.map( + async ([path, spans]) => + [path, { result: results.get(path), container: { items: await spans } }] as const, + ), + ), + ); + } + async function getScenario(path: string) { + scenario ??= runScenario(); + return (await scenario).get(path)!; + } + test('should create spans for fs operations that take target argument', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /readFile-error', [ - { - name: 'fs.readFile', - status: 'error', - attributes: { - 'error.type': { type: 'string', value: 'ENOENT' }, - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-that-doesnt-exist.txt'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/readFile-error'); + expectFileSpans('GET /readFile-error', [ + { + name: 'fs.readFile', + status: 'error', + attributes: { + 'error.type': { type: 'string', value: 'ENOENT' }, + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-that-doesnt-exist.txt'), }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/readFile-error'); expect(result).toEqual('done'); - await runner.completed(); }); testUnlessNonNodeEsm('should create spans for fs operations that take one path', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /readFile', [ - { - name: 'fs.readFile', - status: 'ok', - attributes: { - path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/readFile'); + expectFileSpans('GET /readFile', [ + { + name: 'fs.readFile', + status: 'ok', + attributes: { + path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.readFile', + status: 'ok', + attributes: { + path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promises.txt') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.readFile', + status: 'ok', + attributes: { + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt'), }, - { - name: 'fs.readFile', - status: 'ok', - attributes: { - path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promises.txt') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.readFile', - status: 'ok', - attributes: { - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/readFile'); expect(result).toEqual('done'); - await runner.completed(); }); // CJS destructuring copies the functions at require time, before the instrumentation patches them. test.runIf(mode === 'esm' && supports({ runtimes: ['node'] }))( 'should create spans for named imports from fs and fs/promises', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /named-imports', [ - { - name: 'fs.readFileSync', - status: 'ok', - attributes: { - path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.readFile', - status: 'ok', - attributes: { - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promises.txt'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/named-imports'); + expectFileSpans('GET /named-imports', [ + { + name: 'fs.readFileSync', + status: 'ok', + attributes: { + path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.readFile', + status: 'ok', + attributes: { + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promises.txt'), }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/named-imports'); expect(result).toEqual('done'); - await runner.completed(); }, ); testUnlessNonNodeEsm('should create spans for fs operations that take src and dest arguments', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /copyFile', [ - { - name: 'fs.copyFile', - status: 'ok', - attributes: { - src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, - dest_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt.copy') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/copyFile'); + expectFileSpans('GET /copyFile', [ + { + name: 'fs.copyFile', + status: 'ok', + attributes: { + src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, + dest_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt.copy') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.copyFile', + status: 'ok', + attributes: { + src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promises.txt') }, + dest_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promises.txt.copy'), }, - { - name: 'fs.copyFile', - status: 'ok', - attributes: { - src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promises.txt') }, - dest_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promises.txt.copy'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.copyFile', - status: 'ok', - attributes: { - src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promisify.txt') }, - dest_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt.copy'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.copyFile', + status: 'ok', + attributes: { + src_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file-promisify.txt') }, + dest_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt.copy'), }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/copyFile'); expect(result).toEqual('done'); - await runner.completed(); }); testUnlessNonNodeEsm( 'should create spans for fs operations that take existing path and new path arguments', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /link', [ - { - name: 'fs.link', - status: 'ok', - attributes: { - existing_path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file.txt'), - }, - new_path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file.txt.link'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/link'); + expectFileSpans('GET /link', [ + { + name: 'fs.link', + status: 'ok', + attributes: { + existing_path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file.txt'), }, - { - name: 'fs.link', - status: 'ok', - attributes: { - existing_path_argument: { - type: 'string', - value: expect.stringMatching('/some-file-promises.txt'), - }, - new_path_argument: { - type: 'string', - value: expect.stringMatching('/some-file-promises.txt.link'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + new_path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file.txt.link'), }, - { - name: 'fs.link', - status: 'ok', - attributes: { - existing_path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt'), - }, - new_path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt.link'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.link', + status: 'ok', + attributes: { + existing_path_argument: { + type: 'string', + value: expect.stringMatching('/some-file-promises.txt'), + }, + new_path_argument: { + type: 'string', + value: expect.stringMatching('/some-file-promises.txt.link'), + }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.link', + status: 'ok', + attributes: { + existing_path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt'), + }, + new_path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt.link'), }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/link'); expect(result).toEqual('done'); - await runner.completed(); }, ); test('should create spans for fs operations that take prefix argument', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /mkdtemp', [ - { - name: 'fs.mkdtemp', - status: 'ok', - attributes: { - prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.mkdtemp', - status: 'ok', - attributes: { - prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.mkdtemp', - status: 'ok', - attributes: { - prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - ]), - }) - .start(); + const { result, container } = await getScenario('/mkdtemp'); + expectFileSpans('GET /mkdtemp', [ + { + name: 'fs.mkdtemp', + status: 'ok', + attributes: { + prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.mkdtemp', + status: 'ok', + attributes: { + prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.mkdtemp', + status: 'ok', + attributes: { + prefix_argument: { type: 'string', value: expect.stringMatching('/foo-') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/mkdtemp'); expect(result).toEqual('done'); - await runner.completed(); }); testUnlessNonNodeEsm('should create spans for fs symlink operations that take target argument', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /symlink', [ - { - name: 'fs.symlink', - status: 'ok', - attributes: { - target_argument: { type: 'string', value: expect.stringMatching('/some-file-promisify.txt') }, - path_argument: { type: 'string', value: expect.stringMatching('/some-file-promisify.txt.symlink') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/symlink'); + expectFileSpans('GET /symlink', [ + { + name: 'fs.symlink', + status: 'ok', + attributes: { + target_argument: { type: 'string', value: expect.stringMatching('/some-file-promisify.txt') }, + path_argument: { type: 'string', value: expect.stringMatching('/some-file-promisify.txt.symlink') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.symlink', + status: 'ok', + attributes: { + target_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt'), }, - { - name: 'fs.symlink', - status: 'ok', - attributes: { - target_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt'), - }, - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt.symlink'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt.symlink'), }, - { - name: 'fs.symlink', - status: 'ok', - attributes: { - target_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt'), - }, - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt.symlink'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.symlink', + status: 'ok', + attributes: { + target_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt'), }, - ]), - }) - .start(); + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt.symlink'), + }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/symlink'); expect(result).toEqual('done'); - await runner.completed(); }); testUnlessNonNodeEsm('should create spans for fs.exists callback and promisified versions', async () => { - const runner = createRunner() - .expect({ - span: expectFileSpans('GET /exists', [ - { - name: 'fs.exists', - status: 'ok', - attributes: { - path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, - }, - { - name: 'fs.exists', - status: 'ok', - attributes: { - path_argument: { - type: 'string', - value: expect.stringMatching('/fixtures/some-file-promisify.txt'), - }, - [SENTRY_OP]: { type: 'string', value: 'file' }, - [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, - }, + const { result, container } = await getScenario('/exists'); + expectFileSpans('GET /exists', [ + { + name: 'fs.exists', + status: 'ok', + attributes: { + path_argument: { type: 'string', value: expect.stringMatching('/fixtures/some-file.txt') }, + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + { + name: 'fs.exists', + status: 'ok', + attributes: { + path_argument: { + type: 'string', + value: expect.stringMatching('/fixtures/some-file-promisify.txt'), }, - ]), - }) - .start(); + [SENTRY_OP]: { type: 'string', value: 'file' }, + [SENTRY_ORIGIN]: { type: 'string', value: 'auto.file.fs' }, + }, + }, + ])(container); - const result = await runner.makeRequest('get', '/exists'); expect(result).toEqual('done'); - await runner.completed(); }); }, { copyPaths: ['fixtures'] },