Skip to content

Commit d2fce9b

Browse files
authored
test(node): Add NestJS OpenTelemetry Tests (getsentry#10684)
Part of getsentry#9907
1 parent de681dc commit d2fce9b

File tree

6 files changed

+275
-5
lines changed

6 files changed

+275
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ jobs:
899899
- name: Run integration tests
900900
env:
901901
NODE_VERSION: ${{ matrix.node }}
902+
TS_VERSION: ${{ matrix.typescript }}
902903
run: |
903904
cd dev-packages/node-integration-tests
904905
yarn test

dev-packages/node-integration-tests/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
},
2929
"dependencies": {
3030
"@hapi/hapi": "^20.3.0",
31+
"@nestjs/core": "^10.3.3",
32+
"@nestjs/common": "^10.3.3",
33+
"@nestjs/platform-express": "^10.3.3",
3134
"@prisma/client": "3.15.2",
3235
"@sentry/node": "7.100.0",
3336
"@sentry/tracing": "7.100.0",
@@ -50,6 +53,8 @@
5053
"nock": "^13.1.0",
5154
"pg": "^8.7.3",
5255
"proxy": "^2.1.1",
56+
"reflect-metadata": "0.2.1",
57+
"rxjs": "^7.8.1",
5358
"yargs": "^16.2.0"
5459
},
5560
"devDependencies": {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
3+
import { loggingTransport, sendPortToRunner } from '@sentry-internal/node-integration-tests';
4+
import * as Sentry from '@sentry/node-experimental';
5+
6+
Sentry.init({
7+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
8+
release: '1.0',
9+
tracesSampleRate: 1.0,
10+
transport: loggingTransport,
11+
});
12+
13+
import { Controller, Get, Injectable, Module } from '@nestjs/common';
14+
import { NestFactory } from '@nestjs/core';
15+
16+
const port = 3450;
17+
18+
// Stop the process from exiting before the transaction is sent
19+
// eslint-disable-next-line @typescript-eslint/no-empty-function
20+
setInterval(() => {}, 1000);
21+
22+
@Injectable()
23+
class AppService {
24+
getHello(): string {
25+
return 'Hello World!';
26+
}
27+
}
28+
29+
@Controller()
30+
class AppController {
31+
constructor(private readonly appService: AppService) {}
32+
33+
@Get()
34+
getHello(): string {
35+
return this.appService.getHello();
36+
}
37+
}
38+
39+
@Module({
40+
imports: [],
41+
controllers: [AppController],
42+
providers: [AppService],
43+
})
44+
class AppModule {}
45+
46+
async function init(): Promise<void> {
47+
const app = await NestFactory.create(AppModule);
48+
await app.listen(port);
49+
sendPortToRunner(port);
50+
}
51+
52+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
53+
init();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
3+
4+
jest.setTimeout(20000);
5+
6+
const { TS_VERSION } = process.env;
7+
const isOldTS = TS_VERSION && TS_VERSION.startsWith('3.');
8+
9+
// This is required to run the test with ts-node and decorators
10+
process.env.TS_NODE_PROJECT = `${__dirname}/tsconfig.json`;
11+
12+
conditionalTest({ min: 16 })('nestjs auto instrumentation', () => {
13+
afterAll(async () => {
14+
cleanupChildProcesses();
15+
});
16+
17+
const CREATION_TRANSACTION = {
18+
transaction: 'Create Nest App',
19+
};
20+
21+
const GET_TRANSACTION = {
22+
transaction: 'GET /',
23+
spans: expect.arrayContaining([
24+
expect.objectContaining({
25+
description: 'GET /',
26+
data: expect.objectContaining({
27+
'nestjs.callback': 'getHello',
28+
'nestjs.controller': 'AppController',
29+
'nestjs.type': 'request_context',
30+
'otel.kind': 'INTERNAL',
31+
'sentry.op': 'http',
32+
}),
33+
}),
34+
]),
35+
};
36+
37+
test('should auto-instrument `nestjs` package', done => {
38+
if (isOldTS) {
39+
// Skipping test on old TypeScript
40+
return done();
41+
}
42+
43+
createRunner(__dirname, 'scenario.ts')
44+
.expect({ transaction: CREATION_TRANSACTION })
45+
.expect({ transaction: GET_TRANSACTION })
46+
.start(done)
47+
.makeRequest('get', '/');
48+
});
49+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"include": ["scenario.ts"],
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"emitDecoratorMetadata": true,
6+
"experimentalDecorators": true,
7+
"target": "ES2021",
8+
}
9+
}

0 commit comments

Comments
 (0)