UNPKG

inceptum

Version:

hipages take on the foundational library for enterprise-grade apps written in NodeJS

96 lines 4.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const prometheus = require("prom-client"); const gcStats = require("prometheus-gc-stats"); const NewrelicUtil_1 = require("../newrelic/NewrelicUtil"); const AdminPortPlugin_1 = require("../web/AdminPortPlugin"); const LogManager_1 = require("../log/LogManager"); const OSMetricsService_1 = require("./OSMetricsService"); const Logger = LogManager_1.LogManager.getLogger(__filename); class MetricsPlugin { constructor() { this.name = 'MetricsPlugin'; this.osMetricsService = new OSMetricsService_1.OSMetricsService(); } getName() { return this.name; } didStart(app, pluginContext) { this.prometheusTimer = prometheus.collectDefaultMetrics(); prometheus.register.setDefaultLabels({ app: app.getConfig('app.name', 'not_set') }); const startGcStats = gcStats(); startGcStats(); this.registerOSMetrics(app); const context = app.getContext(); const adminExpress = pluginContext.get(AdminPortPlugin_1.default.CONTEXT_APP_KEY); if (adminExpress) { Logger.info('Registering metrics endpoint in Admin port'); adminExpress.get('/metrics', async (req, res) => { NewrelicUtil_1.NewrelicUtil.setIgnoreTransaction(true); res.type('text/plain'); res.send(prometheus.register.metrics()); }); } } registerOSMetrics(app) { if (app.getConfig('metrics.osmetrics.enabled', 'true') !== 'false') { this.lastMetrics = this.osMetricsService.getOSMetrics(); if (this.lastMetrics.user !== undefined) { // We're able to collect cpu stats. Let's register this Counter this.cpuStats = new prometheus.Counter({ name: 'proc_cpu', help: 'CPU stats from the OS', labelNames: ['type'], }); } else { Logger.info('Can\'t collect cpu stats (this OS doesn\'t have procfs mounted!. Skipping'); } if (this.lastMetrics.load1 !== undefined) { // We're able to collect load stats. Let's register this Gauge this.loadStats = new prometheus.Gauge({ name: 'proc_load', help: 'Load information', labelNames: ['period'], }); } else { Logger.info('Can\'t collect load average stats (this OS doesn\'t have procfs mounted!. Skipping'); } const interval = app.getConfig('metrics.osmetrics.refreshMillis', 20000); this.statsTimer = setInterval(() => { this.pushStats(); }, typeof interval === 'string' ? parseInt(interval, 10) : interval); } } pushStats() { const newStats = this.osMetricsService.getOSMetrics(); if (this.cpuStats) { OSMetricsService_1.CPUOSMetricNames.forEach((name) => this.pushIndividualCPUStat(name, newStats)); } if (this.loadStats) { OSMetricsService_1.LoadOSMetricNames.forEach((name) => this.pushIndividualLoadStat(name, newStats)); } this.lastMetrics = newStats; } pushIndividualCPUStat(name, newStats) { if (newStats[name] !== undefined && this.lastMetrics[name] !== undefined) { this.cpuStats.inc({ type: name }, newStats[name] - this.lastMetrics[name]); } } pushIndividualLoadStat(name, newStats) { if (newStats[name] !== undefined && this.lastMetrics[name] !== undefined) { this.loadStats.set({ period: name }, newStats[name]); } } didStop() { if (this.statsTimer) { clearInterval(this.statsTimer); } prometheus.register.clear(); if (this.prometheusTimer) { Logger.info('Shutting down prometheus interval'); clearInterval(this.prometheusTimer); } } } exports.default = MetricsPlugin; //# sourceMappingURL=MetricsPlugin.js.map