Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change events for Nest.js #29

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as os from 'os';
import http from 'http';
import ResponseTime from 'response-time';
import promClient from 'prom-client';

import type {
Counter,
CounterConfiguration,
Expand Down Expand Up @@ -266,7 +265,7 @@ export class OpenAPM extends LevitateEvents {
// Skip the OPTIONS requests not to blow up cardinality. Express does not provide
// information about the route for OPTIONS requests, which makes it very
// hard to detect correct PATH. Until we fix it properly, the requests are skipped
// to not blow up the cardinality.
// to not blow up the cardinality.
if (!req.route && req.method === 'OPTIONS') {
return;
}
Expand Down Expand Up @@ -315,7 +314,7 @@ export class OpenAPM extends LevitateEvents {
}
if (moduleName === 'nestjs') {
const { NestFactory } = require('@nestjs/core');
instrumentNestFactory(NestFactory, this._REDMiddleware);
instrumentNestFactory(NestFactory, this._REDMiddleware, this);
}
} catch (error) {
if (Object.keys(moduleNames).includes(moduleName)) {
Expand Down
42 changes: 40 additions & 2 deletions src/clients/nestjs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { NestFactoryStatic } from '@nestjs/core/nest-factory';
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
redMiddleware: Function,
openapm: OpenAPM
) => {
// Check if the NestFactory is already wrapped
if (!isWrapped(nestFactory, 'create')) {
Expand All @@ -19,9 +23,43 @@ export const instrumentNestFactory = (
);
// 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);
}
};
12 changes: 12 additions & 0 deletions src/levitate/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class LevitateEvents extends EventEmitter {
event: 'application_started',
...args: (DomainEventsBody | any)[]
): boolean;
public emit(
event: 'application_stopped',
...args: (DomainEventsBody | any)[]
): boolean;
public emit(event: any, ...args: any[]): any {
return super.emit(event, ...args);
}
Expand All @@ -50,6 +54,10 @@ export class LevitateEvents extends EventEmitter {
event: 'application_started',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public on(
event: 'application_stopped',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public on(event: any, listener: (...args: any[]) => void): this {
return super.on(event, listener);
}
Expand All @@ -58,6 +66,10 @@ export class LevitateEvents extends EventEmitter {
event: 'application_started',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public once(
event: 'application_stopped',
listener: (...args: (DomainEventsBody | any)[]) => void
): this;
public once(event: any, listener: (...args: any[]) => void): this {
return super.on(event, listener);
}
Expand Down
Loading