diff --git a/lib/eventLoopAgent.js b/lib/eventLoopAgent.js index dc1ce0f..9339aa0 100644 --- a/lib/eventLoopAgent.js +++ b/lib/eventLoopAgent.js @@ -9,15 +9,15 @@ */ 'use strict' -var Agent = require('spm-agent').Agent -var monitor = require('./eventLoopStats.js') +const { Agent } = require('spm-agent') +const monitor = require('./eventLoopStats.js') module.exports = function () { - var elAgent = new Agent( + const elAgent = new Agent( { start: function (agent) { this.elListener = function (stats) { - var metric = { + const metric = { ts: Date.now(), measurement: 'nodejs.eventloop', tags: {}, diff --git a/lib/gcAgent.js b/lib/gcAgent.js index b8581f4..e1b545d 100644 --- a/lib/gcAgent.js +++ b/lib/gcAgent.js @@ -9,17 +9,12 @@ */ 'use strict' -var SpmAgent = require('spm-agent') -var Agent = SpmAgent.Agent -// var gcStats = (require('@risingstack/gc-stats'))() -var gcStats = (require('gc-stats'))() - -var config = SpmAgent.Config +const { Agent, Config } = require('spm-agent') +const gcStats = (require('gc-stats'))() function getProcessHeapInfo () { - var pm = process.memoryUsage() - var metrics = {} - // metrics.processMemory = [pm.heapUsed, pm.heapTotal, pm.rss] + const pm = process.memoryUsage() + const metrics = {} metrics.processHeapUsed = pm.heapUsed metrics.processHeapTotal = pm.heapTotal metrics.processMemoryRss = pm.rss @@ -29,13 +24,13 @@ function getProcessHeapInfo () { function getGcStats () { return { start: function (agent) { - var Measured = require('measured-core') - var fullGc = new Measured.Counter() - var incGc = new Measured.Counter() - var heapCompactions = new Measured.Counter() - var gcTimes = new Measured.Histogram() - var heapDiff = new Measured.Histogram() - var lastHeapInfo = getProcessHeapInfo() + const Measured = require('measured-core') + const fullGc = new Measured.Counter() + const incGc = new Measured.Counter() + const heapCompactions = new Measured.Counter() + const gcTimes = new Measured.Histogram() + const heapDiff = new Measured.Histogram() + let lastHeapInfo = getProcessHeapInfo() gcStats.on('stats', function (info) { if (info.gctype === 1) { incGc.inc() @@ -54,11 +49,11 @@ function getGcStats () { heapDiff.update(info.diff.usedHeapSize * -1) }) - var timerId = setInterval(function () { - var now = new Date().getTime() - var heapInfo = lastHeapInfo - var heapCompactionsJSON = heapCompactions.toJSON() - var metricValues = { + const timerId = setInterval(function () { + const now = new Date().getTime() + const heapInfo = lastHeapInfo + const heapCompactionsJSON = heapCompactions.toJSON() + const metricValues = { 'gc.inc': incGc.toJSON() || 0, 'gc.full': fullGc.toJSON() || 0, 'gc.time': gcTimes.toJSON().sum || 0, @@ -74,7 +69,7 @@ function getGcStats () { heapCompactions.reset(0) gcTimes.reset() heapDiff.reset() - }, config.collectionInterval) + }, Config.collectionInterval) if (timerId.unref) { timerId.unref() } diff --git a/lib/httpServerAgent.js b/lib/httpServerAgent.js index 418d5ab..11d5904 100644 --- a/lib/httpServerAgent.js +++ b/lib/httpServerAgent.js @@ -15,48 +15,44 @@ * */ -var http = require('http') -var https = require('https') +const http = require('http') +const https = require('https') // constants -var FINISH_EVENT_NAME = 'finish' -var ABRUPT_CLOSE_EVENT_NAME = 'close' +const FINISH_EVENT_NAME = 'finish' +const ABRUPT_CLOSE_EVENT_NAME = 'close' module.exports = function httpServerAgent () { - // var SpmAgent = require('spm-agent') - var SpmAgent = require('../../spm-agent/lib/index') - var Agent = SpmAgent.Agent - var config = SpmAgent.Config - var logger = SpmAgent.Logger - var Measured = require('measured-core') - var histogram = new Measured.Histogram() - var timer = new Measured.Timer() + const { Agent, Config, Logger } = require('spm-agent') + const Measured = require('measured-core') + const histogram = new Measured.Histogram() + const timer = new Measured.Timer() // setup context for monitoring - var ctx = { + const ctx = { reqSize: 0, resSize: 0, timer: timer, - logger: logger, + logger: Logger, histogram: histogram, stats: Measured.createCollection() } // bind monitorHttp safely - var monitor = safeProcess(ctx, monitorHttp) + const monitor = safeProcess(ctx, monitorHttp) // setup monitoring agent - var hAgent = new Agent({ + const hAgent = new Agent({ start: function (agent) { this._agent = agent patchHttpServer(monitor) patchHttpsServer(monitor) - var timerId = setInterval(function () { - var stats = ctx.stats - var httpStats = stats.toJSON() - var responseTimes = histogram.toJSON() - var now = Date.now() - var metricValue = { + const timerId = setInterval(function () { + const stats = ctx.stats + const httpStats = stats.toJSON() + const responseTimes = histogram.toJSON() + const now = Date.now() + const metricValue = { requests: httpStats.requests ? httpStats.requests.count : 0, // http.requestCount (int) errors: httpStats.errRate ? httpStats.errRate.count : 0, // http.errorCount (int) 'errors.3xx': httpStats['3xxRate'] ? httpStats['3xxRate'].count : 0, // http.3xx (int) @@ -82,7 +78,7 @@ module.exports = function httpServerAgent () { timer.reset() ctx.reqSize = 0 ctx.resSize = 0 - }, config.collectionInterval) + }, Config.collectionInterval) // unref so we can gracefully exit if (timerId.unref) { @@ -114,15 +110,15 @@ function safeProcess (ctx, fn) { * @param {http.ServerResponse} res */ function monitorHttp (ctx, req, res) { - var stopwatch = ctx.timer.start() + let stopwatch = ctx.timer.start() function endOfConnectionHandler () { - // cached vars - var stats = ctx.stats - var histogram = ctx.histogram + // cached consts + const stats = ctx.stats + const histogram = ctx.histogram // capture duration - var duration = stopwatch.end() + const duration = stopwatch.end() stopwatch = null stats.meter('requests').mark() @@ -164,12 +160,12 @@ function monitorHttp (ctx, req, res) { /** * Monkey-patching! */ -var origHttpCreateServer = http.createServer -var origHttpsCreateServer = https.createServer +const origHttpCreateServer = http.createServer +const origHttpsCreateServer = https.createServer function patchHttpServer (monitorReqHandler) { http.createServer = function () { - var server = origHttpCreateServer.apply(http, arguments) + const server = origHttpCreateServer.apply(http, arguments) server.on('request', monitorReqHandler) return server } @@ -178,7 +174,7 @@ function patchHttpServer (monitorReqHandler) { function patchHttpsServer (monitorReqHandler) { https.createServer = function () { - var server = origHttpsCreateServer.apply(https, arguments) + const server = origHttpsCreateServer.apply(https, arguments) server.on('request', monitorReqHandler) return server } diff --git a/lib/index.js b/lib/index.js index 0083d53..9a32a84 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,7 +7,8 @@ * This source code is to be used exclusively by users and customers of Sematext. * Please see the full license (found in LICENSE in this distribution) for details on its license and the licenses of its dependencies. */ -var SpmAgent = require('spm-agent') +// var SpmAgent = require('spm-agent') +var SpmAgent = require('../../spm-agent/lib/index') function NodeJSAgent () { if (process.env.SPM_TOKEN && !SpmAgent.Config.tokens.spm) { diff --git a/lib/processAgent.js b/lib/processAgent.js index 1911b2a..98f942a 100644 --- a/lib/processAgent.js +++ b/lib/processAgent.js @@ -9,10 +9,7 @@ */ 'use strict' -// var SpmAgent = require('spm-agent') -var SpmAgent = require('../../spm-agent/lib/index') -var Agent = SpmAgent.Agent -var config = SpmAgent.Config +const { Agent, Config } = require('spm-agent') function getProcessInfo () { const { uptime, pid, ppid, send } = process @@ -29,9 +26,9 @@ function getProcessInfo () { function getProcessStats () { return { start: function (agent) { - var timerId = setInterval(function () { - var now = new Date().getTime() - var { + const timerId = setInterval(function () { + const now = new Date().getTime() + const { uptime, pid, ppid, @@ -51,7 +48,7 @@ function getProcessStats () { processes: 1 } }) - }, config.collectionInterval) + }, Config.collectionInterval) if (timerId.unref) { timerId.unref() } @@ -59,6 +56,7 @@ function getProcessStats () { stop: function () { } } } + module.exports = function () { return new Agent(getProcessStats()) } diff --git a/lib/workerAgent.js b/lib/workerAgent.js index adf998a..4c68c5f 100644 --- a/lib/workerAgent.js +++ b/lib/workerAgent.js @@ -10,10 +10,7 @@ 'use strict' const cluster = require('cluster') -// const SpmAgent = require('spm-agent') -const SpmAgent = require('../../spm-agent/lib/index') -const Agent = SpmAgent.Agent -const config = SpmAgent.Config +const { Agent, Config } = require('spm-agent') function getWorkers () { return { @@ -28,7 +25,7 @@ function getWorkers () { fields: { workers: Object.keys(cluster.workers || {}).length || 1 } }) } - }, config.collectionInterval) + }, Config.collectionInterval) if (timerId.unref) { timerId.unref() } diff --git a/test/test.js b/test/test.js index 7bb57ef..6ee078e 100644 --- a/test/test.js +++ b/test/test.js @@ -10,7 +10,8 @@ */ /* global describe, it */ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' -var config = require('spm-agent').Config +var SpmAgent = require('spm-agent') +var config = SpmAgent.Config var port = (process.env.NJS_TEST_PORT || 8097) var receiverUrl = 'http://127.0.0.1:' + port config.rcFlat.spmSenderBulkInsertUrl = receiverUrl