fyipe-staging
Version:
Fyipe is a JS package that tracks error event and send logs from your applications to your fyipe dashboard.
89 lines (86 loc) • 3.08 kB
JavaScript
/*eslint-disable no-unused-vars*/
import Module from 'module';
const semver = require('semver');
import MongooseListener from './listeners/mongoose';
import IncomingListener from './listeners/incomingListener';
import OutgoingListener from './listeners/outgoingListener';
import PerfTimer from './utils/perfTimer';
import HrTimer from './utils/hrTimer';
class PerformanceTracker {
#apiUrl;
#appId;
#appKey;
#nodeVer;
#nodeUse;
#start;
#end;
#store;
constructor({
apiUrl,
appId,
appKey,
trackIncomingRequest = true,
trackOutgoingRequest = true,
app, // express app instance
}) {
this.#apiUrl = apiUrl;
this.#appId = appId;
this.#appKey = appKey;
this.#nodeVer = process.versions.node;
if (semver.satisfies(this.#nodeVer, '>10.0.0')) {
this.#nodeUse = true;
} else {
this.#nodeUse = false;
}
if (this.#nodeUse) {
const perf = new PerfTimer(this.#apiUrl, this.#appId, this.#appKey);
const { start, end, store } = perf;
this.#start = start;
this.#end = end;
this.#store = store(); // returns the store instance
} else {
const hrt = new HrTimer(this.#apiUrl, this.#appId, this.#appKey);
const { start, end, store } = hrt;
this.#start = start;
this.#end = end;
this.#store = store(); // returns the store instance
}
if (trackIncomingRequest) {
this._setUpIncomingListener(app);
}
if (trackOutgoingRequest) {
this._setUpOutgoingListener();
}
// setup process handler here
// listen for when the server is killed, terminated, unhandledRejection, or uncaughtException
// send response back to our backend
process.on('SIGINT', this._sendDataOnExit.bind(this));
process.on('SIGTERM', this._sendDataOnExit.bind(this));
process.on('SIGHUP', this._sendDataOnExit.bind(this));
process.on('exit', this._sendDataOnExit.bind(this));
process.on('unhandledRejection', this._sendDataOnExit.bind(this));
process.on('uncaughtException', this._sendDataOnExit.bind(this));
}
async _sendDataOnExit() {
await this.#store.processDataOnExit();
}
_setUpOutgoingListener() {
return new OutgoingListener(this.#start, this.#end, this.#store);
}
setUpDataBaseListener() {
const load = Module._load;
const _this = this;
Module._load = function(request, parent) {
const res = load.apply(this, arguments);
if (request === 'mongoose') {
const mongo = new MongooseListener(_this.#start, _this.#end);
return mongo._setUpMongooseListener(res);
}
return res;
};
}
_setUpIncomingListener(app) {
return new IncomingListener(this.#start, this.#end, this.#store, app);
}
}
export default PerformanceTracker;