-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnestjs.ts
65 lines (60 loc) · 2.12 KB
/
nestjs.ts
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
import * as os from 'os';
import { isWrapped, wrap } from '../shimmer';
import type { NestFactoryStatic } from '@nestjs/core/nest-factory';
import type { NestApplication } from '@nestjs/core';
import type OpenAPM from '../OpenAPM';
export const instrumentNestFactory = (
nestFactory: NestFactoryStatic,
redMiddleware: Function,
openapm: OpenAPM
) => {
// Check if the NestFactory is already wrapped
if (!isWrapped(nestFactory, 'create')) {
// Wrap using the wrapper function
wrap(
nestFactory,
'create',
function (original: NestFactoryStatic['create']) {
return async function (this: NestFactoryStatic['create'], ...args) {
const app = await original.apply(
this,
args as Parameters<NestFactoryStatic['create']>
);
// Add a global RED Middleware to the application
app.use(redMiddleware);
wrap(app, 'listen', (ogListen: NestApplication['listen']) => {
return function (
this: NestApplication['listen'],
...args: Parameters<NestApplication['listen']>
) {
openapm.emit('application_started', {
timestamp: new Date().toISOString(),
event_name: `${openapm.program}_app`,
event_state: 'start',
entity_type: 'app',
workspace: os.hostname(),
namespace: openapm.environment,
data_source_name: openapm.levitateConfig?.dataSourceName ?? ''
});
return ogListen.apply(this, args);
} as NestApplication['listen'];
});
return app;
};
}
);
const stopEvent = () => {
openapm.emit('application_stopped', {
timestamp: new Date().toISOString(),
event_name: `${openapm.program}_app`,
event_state: 'stop',
entity_type: 'app',
workspace: os.hostname(),
namespace: openapm.environment,
data_source_name: openapm.levitateConfig?.dataSourceName ?? ''
});
};
process.on('SIGINT', stopEvent);
process.on('SIGTERM', stopEvent);
}
};