@instana/shared-metrics
Version:
Internal metrics plug-in package for Node.js monitoring with Instana
58 lines (48 loc) • 1.75 kB
JavaScript
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2015
*/
;
const path = require('path');
const nativeModuleRetry = require('./util/nativeModuleRetry');
const lag = require('event-loop-lag')(1000);
/** @type {*} */
let eventLoopStats;
exports.payloadPrefix = 'libuv';
exports.init = function init() {
const nativeModuleLoader = nativeModuleRetry.loadNativeAddOn({
nativeModuleName: 'event-loop-stats',
moduleRoot: path.join(__dirname, '..'),
message:
'Could not load event-loop-stats. Event loop information will not be available for this ' +
'application. ' +
"However, general tracing functionality remains unaffected. Enable debug logs with 'INSTANA_DEBUG=true' " +
'for more details about the error. For more information on native add-ons, visit: ' +
'https://www.ibm.com/docs/en/instana-observability/current?topic=nodejs-collector-installation#native-add-ons'
});
nativeModuleLoader.once('loaded', eventLoopStats_ => {
eventLoopStats = eventLoopStats_;
});
nativeModuleLoader.once('failed', () => {
exports.currentPayload.statsSupported = false;
});
};
Object.defineProperty(exports, 'currentPayload', {
get: function () {
const stats = sense();
stats.lag = Math.round(lag() * 100) / 100;
return stats;
}
});
function sense() {
// CASE: If the c++ module does not support the env, it might export `sense` as undefined.
// See https://github.com/bripkens/event-loop-stats/blob/v1.4.1/src/eventLoopStats.js#L1-L2
if (eventLoopStats && eventLoopStats.sense) {
const stats = eventLoopStats.sense();
stats.statsSupported = true;
return stats;
}
return {
statsSupported: false
};
}