Skip to content

Commit

Permalink
Wrap for server close
Browse files Browse the repository at this point in the history
  • Loading branch information
chtushar committed Nov 15, 2023
1 parent 0de1b60 commit 58b33d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const openapm = new OpenAPM({
openapm.on('application_started', (domainEvents) => {
console.log(domainEvents);
});
openapm.on('application_stopped', (domainEvents) => {
console.log(domainEvents);
});
openapm.instrument('express');
openapm.instrument('mysql');

Expand Down
24 changes: 22 additions & 2 deletions src/clients/express.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EventEmitter from 'events';
import type * as Express from 'express';
import type { RequestHandler } from 'express';
import { isWrapped, wrap } from '../shimmer';
import type OpenAPM from '../OpenAPM';
import { Server } from 'http';

export const instrumentExpress = (
express: typeof Express,
Expand Down Expand Up @@ -46,7 +46,27 @@ export const instrumentExpress = (
namespace: '',
data_source_name: ''
});
return original.apply(this, args);
const server = original.apply(this, args) as Server;

wrap(server, 'close', function (original: Server['close']) {
return function (
this: Server['close'],
...args: Parameters<Server['close']>
) {
openapm.emit('application_stopped', {
timestamp: new Date().toISOString(),
event_name: 'express_app',
event_state: 'stop',
entity_type: '',
workspace: '',
namespace: '',
data_source_name: ''
});
return original.apply(this, args);
};
});

return server;
};
}
);
Expand Down

0 comments on commit 58b33d7

Please sign in to comment.